NodeObjectValue::createChildGenerator()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Remorhaz\JSON\Data\Value\DecodedJson;
6
7
use Generator;
8
use Iterator;
9
use Remorhaz\JSON\Data\Value\ObjectValueInterface;
10
use Remorhaz\JSON\Data\Path\PathInterface;
11
use Remorhaz\JSON\Data\Value\NodeValueInterface;
12
use stdClass;
13
14
final class NodeObjectValue implements NodeValueInterface, ObjectValueInterface
15
{
16
17
    private $data;
18
19
    private $path;
20
21
    private $valueFactory;
22
23
    public function __construct(
24
        stdClass $data,
25
        PathInterface $path,
26
        NodeValueFactoryInterface $valueFactory
27
    ) {
28
        $this->data = $data;
29
        $this->path = $path;
30
        $this->valueFactory = $valueFactory;
31
    }
32
33
    public function createChildIterator(): Iterator
34
    {
35
        return $this->createChildGenerator();
36
    }
37
38
    private function createChildGenerator(): Generator
39
    {
40
        foreach (get_object_vars($this->data) as $name => $property) {
41
            $stringName = (string) $name;
42
            yield $stringName => $this
43
                ->valueFactory
44
                ->createValue($property, $this->path->copyWithProperty($stringName));
45
        }
46
    }
47
48
    public function getPath(): PathInterface
49
    {
50
        return $this->path;
51
    }
52
}
53