Passed
Push — master ( 4c267c...1bfd55 )
by Pierrick
10:36
created

ArrayNode   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 75%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 17
dl 0
loc 47
ccs 15
cts 20
cp 0.75
rs 10
c 2
b 0
f 1
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getIterator() 0 3 1
A resolve() 0 6 3
A count() 0 3 1
A getNativeValue() 0 7 2
A __construct() 0 4 2
A add() 0 8 2
1
<?php
2
/**
3
 * This file is part of NACL.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @copyright 2019 Nuglif (2018) Inc.
9
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
10
 * @author    Pierrick Charron <[email protected]>
11
 * @author    Charle Demers <[email protected]>
12
 */
13
14
declare(strict_types=1);
15
16
namespace Nuglif\Nacl;
17
18
/**
19
 * @template-implements \IteratorAggregate<mixed>
20
 */
21
class ArrayNode extends Node implements \IteratorAggregate, \Countable
22
{
23
    private array $value = [];
24
    private bool $isNative = true;
25
26 343
    public function __construct(array $defaultValues = [])
27
    {
28 343
        foreach ($defaultValues as $v) {
29
            $this->add($v);
30
        }
31
    }
32
33 341
    public function add(mixed $item): void
34
    {
35 341
        if ($item instanceof Node) {
36 202
            $item->setParent($this);
37 202
            $this->isNative = false;
38
        }
39
40 341
        $this->value[] = $item;
41
    }
42
43
    public function count(): int
44
    {
45
        return count($this->value);
46
    }
47
48
    public function getIterator(): \Iterator
49
    {
50
        return new \ArrayIterator($this->value);
51
    }
52
53 343
    public function getNativeValue(): array
54
    {
55 343
        if (!$this->isNative) {
56 202
            $this->resolve();
57
        }
58
59 343
        return $this->value;
60
    }
61
62 202
    private function resolve(): void
63
    {
64 202
        foreach ($this->value as $k => $v) {
65 202
            $this->value[$k] = $v instanceof Node ? $v->getNativeValue() : $v;
66
        }
67 202
        $this->isNative = true;
68
    }
69
}
70