Completed
Push — master ( cb0ced...7754cf )
by Edward
04:45
created

LiteralArrayValue::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Remorhaz\JSON\Path\Value;
5
6
use ArrayIterator;
7
use Generator;
8
use Iterator;
9
use Remorhaz\JSON\Data\Event;
10
use Remorhaz\JSON\Data\Value\ArrayValueInterface;
11
use Remorhaz\JSON\Data\Path\Path;
12
use Remorhaz\JSON\Data\Value\ValueInterface;
13
14
final class LiteralArrayValue implements ArrayValueInterface, LiteralValueInterface
15
{
16
17
    private $indexMap;
18
19
    private $values;
20
21
    public function __construct(IndexMapInterface $indexMap, ValueInterface ...$values)
22
    {
23
        $this->indexMap = $indexMap;
24
        $this->values = $values;
25
    }
26
27
    public function createChildIterator(): Iterator
28
    {
29
        return new ArrayIterator($this->values);
30
    }
31
32
    public function createEventIterator(): Iterator
33
    {
34
        return $this->createEventGenerator();
35
    }
36
37
    private function createEventGenerator(): Generator
38
    {
39
        yield new Event\BeforeArrayEvent($this);
40
41
        foreach ($this->values as $index => $value) {
42
            yield new Event\ElementEvent($index, new Path);
43
            yield from $value->createEventIterator();
44
        }
45
        yield new Event\AfterArrayEvent($this);
46
    }
47
}
48