3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Provides a flat list of all the nodes * Eases with modifying the tree inheritance by id */ class NodeList { public $length = 0; /** * @param mixed $index * @return Node|null */ public function item($index) { $tmp = (array) $this; $this->length = count($tmp) - 1; if (false === isset($this->$index)) { return null; } return $this->$index; } } /** * Establish and maintain the tree view of each node/child */ class Tree extends NodeList { /** * Requires id, parent from record set * @param null|array|object $recordSet */ public function __construct($recordSet = null) { if (null !== $recordSet) { $this->setChildren($recordSet); } } /** * @param array|object $recordSet */ private function setChildren($recordSet) { foreach ($recordSet as $record) { if (true === is_array($record)) { $record = (object) $record; } if (true === isset($record->id)) { $this->length++; $this->appendNode($record->id, $record); } } foreach ($this as $node) { if (false === $node instanceof Node) { continue; } if (false === empty($node->parent) && true === isset($this->{$node->parent})) { $children = &$this->{$node->parent}->children; $children->length++; $children->{$node->id} = &$this->{$node->id}; $this->item($node->id); } } } /** * @param string $id * @param null|array|object $data * @return mixed */ public function appendNode($id, $data = null) { $this->$id = new Node($data); return $this->item($id); } /** * @param string $id * @return \Node */ public function getNode($id) { $item = $this->item($id); if (true === empty($item)) { $this->appendNode($id, null); } return $item; } /** * @param string $id * @return \Node|null */ public function getParent($id) { if (null === $this->getNode($id)->parent) { return null; } return $this->getNode($this->getNode($id)->parent); } /** * @param string $id * @return int */ public function getDepth($id) { $i = 0; $item = $this->item($id); if (null !== $item && null !== $item->parent) { $i = $this->getDepth($item->parent) + 1; } $item->depth = $i; return $item->depth; } /** * @param string $id */ public function removeNode($id) { $this->removeChildren($id); if (null !== $this->item(id)) { $parent = false; if ($this->item($id)->parent) { $parent = $this->getParent($id); } $this->$id = null; if ($parent && $parent->children) { unset($parent->children->$id); $parent->children->length--; } } } } /** * Single Node * This is an overloaded class */ class Node { public $id; public $name; public $parent; public $depth; public $children; /** * @param array|object $data */ public function __construct($data) { $this->children = new NodeList(); if (null === $data) { return; } foreach ($data as $key => $value) { /* I escaped these values since they are redundant to this script */ switch ($key) { case 'children': case 'depth': case 'childrenCount': continue 2; } $this->$key = $value; } } /** * @return int */ public function countChildren() { return $this->children->length; } /** * @return \NodeList */ public function getChildren() { return $this->children; } public function removeChildren() { if (null !== $this->getChildren()) { foreach ($this->children as $child) { if (true === isset($child->children)) { $child->removeChildren(); } $child = null; } } $this->children = null; return $this; } } $recordSet = array( array( 'id' => 1, 'name' => 'somename1', 'parent' => null, 'childrenCount' => 0, 'children' => 0, 'foo' => 'foo', ), array( 'id' => 53, 'name' => 'somename2', 'parent' => 1, 'childrenCount' => 0, 'children' => 0, 'bar' => 'bar', ), array( 'id' => 921, 'name' => 'somename3', 'parent' => 53, 'childrenCount' => 0, 'children' => 0, 'baz' => 'baz', ), ); $TreeObject = new Tree($recordSet); echo 'Display the entire tree (This is a fully recursive tree that can be looked up at any point)' . PHP_EOL; print_r($TreeObject); echo PHP_EOL; echo 'Get the middle node id 53' . PHP_EOL; print_r($TreeObject->getNode('53')); echo PHP_EOL . PHP_EOL; echo 'Get the child count of node id 53: ' . $TreeObject->getNode('53')->countChildren(); echo PHP_EOL . PHP_EOL; echo 'Get the children of node 1: ' . PHP_EOL; print_r($TreeObject->getNode('1')->getChildren()); echo PHP_EOL; echo 'Get the depth of the tree node id 921: ' . $TreeObject->getDepth('921'); echo PHP_EOL . PHP_EOL; echo 'Get the depth of the tree node id 53: ' . $TreeObject->getDepth('53'); echo PHP_EOL . PHP_EOL; echo 'Remove the children of node id 53:' . PHP_EOL; print_r($TreeObject->getNode('53')->removeChildren()); echo PHP_EOL; echo 'Display the list of nodes after removing the children of node id 53:' . PHP_EOL; print_r($TreeObject);

Abusive script

This script was stopped while abusing our resources


preferences:
25.46 ms | 404 KiB | 5 Q