<?php
class XmlArrayAccessIterator implements ArrayAccess, IteratorAggregate
{
private $xml;
private $current;
public function __construct($filePath, $isUrl = true)
{
$this->isUrl = $isUrl;
$this->xml = new SimpleXmlIterator($filePath, 0, $isUrl);
}
public function offsetExists($offset)
{
return !is_null($this->xml->$offset);
}
public function offsetGet($offset)
{
if ($this->xml->getName() == $offset) {
return $this;
}
$this->current = $this->xml->$offset;
return new self($this->xml->$offset->asXML(), false);
}
public function __toString()
{
return $this->current;
}
public function offsetSet($offset, $value)
{
throw new RuntimeException('This is readonly. writing to "' . (string) $offset . '" with value "' . (string) $value . '" not possible');
}
public function offsetUnset($offset)
{
throw new RuntimeException('This is readonly. Unsetting "' . (string) $offset . '" not possible');
}
public function getIterator()
{
return $this->xml;
}
}
$str = '<?xml version="1.0"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don\'t forget me this weekend!</body>
<recursive>
<val>2</val>
<val>4</val>
</recursive>
</note>';
$xml = new XmlArrayAccessIterator($str, false);
echo $xml['note']['to'];
//var_dump((string)$xml['note']['recursive']);
Deprecated: Return type of XmlArrayAccessIterator::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/bbQkT on line 14
Deprecated: Return type of XmlArrayAccessIterator::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/bbQkT on line 19
Deprecated: Return type of XmlArrayAccessIterator::offsetSet($offset, $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/bbQkT on line 34
Deprecated: Return type of XmlArrayAccessIterator::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/bbQkT on line 39
Deprecated: Return type of XmlArrayAccessIterator::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/bbQkT on line 44
Deprecated: Creation of dynamic property XmlArrayAccessIterator::$isUrl is deprecated in /in/bbQkT on line 10
Deprecated: Creation of dynamic property XmlArrayAccessIterator::$isUrl is deprecated in /in/bbQkT on line 10
Fatal error: Uncaught TypeError: XmlArrayAccessIterator::__toString(): Return value must be of type string, null returned in /in/bbQkT:31
Stack trace:
#0 /in/bbQkT(67): XmlArrayAccessIterator->__toString()
#1 {main}
thrown in /in/bbQkT on line 31
Process exited with code 255.
Output for 8.1.0 - 8.1.28
Deprecated: Return type of XmlArrayAccessIterator::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/bbQkT on line 14
Deprecated: Return type of XmlArrayAccessIterator::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/bbQkT on line 19
Deprecated: Return type of XmlArrayAccessIterator::offsetSet($offset, $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/bbQkT on line 34
Deprecated: Return type of XmlArrayAccessIterator::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/bbQkT on line 39
Deprecated: Return type of XmlArrayAccessIterator::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/bbQkT on line 44
Fatal error: Uncaught TypeError: XmlArrayAccessIterator::__toString(): Return value must be of type string, null returned in /in/bbQkT:31
Stack trace:
#0 /in/bbQkT(67): XmlArrayAccessIterator->__toString()
#1 {main}
thrown in /in/bbQkT on line 31
Process exited with code 255.
Output for 8.0.0 - 8.0.30
Fatal error: Uncaught TypeError: XmlArrayAccessIterator::__toString(): Return value must be of type string, null returned in /in/bbQkT:31
Stack trace:
#0 /in/bbQkT(67): XmlArrayAccessIterator->__toString()
#1 {main}
thrown in /in/bbQkT on line 31
Process exited with code 255.
Output for 7.4.0 - 7.4.33
Fatal error: Uncaught Error: Method XmlArrayAccessIterator::__toString() must return a string value in /in/bbQkT:67
Stack trace:
#0 {main}
thrown in /in/bbQkT on line 67
Process exited with code 255.