Completed
Push — master ( c777ad...d0e678 )
by WEBEWEB
01:18
created

AbstractNode::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2017 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Library\Core\Model;
13
14
use WBW\Library\Core\Sorting\AlphabeticalTreeNodeInterface;
15
16
/**
17
 * Abstract node.
18
 *
19
 * @author webeweb <https://github.com/webeweb/>
20
 * @package WBW\Library\Core\Model
21
 * @abstract
22
 */
23
abstract class AbstractNode implements AlphabeticalTreeNodeInterface {
24
25
    /**
26
     * Id.
27
     *
28
     * @var string
29
     */
30
    private $id;
31
32
    /**
33
     * Index.
34
     *
35
     * @var array
36
     */
37
    private $index;
38
39
    /**
40
     * Children nodes.
41
     *
42
     * @var AbstractNode[]
43
     */
44
    private $nodes;
45
46
    /**
47
     * Parent node.
48
     *
49
     * @var AbstractNode
50
     */
51
    private $parent;
52
53
    /**
54
     * Constructor.
55
     *
56
     * @param string $id The id.
57
     */
58
    protected function __construct($id) {
59
        $this->setId($id);
60
        $this->setIndex([]);
61
        $this->setNodes([]);
62
        $this->setParent(null);
63
    }
64
65
    /**
66
     * Add a children node.
67
     *
68
     * @param AbstractNode $node The children node.
69
     * @return AbstractNode Returns this node.
70
     */
71
    public function addNode(AbstractNode $node) {
72
        $node->parent           = $this;
73
        $this->index[$node->id] = count($this->nodes);
74
        $this->nodes[]          = $node;
75
        return $this;
76
    }
77
78
    /**
79
     * Clear the nodes.
80
     *
81
     * @return AbstractNode Returns this node.
82
     */
83
    public function clearNodes() {
84
        foreach ($this->nodes as $node) {
85
            $this->removeNode($node);
86
        }
87
        return $this;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function getAlphabeticalTreeNodeLabel() {
94
        return $this->id;
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function getAlphabeticalTreeNodeParent() {
101
        return $this->parent;
102
    }
103
104
    /**
105
     * Get the first children node.
106
     *
107
     * @return AbstractNode|null Returns the first node in case of success, null otherwise.
108
     */
109
    public function getFirstNode() {
110
        return $this->getNodeAt(0);
111
    }
112
113
    /**
114
     * Get the id.
115
     *
116
     * @return string Returns the id.
117
     */
118
    public function getId() {
119
        return $this->id;
120
    }
121
122
    /**
123
     * Get the last children node.
124
     *
125
     * @return AbstractNode|null Returns the last node in case of success, null otherwise.
126
     */
127
    public function getLastNode() {
128
        return $this->getNodeAt(count($this->nodes) - 1);
129
    }
130
131
    /**
132
     * Get a node at position.
133
     *
134
     * @param int $position The position.
135
     * @return AbstractNode|null Returns the node in case of success, null otherwise.
136
     */
137
    public function getNodeAt($position) {
138
        if (0 <= $position && $position <= count($this->nodes) - 1) {
139
            return $this->nodes[$position];
140
        }
141
        return null;
142
    }
143
144
    /**
145
     * Get a node by id.
146
     *
147
     * @param string $id The id.
148
     * @param bool $recursively Recursively ?
149
     * @return AbstractNode|null Returns a node in case of success, null otherwise.
150
     */
151
    public function getNodeById($id, $recursively = false) {
152
        $found = null;
153
        if (true === array_key_exists($id, $this->index)) {
154
            $found = $this->getNodeAt($this->index[$id]);
155
        }
156
        if (null === $found && true === $recursively) {
157
            foreach ($this->nodes as $current) {
158
                $found = $current->getNodeById($id, $recursively);
159
                if (null !== $found) {
160
                    break;
161
                }
162
            }
163
        }
164
        return $found;
165
    }
166
167
    /**
168
     * Get the nodes.
169
     *
170
     * @return AbstractNode[] Returns the nodes.
171
     */
172
    public function getNodes() {
173
        return $this->nodes;
174
    }
175
176
    /**
177
     * Get the parent.
178
     *
179
     * @return AbstractNode Returns the parent.
180
     */
181
    public function getParent() {
182
        return $this->parent;
183
    }
184
185
    /**
186
     * Remove a node.
187
     *
188
     * @param AbstractNode $node The children node.
189
     * @return AbstractNode Returns this node.
190
     */
191
    public function removeNode(AbstractNode $node) {
192
        if (true === array_key_exists($node->id, $this->index)) {
193
            unset($this->nodes[$this->index[$node->id]]);
194
            unset($this->index[$node->id]);
195
            $node->parent = null;
196
        }
197
        return $this;
198
    }
199
200
    /**
201
     * Set the id.
202
     *
203
     * @param string $id The id.
204
     * @return AbstractNode Returns this node.
205
     */
206
    protected function setId($id) {
207
        $this->id = $id;
208
        return $this;
209
    }
210
211
    /**
212
     * Set the index.
213
     *
214
     * @param array $index The index.
215
     * @return AbstractNode Returns this node.
216
     */
217
    protected function setIndex(array $index) {
218
        $this->index = $index;
219
        return $this;
220
    }
221
222
    /**
223
     * Set the nodes.
224
     *
225
     * @param AbstractNode[] $nodes The nodes.
226
     * @return AbstractNode Returns this node.
227
     */
228
    protected function setNodes(array $nodes) {
229
        $this->nodes = $nodes;
230
        return $this;
231
    }
232
233
    /**
234
     * Set the parent.
235
     *
236
     * @param AbstractNode $parent The parent.
237
     * @return AbstractNode Returns this node.
238
     */
239
    protected function setParent(AbstractNode $parent = null) {
240
        $this->parent = $parent;
241
        return $this;
242
    }
243
244
    /**
245
     * Size.
246
     *
247
     * @return int Returns the size.
248
     */
249
    public function size() {
250
        return count($this->nodes);
251
    }
252
}
253