Passed
Branch master (ef264b)
by Pierrick
01:53
created

ArrayNode   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 80.95%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 47
ccs 17
cts 21
cp 0.8095
rs 10
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getIterator() 0 4 1
A resolve() 0 6 3
A count() 0 4 1
A getNativeValue() 0 7 2
A __construct() 0 3 1
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
class ArrayNode extends Node implements \IteratorAggregate, \Countable
19
{
20
    private $value;
21
    private $isNative = true;
22
23 343
    public function __construct(array $defaultValues = [])
24
    {
25 343
        $this->value = $defaultValues;
26 343
    }
27
28 341
    public function add($item)
29
    {
30 341
        if ($item instanceof Node) {
31 202
            $item->setParent($this);
32 202
            $this->isNative = false;
33
        }
34
35 341
        $this->value[] = $item;
36 341
    }
37
38
    #[\ReturnTypeWillChange]
39
    public function count()
40
    {
41
        return count($this->value);
42
    }
43
44
    #[\ReturnTypeWillChange]
45
    public function getIterator()
46
    {
47
        return new \ArrayIterator($this->value);
48
    }
49
50 343
    public function getNativeValue()
51
    {
52 343
        if (!$this->isNative) {
53 202
            $this->resolve();
54
        }
55
56 343
        return $this->value;
57
    }
58
59 202
    private function resolve()
60
    {
61 202
        foreach ($this->value as $k => $v) {
62 202
            $this->value[$k] = $v instanceof Node ? $v->getNativeValue() : $v;
63
        }
64 202
        $this->isNative = true;
65 202
    }
66
}
67