1: <?php declare(strict_types=1);
2:
3: namespace Salient\Iterator;
4:
5: use Salient\Iterator\Concern\RecursiveGraphIteratorTrait;
6: use RecursiveIterator;
7:
8: /**
9: * Iterates over the properties of objects and the elements of arrays,
10: * descending into them recursively while allowing the current element to be
11: * replaced
12: *
13: * @api
14: *
15: * @implements RecursiveIterator<array-key,mixed>
16: */
17: class RecursiveMutableGraphIterator extends MutableGraphIterator implements RecursiveIterator
18: {
19: use RecursiveGraphIteratorTrait;
20:
21: /**
22: * If the current element is an object with children, replace it with an
23: * array of its properties
24: *
25: * @return $this
26: */
27: public function maybeConvertToArray()
28: {
29: $current = $this->current();
30: if ($current === false) {
31: // @codeCoverageIgnoreStart
32: return $this;
33: // @codeCoverageIgnoreEnd
34: }
35:
36: if (is_object($current) && $this->hasChildren()) {
37: $array = [];
38: // @phpstan-ignore-next-line
39: foreach ($current as $key => $value) {
40: $array[$key] = $value;
41: }
42:
43: return $this->replace($array);
44: }
45:
46: return $this;
47: }
48: }
49: