AST::import()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
namespace VLF;
4
5
/**
6
 * AST дерево
7
 */
8
class AST
9
{
10
    // Список корневых нод
11
    protected array $nodes = [];
12
13
    /**
14
     * Конструктор дерева
15
     * 
16
     * [@param array $tree = null] - массив для импорта дерева
17
     */
18
    public function __construct (array $tree = null)
19
    {
20
        if ($tree !== null)
21
            $this->import ($tree);
22
    }
23
24
    /**
25
     * Импорт дерева
26
     * 
27
     * @param array $tree - массив для импорта
28
     * 
29
     * @return AST - возвращает сам себя
30
     */
31
    public function import (array $tree): AST
32
    {
33
        foreach ($tree as $node)
34
            $this->nodes[] = new Node ($node);
35
36
        return $this;
37
    }
38
39
    /**
40
     * Экспорт дерева
41
     * 
42
     * @return array - возращает массив экспортированного дерева
43
     */
44
    public function export (): array
45
    {
46
        return array_map (fn (Node $node) => $node->export (), $this->nodes);
47
    }
48
49
    /**
50
     * Добавить корневую ноду
51
     * 
52
     * @param Node $node - нода для добавления
53
     * 
54
     * @return AST - возвращает сам себя
55
     */
56
    public function push (Node $node): AST
57
    {
58
        $this->nodes[] = $node;
59
60
        return $this;
61
    }
62
63
    /**
64
     * Получить список корневых нод
65
     * 
66
     * @return array - возвращает список корневых нод
67
     */
68
    public function getNodes (): array
69
    {
70
        return $this->nodes;
71
    }
72
}
73