/** * Loop through template sections and organize them into a hierarchy * and assign addition meta data to any tags * @param array $array Array of template segments * @return array */ protected function _tree($array) { $root = array('children' => array()); $current =& $root; foreach ($array as $i => $node) { $result = $this->_tag($node); if ($result) { if (isset($result['tag'])) { $tag = $result['tag']; } else { $tag = ""; } if (isset($result['arguments'])) { $arguments = $result['arguments']; } else { $arguments = ""; } if ($tag) { // If segment does not contain a closer if (!$result['closer']) { // clean up syntax if segment is isolated and // preceded by an plain text segment $last = ArrayMethods::last($current['children']); if ($result['isolated'] && is_string($last)) { array_pop($current['children']); } $current['children'][] = array('index' => $i, 'parent' => &$current, 'children' => array(), 'raw' => $result['source'], 'tag' => $tag, 'arguments' => $arguments, 'delimiter' => $result['delimiter'], 'number' => count($current['children'])); $current =& $current['children'][count($current['children']) - 1]; } else { if (isset($current['tag']) && $result['tag'] == $current['tag']) { $start = $current['index'] + 1; $length = $i - $start; $current['source'] = implode(array_slice($array, $start, $length)); $current =& $current['parent']; } } } else { $current['children'][] = array('index' => $i, 'parent' => &$current, 'children' => array(), 'raw' => $result['source'], 'tag' => $tag, 'arguments' => $arguments, 'delimiter' => $result['delimiter'], 'number' => count($current['children'])); } } else { $current['children'][] = $node; } } return $root; }
/** * Loops through the array of template segments, generated by the _array() method, * and organizes them into a hierarchical structure. Plain text nodes are simply assigned as-is to the tree, * while additional metadata is generated and assigned with the tags. * * @param type $array * @return array */ protected function _tree($array) { $root = array("children" => array()); $current =& $root; foreach ($array as $i => $node) { $result = $this->_tag($node); if ($result) { $tag = isset($result["tag"]) ? $result["tag"] : ""; $arguments = isset($result["arguments"]) ? $result["arguments"] : ""; if ($tag) { if (!$result["closer"]) { $last = ArrayMethods::last($current["children"]); if ($result["isolated"] && is_string($last)) { array_pop($current["children"]); } $current["children"][] = array("index" => $i, "parent" => &$current, "children" => array(), "raw" => $result["source"], "tag" => $tag, "arguments" => $arguments, "delimiter" => $result["delimiter"], "number" => sizeof($current["children"])); $current =& $current["children"][sizeof($current["children"]) - 1]; } else { if (isset($current["tag"]) && $result["tag"] == $current["tag"]) { $start = $current["index"] + 1; $length = $i - $start; $current["source"] = implode(array_slice($array, $start, $length)); $current =& $current["parent"]; } } } else { $current["children"][] = array("index" => $i, "parent" => &$current, "children" => array(), "raw" => $result["source"], "tag" => $tag, "arguments" => $arguments, "delimiter" => $result["delimiter"], "number" => sizeof($current["children"])); } } else { $current["children"][] = $node; } } return $root; }