Passed
Push — master ( b48009...a47442 )
by Pol
02:39
created

SimpleArray::createNode()   A

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
declare(strict_types=1);
4
5
namespace loophp\phptree\Importer;
6
7
use loophp\phptree\Node\AttributeNode;
8
use loophp\phptree\Node\AttributeNodeInterface;
9
use loophp\phptree\Node\NodeInterface;
10
11
/**
12
 * Class SimpleArray.
13
 */
14
final class SimpleArray implements ImporterInterface
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19 1
    public function import($data): NodeInterface
20
    {
21 1
        return $this->parseNode(new AttributeNode(['label' => 'root']), $data);
22
    }
23
24
    /**
25
     * Create a node.
26
     *
27
     * @param mixed $data
28
     *   The arguments
29
     *
30
     * @return AttributeNodeInterface
31
     *   The node
32
     */
33 1
    private function createNode($data): AttributeNodeInterface
34
    {
35 1
        return new AttributeNode([
36 1
            'data' => $data,
37
        ]);
38
    }
39
40
    /**
41
     * @param AttributeNodeInterface $parent
42
     * @param array ...$nodes
43
     *
44
     * @return \loophp\phptree\Node\NodeInterface
45
     */
46 1
    private function parseNode(AttributeNodeInterface $parent, array ...$nodes): NodeInterface
47
    {
48 1
        return array_reduce(
49 1
            $nodes,
50
            function (AttributeNodeInterface $carry, array $node): NodeInterface {
51 1
                $node += ['children' => []];
52
53
                return $carry
54 1
                    ->add(
55 1
                        $this->parseNode(
56 1
                            $this->createNode($node),
57 1
                            ...$node['children']
58
                        )
59
                    );
60 1
            },
61
            $parent
62
        );
63
    }
64
}
65