public function testRefusesToWrapAnIndexedArray() { $array = array(0 => "One", 1 => "Two"); $this->assertFalse(ArrayLib::is_associative($array)); /* * Expect user_error() to be called below, if enabled * (tobych) That should be an exception. Something like: * $this->setExpectedException('InvalidArgumentException'); */ // $arrayData = new ArrayData($array); }
/** * Gets a field from this object. * * * If the value is an object but not an instance of * ViewableData, it will be converted recursively to an * ArrayData. * * If the value is an associative array, it will likewise be * converted recursively to an ArrayData. * * @param string $field * @return mixed */ public function getField($field) { $value = $this->array[$field]; if (is_object($value) && !$value instanceof ViewableData) { return new ArrayData($value); } elseif (ArrayLib::is_associative($value)) { return new ArrayData($value); } else { return $value; } }
/** * Builds a new currency field based on the allowed currencies configured * * @return FormField */ protected function buildCurrencyField() { $name = $this->getName(); $allowedCurrencies = $this->getAllowedCurrencies(); if ($allowedCurrencies) { $field = new DropdownField("{$name}[Currency]", _t('MoneyField.FIELDLABELCURRENCY', 'Currency'), ArrayLib::is_associative($allowedCurrencies) ? $allowedCurrencies : array_combine($allowedCurrencies, $allowedCurrencies)); } else { $field = new TextField("{$name}[Currency]", _t('MoneyField.FIELDLABELCURRENCY', 'Currency')); } $field->setReadonly($this->isReadonly()); $field->setDisabled($this->isDisabled()); return $field; }
/** * Merge some arbitrary data in with this object. This method returns a {@link ViewableData_Customised} instance * with references to both this and the new custom data. * * Note that any fields you specify will take precedence over the fields on this object. * * @param array|ViewableData $data * @return ViewableData_Customised */ public function customise($data) { if (is_array($data) && (empty($data) || ArrayLib::is_associative($data))) { $data = new ArrayData($data); } if ($data instanceof ViewableData) { return new ViewableData_Customised($this, $data); } throw new InvalidArgumentException('ViewableData->customise(): $data must be an associative array or a ViewableData instance'); }
/** * This is the main translator function. Returns the string defined by $class and $entity according to the * currently set locale. * * @param string $entity Entity that identifies the string. It must be in the form "Namespace.Entity" where * Namespace will be usually the class name where this string is used and Entity identifies * the string inside the namespace. * @param string $string The original string itself. In a usual call this is a mandatory parameter, but if you are * reusing a string which has already been "declared" (using another call to this function, * with the same class and entity), you can omit it. * @param string $context (optional) If the string can be difficult to translate by any reason, you can help * translators with some more info using this param * @param array $injection (optional) array of key value pairs that are used to replace corresponding * expressions in {curly brackets} in the $string. The injection array can also be * used as the their argument to the _t() function * @return string The translated string, according to the currently set locale {@link i18n::set_locale()} */ public static function _t($entity, $string = "", $context = "", $injection = null) { //fetch the injection array out of the parameters (if it is present) $argList = func_get_args(); $argNum = func_num_args(); //_t($entity, $string = "", $context (optional), $injectionArray (optional)) $injectionArray = null; for ($i = 0; $i < $argNum; $i++) { if (is_array($argList[$i])) { //we have reached the injectionArray $injectionArray = $argList[$i]; //any array in the args will be the injection array } } // Find best translation $locale = i18n::get_locale(); $returnValue = static::with_translators(function (Zend_Translate_Adapter $adapter) use($entity, $locale) { // Return translation only if we found a match thats not the entity itself (Zend fallback) $translation = $adapter->translate($entity, $locale); if ($translation && $translation != $entity) { return $translation; } return null; }); // Fall back to default string argument if ($returnValue === null) { $returnValue = is_string($string) ? $string : ''; } // inject the variables from injectionArray (if present) if ($injectionArray) { $regex = '/\\{[\\w\\d]*\\}/i'; if (!preg_match($regex, $returnValue)) { // Legacy mode: If no injection placeholders are found, // replace sprintf placeholders in fixed order. // Fail silently in case the translation is outdated preg_match_all('/%[s,d]/', $returnValue, $returnValueArgs); if ($returnValueArgs) { foreach ($returnValueArgs[0] as $i => $returnValueArg) { if ($i >= count($injectionArray)) { $injectionArray[] = ''; } } } $replaced = vsprintf($returnValue, array_values($injectionArray)); if ($replaced) { $returnValue = $replaced; } } else { if (!ArrayLib::is_associative($injectionArray)) { // Legacy mode: If injection placeholders are found, // but parameters are passed without names, replace them in fixed order. $returnValue = preg_replace_callback($regex, function () use(&$injectionArray) { return $injectionArray ? array_shift($injectionArray) : ''; }, $returnValue); } else { // Standard placeholder replacement with named injections and variable order. foreach ($injectionArray as $variable => $injection) { $placeholder = '{' . $variable . '}'; $returnValue = str_replace($placeholder, $injection, $returnValue, $count); if (!$count) { Injector::inst()->get('Logger')->log('notice', sprintf("Couldn't find placeholder '%s' in translation string '%s' (id: '%s')", $placeholder, $returnValue, $entity)); } } } } } return $returnValue; }