tree = $tree; } public function parse() { $lines = explode(PHP_EOL, $this->tree); $this->setStructureAndOrderedNodes($lines); $root = $this->structure[TreeNode::ROOT_LEVEL][TreeNode::ROOT_ORDER]; foreach ($this->orderedNodes as $node) { if ($node->isRoot) { continue; } $parent = $this->getParentForNode($node); $parent->children[] = $node; $node->parent = $parent; } return $root; } public function setTree($tree) { $this->tree = $tree; } public function getTree() { return $this->tree; } /** * @return array */ public function getOrderedNodes() { return $this->orderedNodes; } /** * @return array */ public function getStructure() { return $this->structure; } /** * @param int $indentation * @return TreeParser */ public function setIndentation($indentation) { $this->indentation = $indentation; return $this; } /** * @param TreeNode $node * @return TreeNode|null */ public function getParentForNode(TreeNode $node) { if ($node->level === TreeNode::ROOT_LEVEL) { return null; } $parentLevel = $node->level - 1; $candidates = $this->structure[$parentLevel]; $order = $node->order - 1; while ($order >= TreeNode::ROOT_ORDER) { if (isset($candidates[$order])) { return $candidates[$order]; } --$order; } } /** * @param $line * @return int */ private function numberOfSpaces($line) { return strspn($line, ' '); } /** * @param $numberOfSpaces * @return int * @throws InvalidNumberOfSpaces */ private function spacesToLevel($numberOfSpaces) { if ($numberOfSpaces === $this->initialSpaces) { return 0; } if (($numberOfSpaces - $this->initialSpaces) % $this->indentation !== 0) { throw new InvalidNumberOfSpaces( "Make sure children's leading spaces being a multiple of {$this->indentation}" ); } return (int) ($numberOfSpaces - $this->initialSpaces) / $this->indentation; } private function lineToNode($line) { $line = trim($line); $line = ltrim($line, '|-'); $line = trim($line); $node = new TreeNode(); $node->name = $line; return $node; } private function setStructureAndOrderedNodes(array $lines) { $this->orderedNodes = []; $this->initialSpaces = null; $this->structure = []; foreach ($lines as $order => $line) { $node = $this->lineToNode($line); if ($this->initialSpaces === null) { $this->initialSpaces = $this->numberOfSpaces($line); $node->isRoot = true; } $node->order = (int) $order; $node->level = $this->spacesToLevel($this->numberOfSpaces($line)); $this->structure[$node->level][$node->order] = $node; $this->orderedNodes[] = $node; } } } __halt_compiler();----SIGNATURE:----yBYv2g5+NUUMLi9gcgPbrys1mol7ly8n94n3543s5DEOmHLbi/hoYqIwSAmMZ/QJt9otA2WDQAPwV4XA4LHbWbvZXkRziKlbZkcigsRJrt8IGB8+fZU5sXt3LoxPc4mLxbBhBxACJJZ02HjI8mKH/q8CuObkDIBxcKGHT0qrEoA8pH+W/dMxE6ehMmnSvmc6xpLXrg/OxGZ9FGnE9FcHkVe3UBOYcq4f+2sDTMFGCVFg0JhlKp04qY2kyH+evAOCmxFBzmQTVZyc6lycPRiEIocCepkzDS1gVP6/eYDk5/iGi/Xav4hUPVgcb/1IaXO8Gqe4n4Tgw95Ztnv0bbYpAaFbiKflqPKTR/lQv3odfcVVZ9AjpHT4ZMG+jKJOSCS2+6/sLnfoNkppEVGU/I4R4MkB6Oo/hOZX70iGo/NLdhLH64mwd5A/2JyQOXIttfcqAMzR41pA1TbFvYwnpfKSHVx0TFb6LXJDKChrE3W3Qjfd6/4LaYTUKiASDNygMxpsPONGXprUEaBEpTg21S6aa491MjQRkoJVCuTOQnZgSOScXA6Y5xDMcST0BN3ferpuBHVJC4y9J7FSce/gxVFKfg9h1qKoPzpU0btXqrq+2Tm7t9bgdiX/a6RZ8bHvhIiOyeyjik6b4gNZ1A9KLi4rQPK4jNmn0c9JNc1WCAyQhSY=----ATTACHMENT:----NTIwNDgyMzQ5NDEzNjI0NyA0OTc0NTg3NDMwNjM2MzE5IDQ4MTk3NDAxOTE0NDY3NjY=