public function testWalkRecursive() { $expected = $actual = array('one' => array('two' => array('three' => array('four' => 'eight', 'twelve' => 'thirteen'), 'five' => 'nine'), 'six' => 'ten'), 'seven' => 'eleven'); ArrayUtil::walkRecursive($actual, function (&$value, $key, &$array) { if ('four' === $key) { unset($array[$key]); $array['changed'] = $value; } }); unset($expected['one']['two']['three']['four']); $expected['one']['two']['three']['changed'] = 'eight'; $this->assertSame($expected, $actual); }
/** * Loads the configuration data from a resource and returns it flattened. * * @param mixed $resource A resource. * @param string $type The resource type. * @param boolean $require Require processing? * * @return array The data. */ public function loadFlat($resource, $type = null, $require = false) { return ArrayUtil::flatten($this->load($resource, $type, $require)); }
/** * Imports other configuration files and resolves references. * * @param array $data The data. * @param string $file The file source. * * @return array The processed data. * * @throws ImportException If "imports" is invalid. * @throws InvalidReferenceException If an invalid reference is used. */ public function process($data, $file) { if (empty($data)) { return array(); } if (isset($data['imports'])) { if (false === is_array($data['imports'])) { throw ImportException::format('The "imports" value is not valid in "%s".', $file); } $dir = dirname($file); foreach ($data['imports'] as $i => $import) { if (false === is_array($import)) { throw ImportException::format('One of the "imports" values (#%d) is not valid in "%s".', $i, $file); } if (false === isset($import['resource'])) { throw ImportException::format('A resource was not defined for an import in "%s".', $file); } $this->setCurrentDir($dir); $data = array_replace_recursive($this->import($import['resource'], null, isset($import['ignore_errors']) ? (bool) $import['ignore_errors'] : false), $data); } } $global = $this->wise ? $this->wise->getGlobalParameters() : array(); $_this = $this; ArrayUtil::walkRecursive($data, function (&$value, $key, &$array) use(&$data, $global, $_this) { $value = $_this->doReplace($value, $data, $global); if (false !== strpos($key, '%')) { unset($array[$key]); $key = $_this->doReplace($key, $data, $global); $array[$key] = $value; } }); return $data; }