Passed
Push — master ( 7065f2...e55ca3 )
by Edward
03:45
created

NodeArrayValue   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
dl 0
loc 35
rs 10
c 1
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getPath() 0 3 1
A createChildIterator() 0 10 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Remorhaz\JSON\Data\Value\DecodedJson;
5
6
use Iterator;
7
use Remorhaz\JSON\Data\Value\ArrayValueInterface;
8
use Remorhaz\JSON\Data\Path\PathInterface;
9
use Remorhaz\JSON\Data\Value\NodeValueInterface;
10
11
final class NodeArrayValue implements NodeValueInterface, ArrayValueInterface
12
{
13
14
    private $data;
15
16
    private $path;
17
18
    private $valueFactory;
19
20
    public function __construct(
21
        array $data,
22
        PathInterface $path,
23
        NodeValueFactoryInterface $valueFactory
24
    ) {
25
        $this->data = $data;
26
        $this->path = $path;
27
        $this->valueFactory = $valueFactory;
28
    }
29
30
    public function createChildIterator(): Iterator
31
    {
32
        $validIndex = 0;
33
        foreach ($this->data as $index => $element) {
34
            if ($index !== $validIndex++) {
35
                throw new Exception\InvalidElementKeyException($index, $this->path);
36
            }
37
            yield $index => $this
38
                ->valueFactory
39
                ->createValue($element, $this->path->copyWithElement($index));
40
        }
41
    }
42
43
    public function getPath(): PathInterface
44
    {
45
        return $this->path;
46
    }
47
}
48