/** * Parse variable and replace it. This method is a simple template engine. * * Example: The {{ foo.bar.yoo }} will be replace to value of `$data['foo']['bar']['yoo']` * * @param string $string The template to replace. * @param array $data The data to find. * @param array $tags The variable tags. * * @return string Replaced template. */ public static function parseVariable($string, $data = array(), $tags = array('{{', '}}')) { return preg_replace_callback('/\\{\\{\\s*(.+?)\\s*\\}\\}/', function ($match) use($data) { $return = ArrayHelper::getByPath($data, $match[1]); if (is_array($return) || is_object($return)) { return print_r($return, 1); } else { return $return; } }, $string); }
/** * doRender * * @param string $name * @param XulEngine $engine * @param \SimpleXmlElement $element * @param mixed $data * * @throws \LogicException * @return mixed */ protected static function doRender($name, XulEngine $engine, \SimpleXmlElement $element, $data) { $itemsKey = XmlHelper::get($element, 'data', 'items'); $items = (array) ArrayHelper::getByPath($data, $itemsKey); $rows = new HtmlElements(); foreach ($items as $i => $item) { // Prepare data $item = new Data($item); $data->xulControl->currentItem = $item; // Prepare item for GridHelper $data->grid->setItem($item, $i); $rows[] = RowRenderer::render('row', $engine, $element, $data); } return $rows; }
/** * doRender * * @param string $name * @param \SimpleXmlElement $element * @param mixed $data * * @throws \LogicException * @return mixed */ protected static function doRender($name, XulEngine $engine, \SimpleXmlElement $element, $data) { $dataKey = XmlHelper::get($element, 'data'); $sidebar = $dataKey ? ArrayHelper::getByPath($data, $dataKey) : $data->sidebar; if (empty($sidebar)) { return ''; } $html = new HtmlElements(); $html[] = new HtmlElement('h4', JText::_(XmlHelper::get($element, 'title', 'JOPTION_MENUS'))); $html[] = $sidebar; $element->addChild('block', $html); if (!isset($data->view->colSpan)) { throw new \LogicException('Please put "sidebar" tag in "row" tag.'); } return parent::doRender($name, $engine, $element, $data); }
/** * Method to test repair(). * * @return void * * @covers \Windwalker\Helper\ArrayHelper::getByPath */ public function testGetByPath() { $object = new \stdClass(); $object->Alice = 'Julia'; $object->Johnny = array('David' => 123, 'Peter' => 'John'); $object->Vanessa = new \stdClass(); $object->Vanessa->Maria = 'Catherine'; $data = array('Jones' => array('Sakura' => 223), 'Arthur' => array('Lancelot' => array('Jessica' => $object, 'Rose' => array('Taylor' => 323)))); // Test null return $this->assertEquals(null, ArrayHelper::getByPath($data, '')); $this->assertEquals(null, ArrayHelper::getByPath($data, null)); // Test paths $this->assertEquals(223, ArrayHelper::getByPath($data, 'Jones.Sakura')); $this->assertEquals(223, ArrayHelper::getByPath($data, 'Jones..Sakura')); $this->assertEquals(array('Taylor' => 323), ArrayHelper::getByPath($data, 'Arthur.Lancelot.Rose')); $this->assertEquals(323, ArrayHelper::getByPath($data, 'Arthur.Lancelot.Rose.Taylor')); $this->assertEquals('Julia', ArrayHelper::getByPath($data, 'Arthur.Lancelot.Jessica.Alice')); $this->assertEquals(array('David' => 123, 'Peter' => 'John'), ArrayHelper::getByPath($data, 'Arthur.Lancelot.Jessica.Johnny')); $this->assertEquals(123, ArrayHelper::getByPath($data, 'Arthur.Lancelot.Jessica.Johnny.David')); $this->assertEquals('John', ArrayHelper::getByPath($data, 'Arthur.Lancelot.Jessica.Johnny.Peter')); }
/** * getArguments * * @param \SimpleXmlElement $element * @param mixed $data * * @return array */ protected static function getArguments($element, $data, $argumrntTag = 'argument') { $args = $element->xpath($argumrntTag); $return = array(); foreach ($args as $arg) { if (isset($arg['data'])) { $return[] = ArrayHelper::getByPath($data, (string) $arg['data']); } else { if (strtolower($arg) == 'null') { $arg = null; } if (strtolower($arg) == 'false') { $arg = false; } $return[] = String::parseVariable((string) $arg, $data); } } return $return; }
/** * Query a two-dimensional array values to get second level array. * * This is a clone from Windwalke RAD 2.1, will be remove if RAD 2.1 released. * * @param array $array An array to query. * @param mixed $queries Query strings, may contain Comparison Operators: '>', '>=', '<', '<='. * Example: * array( * 'id' => 6, // Get all elements where id=6 * '>published' => 0 // Get all elements where published>0 * ); * @param boolean $strict Use strict to compare equals. * @param boolean $keepKey Keep origin array keys. * * @return array An new two-dimensional array queried. * * @since 2.0 */ public static function query($array, $queries = array(), $strict = false, $keepKey = false) { $results = array(); $queries = (array) $queries; // Visit Array foreach ((array) $array as $k => $v) { $data = (array) $v; /* * Key: is query key * Val: is query value * Data: is array element */ $boolean = array(); // Visit Query Rules foreach ($queries as $key => $val) { if (substr($key, -2) == '>=') { $boolean[] = ArrayHelper::getByPath($data, trim(substr($key, 0, -2))) >= $val; } elseif (substr($key, -2) == '<=') { $boolean[] = ArrayHelper::getByPath($data, trim(substr($key, 0, -2))) <= $val; } elseif (substr($key, -1) == '>') { $boolean[] = ArrayHelper::getByPath($data, trim(substr($key, 0, -1))) > $val; } elseif (substr($key, -1) == '<') { $boolean[] = ArrayHelper::getByPath($data, trim(substr($key, 0, -1))) < $val; } else { if ($strict) { $boolean[] = ArrayHelper::getByPath($data, $key) === $val; } else { // Workaround for PHP 5.4 object compare bug, see: https://bugs.php.net/bug.php?id=62976 $compare1 = is_object(ArrayHelper::getByPath($data, $key)) ? get_object_vars(ArrayHelper::getByPath($data, $key)) : ArrayHelper::getByPath($data, $key); $compare2 = is_object($val) ? get_object_vars($val) : $val; $boolean[] = $compare1 == $compare2; } } } // Set Query results if (!in_array(false, $boolean, true)) { if ($keepKey) { $results[$k] = $v; } else { $results[] = $v; } } } return $results; }