NodeArrayValue::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 8
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Remorhaz\JSON\Data\Value\DecodedJson;
6
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
    public function __construct(
22
        array $data,
23
        PathInterface $path,
24
        NodeValueFactoryInterface $valueFactory
25
    ) {
26
        $this->data = $data;
27
        $this->path = $path;
28
        $this->valueFactory = $valueFactory;
29
    }
30
31
    public function createChildIterator(): Iterator
32
    {
33
        $validIndex = 0;
34
        foreach ($this->data as $index => $element) {
35
            if ($index !== $validIndex++) {
36
                throw new Exception\InvalidElementKeyException($index, $this->path);
37
            }
38
            yield $index => $this
39
                ->valueFactory
40
                ->createValue($element, $this->path->copyWithElement($index));
41
        }
42
    }
43
44
    public function getPath(): PathInterface
45
    {
46
        return $this->path;
47
    }
48
}
49