Builder::nodeInstanceByValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php namespace Mbh\Tree\Traits;
2
3
/**
4
 * MBHFramework
5
 *
6
 * @link      https://github.com/MBHFramework/mbh-framework
7
 * @copyright Copyright (c) 2017 Ulises Jeremias Cornejo Fandos
8
 * @license   https://github.com/MBHFramework/mbh-framework/blob/master/LICENSE (MIT License)
9
 */
10
11
use Mbh\Tree\Interfaces\Node as NodeInterface;
12
13
trait Builder
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18
    public function setNode(NodeInterface $node)
19
    {
20
        $this->emptyStack()->pushNode($node);
21
22
        return $this;
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function getNode()
29
    {
30
        return $this->peekStack();
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function leaf($value = null)
37
    {
38
        $this->getNode()->addChild(
39
            $this->nodeInstanceByValue($value)
40
        );
41
42
        return $this;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function leafs($value1 /*,  $value2, ... */)
49
    {
50
        foreach (func_get_args() as $value) {
51
            $this->leaf($value);
52
        }
53
54
        return $this;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function tree($value)
61
    {
62
        $node = $this->nodeInstanceByValue($value);
63
        $this->getNode()->addChild($node);
64
        $this->pushNode($node);
65
66
        return $this;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function end()
73
    {
74
        $this->popNode();
75
76
        return $this;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function nodeInstanceByValue($value = null)
83
    {
84
        return new \Mbh\Tree\Node($value);
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function value($value = null)
91
    {
92
        $this->getNode()->setValue($value);
93
        return $this;
94
    }
95
96
    abstract protected function peekStack(): NodeInterface;
97
98
    abstract protected function emptyStack();
99
100
    abstract protected function pushNode(NodeInterface $node);
101
102
    abstract protected function popNode();
103
}
104