/** * Recursively set value of a dimension in a multi-dimensional array. Returns the resulting processed array. * * @param array $array Target array * @param string|array $dimension String, as an array dimension, in format of {level1}{level2}{levelX}, etc.; or Array, as an associative array of multilple assignments ( 'dimension1' => 'value1', 'dimension2' => 'value2' ) * @param scalar|array $value Any scalar or array value to be assigned, for single assignment * @param boolean $autolink [optional] If missing nodes should be auto-linked, otherwise, will throw an exception * @return array * * @throws \Exceptions\DimensionNotFoundException */ public static function RecursiveSet(array $array, $dimension, $value, $autolink = false) { // If this is a multiple assignment if (is_array($dimension)) { $result = $array; $keys = array_keys($dimension); foreach ($keys as $key) { $result = self::RecursiveSet($result, $key, $dimension[$key], $autolink); } return $result; } $nodes = self::GetDimensionNodes($dimension); $final_node = $nodes[sizeof($nodes) - 1]; $current = $array; $variable = '$current'; foreach ($nodes as $current_node) { $variable .= "['{$current_node}']"; if (!eval('return isset($variable);')) { if (!$autolink) { throw new \Exceptions\DimensionNotFoundException($array, $dimension); } // Link node if `autolink` is true eval("{$variable} = array();"); } // If this is the final node, set it, NOW if ($final_node == $current_node) { eval(str("{$variable} = {0};", Runtime::GetVirtual($value))); } } return eval(str("return {0};", substr("{$variable}", 0, strpos("{$variable}", "[")))); }
public function testGetVirtual_BooleanTest() { $this->assertEqual(Runtime::GetVirtual(true), 'true'); $this->assertEqual(Runtime::GetVirtual(false), 'false'); }