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

NodeArrayValue   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 70.59%

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 40
ccs 12
cts 17
cp 0.7059
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A createChildGenerator() 0 10 3
A getPath() 0 3 1
A createChildIterator() 0 3 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\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