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

NodeArrayValue::createChildGenerator()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 10
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A NodeArrayValue::getPath() 0 3 1
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