Passed
Push — master ( 4f4697...c69a90 )
by Edward
04:40
created

NodeObjectValue   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 56
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A createEventGenerator() 0 13 2
A createEventIterator() 0 3 1
A createChildIterator() 0 3 1
A getPath() 0 3 1
A createChildGenerator() 0 6 2
A __construct() 0 8 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Remorhaz\JSON\Data\Value\DecodedJson;
5
6
use Generator;
7
use Iterator;
8
use Remorhaz\JSON\Data\Event\AfterObjectEvent;
9
use Remorhaz\JSON\Data\Event\BeforeObjectEvent;
10
use Remorhaz\JSON\Data\Event\PropertyEvent;
11
use Remorhaz\JSON\Data\Value\ObjectValueInterface;
12
use Remorhaz\JSON\Data\Path\PathInterface;
13
use Remorhaz\JSON\Data\Value\NodeValueInterface;
14
use stdClass;
15
16
final class NodeObjectValue implements NodeValueInterface, ObjectValueInterface
17
{
18
19
    private $data;
20
21
    private $path;
22
23
    private $valueFactory;
24
25 3
    public function __construct(
26
        stdClass $data,
27
        PathInterface $path,
28
        NodeValueFactoryInterface $valueFactory
29
    ) {
30 3
        $this->data = $data;
31 3
        $this->path = $path;
32 3
        $this->valueFactory = $valueFactory;
33 3
    }
34
35 3
    public function createChildIterator(): Iterator
36
    {
37 3
        return $this->createChildGenerator();
38
    }
39
40 3
    private function createChildGenerator(): Generator
41
    {
42 3
        foreach (get_object_vars($this->data) as $name => $property) {
43
            yield $name => $this
44 2
                ->valueFactory
45 2
                ->createValue($property, $this->path->copyWithProperty($name));
46
        }
47 3
    }
48
49 3
    public function createEventIterator(): Iterator
50
    {
51 3
        return $this->createEventGenerator($this->data, $this->path);
52
    }
53
54 3
    private function createEventGenerator(stdClass $data, PathInterface $path): Generator
55
    {
56 3
        yield new BeforeObjectEvent($this);
57
58 3
        foreach (get_object_vars($data) as $name => $property) {
59 2
            yield new PropertyEvent($name, $path);
60
            yield from $this
61 2
                ->valueFactory
62 2
                ->createValue($property, $path->copyWithProperty($name))
63 2
                ->createEventIterator();
64
        }
65
66 3
        yield new AfterObjectEvent($this);
67 3
    }
68
69 3
    public function getPath(): PathInterface
70
    {
71 3
        return $this->path;
72
    }
73
}
74