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) { if (false === $pos = strpos($address, $this->pathSeparator)) { return false; } else if (!$pos && !$pos = strpos($address, $this->pathSeparator, strlen($this->pathSeparator))) { var_dump($address, $this->pathSeparator, $pos); 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, ], 'qux' => [ '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 8.1.0 - 8.1.27, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
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/Ei13J on line 310 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/Ei13J on line 319 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/Ei13J on line 328 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/Ei13J on line 336 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/Ei13J on line 293 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/Ei13J on line 301 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/Ei13J on line 259 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/Ei13J on line 264 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/Ei13J on line 272 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/Ei13J on line 280 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/Ei13J on line 285 Fatal error: Uncaught InvalidArgumentException: Target element address invalid: branch '/foo' does not exist in /in/Ei13J:208 Stack trace: #0 /in/Ei13J(321): AddressableTree->getElementAtAddress('/foo/bar') #1 /in/Ei13J(353): AddressableTree->offsetGet('/foo/bar') #2 {main} thrown in /in/Ei13J on line 208
Process exited with code 255.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 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/Ei13J on line 310 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/Ei13J on line 319 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/Ei13J on line 328 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/Ei13J on line 336 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/Ei13J on line 293 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/Ei13J on line 301 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/Ei13J on line 259 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/Ei13J on line 264 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/Ei13J on line 272 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/Ei13J on line 280 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/Ei13J on line 285 Fatal error: Uncaught InvalidArgumentException: Target element address invalid: branch '/foo' does not exist in /in/Ei13J:208 Stack trace: #0 /in/Ei13J(321): AddressableTree->getElementAtAddress('/foo/bar') #1 /in/Ei13J(353): AddressableTree->offsetGet('/foo/bar') #2 {main} thrown in /in/Ei13J on line 208
Process exited with code 255.
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
Fatal error: Uncaught InvalidArgumentException: Target element address invalid: branch '/foo' does not exist in /in/Ei13J:208 Stack trace: #0 /in/Ei13J(321): AddressableTree->getElementAtAddress('/foo/bar') #1 /in/Ei13J(353): AddressableTree->offsetGet('/foo/bar') #2 {main} thrown in /in/Ei13J on line 208
Process exited with code 255.
Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.28
Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Target element address invalid: branch '/foo' does not exist' in /in/Ei13J:208 Stack trace: #0 /in/Ei13J(321): AddressableTree->getElementAtAddress('/foo/bar') #1 /in/Ei13J(353): AddressableTree->offsetGet('/foo/bar') #2 {main} thrown in /in/Ei13J on line 208
Process exited with code 255.

preferences:
230.85 ms | 402 KiB | 301 Q