3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * @property string $pathSeparator * @property-read AddressableTree|null $rootNode */ class AddressableTree implements ArrayAccess, RecursiveIterator { /** * @var string */ private $pathSeparator; /** * @var AddressableTree */ private $rootNode; /** * @var array */ private $data = []; /** * @var self[] */ private $branches = []; /** * @param array $data * @param string $pathSeparator */ public function __construct(array $data = [], $pathSeparator = '/') { $this->setRootNode(null); $this->setPathSeparator($pathSeparator); foreach ($data as $key => $value) { $this->setElementAtKey($key, is_array($value) ? $this->createBranch($value) : $value, true); } } /** * @param string $name * @return mixed */ public function __get($name) { if (!in_array($name, ['pathSeparator', 'rootNode'])) { throw new \LogicException('Read of undefined property: ' . $name); } return $this->$name; } /** * @param string $name * @param mixed $value */ public function __set($name, $value) { if (!in_array($name, ['pathSeparator', 'rootNode'])) { throw new \LogicException('Write of undefined property: ' . $name); } call_user_func([$this, 'set' . $name], $value); } /** * @param string $value */ private function setPathSeparator($value) { $value = (string)$value; if ($this->rootNode && $value !== $this->rootNode->pathSeparator) { throw new \LogicException('Path separator can only be set on the root node'); } $this->pathSeparator = $value; foreach ($this->branches as $branch) { $branch->pathSeparator = $value; } } /** * @param AddressableTree|null $value */ private function setRootNode(AddressableTree $value = null) { $this->rootNode = $value; $branchRoot = $value ?: $this; foreach ($this->branches as $branch) { $branch->rootNode = $branchRoot; } } /** * @param array $value * @return AddressableTree */ private function createBranch(array $value = []) { $branch = new self($value, $this->pathSeparator); $branch->rootNode = $this->rootNode ?: $this; return $branch; } /** * @param $address * @return array|bool */ private function parseAddressParts($address) { $pos = strpos($address, $this->pathSeparator); if ($pos === 0) { $address = substr($address, strlen($this->pathSeparator)); $pos = strpos($address, $this->pathSeparator); } if ($pos === false) { return false; } return [substr($address, 0, $pos), substr($address, $pos + strlen($this->pathSeparator))]; } /** * @param string $key * @param mixed $value * @param bool $forceBranch */ private function setElementAtKey($key, $value, $forceBranch = false) { if ($value instanceof self) { if ($value->rootNode && !$forceBranch) { throw new \LogicException('Cannot add branch to tree: already attached to a tree'); } $value->rootNode = $this->rootNode ?: $this; $value->pathSeparator = $this->pathSeparator; if (isset($this->data[$key])) { $this->removeElementAtKey($key); } $this->data[$key] = $value; $this->branches[$key] = $value; } else { $this->data[$key] = $value; } } /** * @param string $key */ private function removeElementAtKey($key) { if (!array_key_exists($key, $this->data)) { throw new \LogicException("Cannot remove element at '$key': does not exist"); } if ($this->data[$key] instanceof self) { $this->data[$key]->rootNode = null; unset($this->branches[$key]); } unset($this->data[$key]); } /** * @param string $address * @param mixed $value */ public function setElementAtAddress($address, $value) { if (!$parts = $this->parseAddressParts($address)) { $this->setElementAtKey($address, $value); return; } list($key, $subAddress) = $parts; if (!array_key_exists($key, $this->data)) { $this->data[$key] = $this->branches[$key] = $this->createBranch(); } else if (!($this->data[$key] instanceof self)) { throw new \InvalidArgumentException("Target element address invalid: treats leaf '{$key}' as a branch"); } $this->branches[$key]->setElementAtAddress($subAddress, $value); } /** * @param string $address * @return mixed */ public function getElementAtAddress($address) { if (!$parts = $this->parseAddressParts($address)) { if (!array_key_exists($address, $this->data)) { throw new \InvalidArgumentException("Target leaf address invalid: element '{$address}' does not exist"); } return $this->data[$address]; } list($key, $subAddress) = $parts; if (!array_key_exists($key, $this->data)) { throw new \InvalidArgumentException("Target element address invalid: branch '{$key}' does not exist"); } else if (!($this->data[$key] instanceof self)) { throw new \InvalidArgumentException("Target element address invalid: treats leaf '{$key}' as a branch"); } return $this->branches[$key]->getElementAtAddress($subAddress); } /** * @param string $address */ public function removeElementAtAddress($address) { if (!$parts = $this->parseAddressParts($address)) { $this->removeElementAtKey($address); return; } list($key, $subAddress) = $parts; if (!array_key_exists($key, $this->data)) { throw new \InvalidArgumentException("Target element address invalid: branch '{$key}' does not exist"); } else if (!($this->data[$key] instanceof self)) { throw new \InvalidArgumentException("Target element address invalid: treats leaf '{$key}' as a branch"); } $this->branches[$key]->removeElementAtAddress($subAddress); } /** * @param string $address * @return bool */ public function addressExists($address) { if (!$parts = $this->parseAddressParts($address)) { return array_key_exists($address, $this->data); } list($key, $subAddress) = $parts; if (!array_key_exists($key, $this->data) || !($this->data[$key] instanceof self)) { return false; } return $this->branches[$key]->addressExists($subAddress); } /** * @return mixed */ public function current() { return current($this->data); } public function next() { next($this->data); } /** * @return string */ public function key() { return key($this->data); } /** * @return bool */ public function valid() { return key($this->data) !== null; } public function rewind() { reset($this->data); } /** * @return bool */ public function hasChildren() { return current($this->data) instanceof self; } /** * @return RecursiveIterator|null */ public function getChildren() { return current($this->data) instanceof self ? current($this->data) : null; } /** * @param string $address * @return bool */ public function offsetExists($address) { return $this->addressExists($address); } /** * @param string $address * @return mixed */ public function offsetGet($address) { return $this->getElementAtAddress($address); } /** * @param string $address * @param mixed $value */ public function offsetSet($address, $value) { $this->setElementAtAddress($address, $value); } /** * @param string $key */ public function offsetUnset($key) { $this->removeElementAtAddress($key); } } $tree = new AddressableTree([ 'foo' => [ 'bar' => 1, 'baz' => 2, ], 'yo' => ['mama' => ['so' => 'fat']], 'stuff' => 'ting', ]); var_dump($tree['foo/bar'], $tree['/foo/bar'], $tree['foo']['bar']); $tree['/yo/mama/so'] = 'ugly'; var_dump($tree);
Output for git.master, git.master_jit, rfc.property-hooks
Deprecated: Return type of AddressableTree::offsetExists($address) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/pb6ZQ on line 314 Deprecated: Return type of AddressableTree::offsetGet($address) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/pb6ZQ on line 323 Deprecated: Return type of AddressableTree::offsetSet($address, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/pb6ZQ on line 332 Deprecated: Return type of AddressableTree::offsetUnset($key) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/pb6ZQ on line 340 Deprecated: Return type of AddressableTree::hasChildren() should either be compatible with RecursiveIterator::hasChildren(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/pb6ZQ on line 297 Deprecated: Return type of AddressableTree::getChildren() should either be compatible with RecursiveIterator::getChildren(): ?RecursiveIterator, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/pb6ZQ on line 305 Deprecated: Return type of AddressableTree::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/pb6ZQ on line 263 Deprecated: Return type of AddressableTree::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/pb6ZQ on line 268 Deprecated: Return type of AddressableTree::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/pb6ZQ on line 276 Deprecated: Return type of AddressableTree::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/pb6ZQ on line 284 Deprecated: Return type of AddressableTree::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/pb6ZQ on line 289 int(1) int(1) int(1) object(AddressableTree)#1 (4) { ["pathSeparator":"AddressableTree":private]=> string(1) "/" ["rootNode":"AddressableTree":private]=> NULL ["data":"AddressableTree":private]=> array(3) { ["foo"]=> object(AddressableTree)#2 (4) { ["pathSeparator":"AddressableTree":private]=> string(1) "/" ["rootNode":"AddressableTree":private]=> *RECURSION* ["data":"AddressableTree":private]=> array(2) { ["bar"]=> int(1) ["baz"]=> int(2) } ["branches":"AddressableTree":private]=> array(0) { } } ["yo"]=> object(AddressableTree)#3 (4) { ["pathSeparator":"AddressableTree":private]=> string(1) "/" ["rootNode":"AddressableTree":private]=> *RECURSION* ["data":"AddressableTree":private]=> array(1) { ["mama"]=> object(AddressableTree)#4 (4) { ["pathSeparator":"AddressableTree":private]=> string(1) "/" ["rootNode":"AddressableTree":private]=> *RECURSION* ["data":"AddressableTree":private]=> array(1) { ["so"]=> string(4) "ugly" } ["branches":"AddressableTree":private]=> array(0) { } } } ["branches":"AddressableTree":private]=> array(1) { ["mama"]=> object(AddressableTree)#4 (4) { ["pathSeparator":"AddressableTree":private]=> string(1) "/" ["rootNode":"AddressableTree":private]=> *RECURSION* ["data":"AddressableTree":private]=> array(1) { ["so"]=> string(4) "ugly" } ["branches":"AddressableTree":private]=> array(0) { } } } } ["stuff"]=> string(4) "ting" } ["branches":"AddressableTree":private]=> array(2) { ["foo"]=> object(AddressableTree)#2 (4) { ["pathSeparator":"AddressableTree":private]=> string(1) "/" ["rootNode":"AddressableTree":private]=> *RECURSION* ["data":"AddressableTree":private]=> array(2) { ["bar"]=> int(1) ["baz"]=> int(2) } ["branches":"AddressableTree":private]=> array(0) { } } ["yo"]=> object(AddressableTree)#3 (4) { ["pathSeparator":"AddressableTree":private]=> string(1) "/" ["rootNode":"AddressableTree":private]=> *RECURSION* ["data":"AddressableTree":private]=> array(1) { ["mama"]=> object(AddressableTree)#4 (4) { ["pathSeparator":"AddressableTree":private]=> string(1) "/" ["rootNode":"AddressableTree":private]=> *RECURSION* ["data":"AddressableTree":private]=> array(1) { ["so"]=> string(4) "ugly" } ["branches":"AddressableTree":private]=> array(0) { } } } ["branches":"AddressableTree":private]=> array(1) { ["mama"]=> object(AddressableTree)#4 (4) { ["pathSeparator":"AddressableTree":private]=> string(1) "/" ["rootNode":"AddressableTree":private]=> *RECURSION* ["data":"AddressableTree":private]=> array(1) { ["so"]=> string(4) "ugly" } ["branches":"AddressableTree":private]=> array(0) { } } } } } }

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
38.79 ms | 417 KiB | 8 Q