SimpleArray::createNode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
/**
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 */
7
8
declare(strict_types=1);
9
10
namespace loophp\phptree\Importer;
11
12
use loophp\phptree\Node\AttributeNode;
13
use loophp\phptree\Node\AttributeNodeInterface;
14
use loophp\phptree\Node\NodeInterface;
15
16
/**
17
 * Class SimpleArray.
18
 */
19
final class SimpleArray implements ImporterInterface
20
{
21 1
    public function import($data): NodeInterface
22
    {
23 1
        return $this->parseNode(new AttributeNode(['label' => 'root']), $data);
24
    }
25
26
    /**
27
     * Create a node.
28
     *
29
     * @param mixed $data
30
     *   The arguments
31
     *
32
     * @return AttributeNodeInterface
33
     *   The node
34
     */
35 1
    private function createNode($data): AttributeNodeInterface
36
    {
37 1
        return new AttributeNode([
38 1
            'data' => $data,
39
        ]);
40
    }
41
42
    /**
43
     * @param array ...$nodes
44
     */
45 1
    private function parseNode(AttributeNodeInterface $parent, array ...$nodes): NodeInterface
46
    {
47 1
        return array_reduce(
48 1
            $nodes,
49
            function (AttributeNodeInterface $carry, array $node): NodeInterface {
50 1
                $node += ['children' => []];
51
52
                return $carry
53 1
                    ->add(
54 1
                        $this->parseNode(
55 1
                            $this->createNode($node),
56 1
                            ...$node['children']
57
                        )
58
                    );
59 1
            },
60
            $parent
61
        );
62
    }
63
}
64