| Total Complexity | 9 |
| Total Lines | 86 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php namespace Mbh\Tree\Traits; |
||
| 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->getNodeAt(count($this->nodeStack) - 1); |
||
|
|
|||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * {@inheritdoc} |
||
| 35 | */ |
||
| 36 | public function leaf($value = null) |
||
| 37 | { |
||
| 38 | $this->getNode()->addChild( |
||
| 39 | $this->nodeInstanceByValue($value) |
||
| 40 | ); |
||
| 41 | return $this; |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * {@inheritdoc} |
||
| 46 | */ |
||
| 47 | public function leafs($value1 /*, $value2, ... */) |
||
| 48 | { |
||
| 49 | foreach (func_get_args() as $value) { |
||
| 50 | $this->leaf($value); |
||
| 51 | } |
||
| 52 | return $this; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * {@inheritdoc} |
||
| 57 | */ |
||
| 58 | public function tree($value = null) |
||
| 59 | { |
||
| 60 | $node = $this->nodeInstanceByValue($value); |
||
| 61 | $this->getNode()->addChild($node); |
||
| 62 | $this->pushNode($node); |
||
| 63 | return $this; |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * {@inheritdoc} |
||
| 68 | */ |
||
| 69 | public function end() |
||
| 70 | { |
||
| 71 | $this->popNode(); |
||
| 72 | return $this; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * {@inheritdoc} |
||
| 77 | */ |
||
| 78 | public function nodeInstanceByValue($value = null) |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * {@inheritdoc} |
||
| 85 | */ |
||
| 86 | public function value($value) |
||
| 87 | { |
||
| 90 | } |
||
| 91 | |||
| 92 | abstract protected function getNodeAt(int $index): NodeInterface; |
||
| 93 | |||
| 94 | abstract protected function emptyStack(); |
||
| 95 | |||
| 96 | abstract protected function pushNode(NodeInterface $node); |
||
| 97 | |||
| 98 | abstract protected function popNode(); |
||
| 99 | } |
||
| 100 |