1: <?php declare(strict_types=1);
2:
3: namespace Salient\Iterator;
4:
5: use Salient\Iterator\Contract\MutableIterator;
6: use LogicException;
7:
8: /**
9: * Iterates over the properties of an object or the elements of an array while
10: * allowing the current element to be replaced
11: *
12: * @api
13: *
14: * @implements MutableIterator<array-key,mixed>
15: */
16: class MutableGraphIterator extends GraphIterator implements MutableIterator
17: {
18: /**
19: * @param object|mixed[] $graph
20: */
21: public function __construct(&$graph)
22: {
23: $this->doConstruct($graph);
24: }
25:
26: /**
27: * @inheritDoc
28: */
29: public function replace($value)
30: {
31: $key = current($this->Keys);
32: if ($key === false) {
33: // @codeCoverageIgnoreStart
34: throw new LogicException('Current position is not valid');
35: // @codeCoverageIgnoreEnd
36: }
37:
38: if ($this->IsObject) {
39: $this->Graph->{$key} = $value;
40: return $this;
41: }
42:
43: // @phpstan-ignore-next-line
44: $this->Graph[$key] = $value;
45: return $this;
46: }
47: }
48: