/** * @covers \Magento\Framework\Stdlib\ArrayUtils::decorateArray */ public function testDecorateArray() { $original = [['value' => 1], ['value' => 2], ['value' => 3]]; $decorated = [ ['value' => 1, 'is_first' => true, 'is_odd' => true], ['value' => 2, 'is_even' => true], ['value' => 3, 'is_last' => true, 'is_odd' => true], ]; // arrays $this->assertEquals($decorated, $this->_arrayUtils->decorateArray($original, '')); // \Magento\Framework\Object $sample = [ new \Magento\Framework\Object($original[0]), new \Magento\Framework\Object($original[1]), new \Magento\Framework\Object($original[2]), ]; $decoratedVo = [ new \Magento\Framework\Object($decorated[0]), new \Magento\Framework\Object($decorated[1]), new \Magento\Framework\Object($decorated[2]), ]; $this->assertEquals($decoratedVo, $this->_arrayUtils->decorateArray($sample, '')); }
/** * @covers \Magento\Framework\Stdlib\ArrayUtils::decorateArray */ public function testDecorateArray() { $original = array(array('value' => 1), array('value' => 2), array('value' => 3)); $decorated = array(array('value' => 1, 'is_first' => true, 'is_odd' => true), array('value' => 2, 'is_even' => true), array('value' => 3, 'is_last' => true, 'is_odd' => true)); // arrays $this->assertEquals($decorated, $this->_arrayUtils->decorateArray($original, '')); // \Magento\Framework\Object $sample = array(new \Magento\Framework\Object($original[0]), new \Magento\Framework\Object($original[1]), new \Magento\Framework\Object($original[2])); $decoratedVo = array(new \Magento\Framework\Object($decorated[0]), new \Magento\Framework\Object($decorated[1]), new \Magento\Framework\Object($decorated[2])); foreach ($decoratedVo as $obj) { $obj->setDataChanges(true); // hack for assertion } $this->assertEquals($decoratedVo, $this->_arrayUtils->decorateArray($sample, '')); }
/** * Convert collection items to select options array * * @param string|boolean $emptyLabel * @return array */ public function toOptionArray($emptyLabel = ' ') { $options = $this->_toOptionArray('country_id', 'name', ['title' => 'iso2_code']); $sort = []; foreach ($options as $data) { $name = (string) $this->_localeLists->getCountryTranslation($data['value']); if (!empty($name)) { $sort[$name] = $data['value']; } } $this->_arrayUtils->ksortMultibyte($sort, $this->_localeResolver->getLocale()); foreach (array_reverse($this->_foregroundCountries) as $foregroundCountry) { $name = array_search($foregroundCountry, $sort); unset($sort[$name]); $sort = [$name => $foregroundCountry] + $sort; } $options = []; foreach ($sort as $label => $value) { $option = ['value' => $value, 'label' => $label]; if ($this->helperData->isRegionRequired($value)) { $option['is_region_required'] = true; } $options[] = $option; } if (count($options) > 0 && $emptyLabel !== false) { array_unshift($options, ['value' => '', 'label' => $emptyLabel]); } return $options; }
/** * Convert collection items to select options array * * @param string|boolean $emptyLabel * @return array */ public function toOptionArray($emptyLabel = ' ') { $options = $this->_toOptionArray('country_id', 'name', array('title' => 'iso2_code')); $sort = array(); foreach ($options as $data) { $name = (string) $this->_localeLists->getCountryTranslation($data['value']); if (!empty($name)) { $sort[$name] = $data['value']; } } $this->_arrayUtils->ksortMultibyte($sort, $this->_localeResolver->getLocaleCode()); foreach (array_reverse($this->_foregroundCountries) as $foregroundCountry) { $name = array_search($foregroundCountry, $sort); unset($sort[$name]); $sort = array($name => $foregroundCountry) + $sort; } $options = array(); foreach ($sort as $label => $value) { $options[] = array('value' => $value, 'label' => $label); } if (count($options) > 0 && $emptyLabel !== false) { array_unshift($options, array('value' => '', 'label' => $emptyLabel)); } return $options; }
/** * Decorate a plain array of arrays or objects * * @param array $array * @param string $prefix * @param bool $forceSetAll * @return array */ public function decorateArray($array, $prefix = 'decorated_', $forceSetAll = false) { return $this->arrayUtils->decorateArray($array, $prefix, $forceSetAll); }