Passed
Push — master ( c69a90...df5d45 )
by Edward
04:44
created

NodeArrayValue::createEventGenerator()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 2
dl 0
loc 17
ccs 11
cts 11
cp 1
crap 3
rs 9.9
c 0
b 0
f 0
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\Value\ArrayValueInterface;
9
use Remorhaz\JSON\Data\Path\PathInterface;
10
use Remorhaz\JSON\Data\Value\NodeValueInterface;
11
12
final class NodeArrayValue implements NodeValueInterface, ArrayValueInterface
13
{
14
15
    private $data;
16
17
    private $path;
18
19
    private $valueFactory;
20
21 2
    public function __construct(
22
        array $data,
23
        PathInterface $path,
24
        NodeValueFactoryInterface $valueFactory
25
    ) {
26 2
        $this->data = $data;
27 2
        $this->path = $path;
28 2
        $this->valueFactory = $valueFactory;
29 2
    }
30
31 2
    public function createChildIterator(): Iterator
32
    {
33 2
        return $this->createChildGenerator();
34
    }
35
36 2
    private function createChildGenerator(): Generator
37
    {
38 2
        $validIndex = 0;
39 2
        foreach ($this->data as $index => $element) {
40 2
            if ($index !== $validIndex++) {
41 2
                throw new Exception\InvalidElementKeyException($index, $this->path);
42
            }
43
            yield $index => $this
44
                ->valueFactory
45
                ->createValue($element, $this->path->copyWithElement($index));
46
        }
47
    }
48
49
    public function getPath(): PathInterface
50
    {
51
        return $this->path;
52
    }
53
}
54