AbstractNode::setParent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
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\Sorter\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(string $id) {
59
        $this->setId($id);
60
        $this->setIndex([]);
61
        $this->setNodes([]);
62
    }
63
64
    /**
65
     * Add a children node.
66
     *
67
     * @param AbstractNode $node The children node.
68
     * @return AbstractNode Returns this node.
69
     */
70
    public function addNode(AbstractNode $node): AbstractNode {
71
        $node->parent           = $this;
72
        $this->index[$node->id] = count($this->nodes);
73
        $this->nodes[]          = $node;
74
        return $this;
75
    }
76
77
    /**
78
     * Clear the nodes.
79
     *
80
     * @return AbstractNode Returns this node.
81
     */
82
    public function clearNodes(): AbstractNode {
83
        foreach ($this->nodes as $node) {
84
            $this->removeNode($node);
85
        }
86
        return $this;
87
    }
88
89
    /**
90
     * {@inheritDoc}
91
     */
92
    public function getAlphabeticalTreeNodeLabel(): ?string {
93
        return $this->id;
94
    }
95
96
    /**
97
     * {@inheritDoc}
98
     */
99
    public function getAlphabeticalTreeNodeParent(): ?AlphabeticalTreeNodeInterface {
100
        return $this->parent;
101
    }
102
103
    /**
104
     * Get the first children node.
105
     *
106
     * @return AbstractNode|null Returns the first node in case of success, null otherwise.
107
     */
108
    public function getFirstNode(): ?AbstractNode {
109
        return $this->getNodeAt(0);
110
    }
111
112
    /**
113
     * Get the id.
114
     *
115
     * @return string Returns the id.
116
     */
117
    public function getId(): string {
118
        return $this->id;
119
    }
120
121
    /**
122
     * Get the last children node.
123
     *
124
     * @return AbstractNode|null Returns the last node in case of success, null otherwise.
125
     */
126
    public function getLastNode(): ?AbstractNode {
127
        return $this->getNodeAt(count($this->nodes) - 1);
128
    }
129
130
    /**
131
     * Get a node at position.
132
     *
133
     * @param int $position The position.
134
     * @return AbstractNode|null Returns the node in case of success, null otherwise.
135
     */
136
    public function getNodeAt(int $position): ?AbstractNode {
137
        if (0 <= $position && $position <= count($this->nodes) - 1) {
138
            return $this->nodes[$position];
139
        }
140
        return null;
141
    }
142
143
    /**
144
     * Get a node by id.
145
     *
146
     * @param string $id The id.
147
     * @param bool $recursively Recursively ?
148
     * @return AbstractNode|null Returns a node in case of success, null otherwise.
149
     */
150
    public function getNodeById(string $id, $recursively = false): ?AbstractNode {
151
        $found = null;
152
        if (true === array_key_exists($id, $this->index)) {
153
            $found = $this->getNodeAt($this->index[$id]);
154
        }
155
        if (null === $found && true === $recursively) {
156
            foreach ($this->nodes as $current) {
157
                $found = $current->getNodeById($id, $recursively);
158
                if (null !== $found) {
159
                    break;
160
                }
161
            }
162
        }
163
        return $found;
164
    }
165
166
    /**
167
     * Get the nodes.
168
     *
169
     * @return AbstractNode[] Returns the nodes.
170
     */
171
    public function getNodes(): array {
172
        return $this->nodes;
173
    }
174
175
    /**
176
     * Get the parent.
177
     *
178
     * @return AbstractNode Returns the parent.
179
     */
180
    public function getParent(): ?AbstractNode {
181
        return $this->parent;
182
    }
183
184
    /**
185
     * Remove a node.
186
     *
187
     * @param AbstractNode $node The children node.
188
     * @return AbstractNode Returns this node.
189
     */
190
    public function removeNode(AbstractNode $node): AbstractNode {
191
        if (true === array_key_exists($node->id, $this->index)) {
192
            unset($this->nodes[$this->index[$node->id]]);
193
            unset($this->index[$node->id]);
194
            $node->parent = null;
195
        }
196
        return $this;
197
    }
198
199
    /**
200
     * Set the id.
201
     *
202
     * @param string $id The id.
203
     * @return AbstractNode Returns this node.
204
     */
205
    protected function setId(string $id): AbstractNode {
206
        $this->id = $id;
207
        return $this;
208
    }
209
210
    /**
211
     * Set the index.
212
     *
213
     * @param array $index The index.
214
     * @return AbstractNode Returns this node.
215
     */
216
    protected function setIndex(array $index): AbstractNode {
217
        $this->index = $index;
218
        return $this;
219
    }
220
221
    /**
222
     * Set the nodes.
223
     *
224
     * @param AbstractNode[] $nodes The nodes.
225
     * @return AbstractNode Returns this node.
226
     */
227
    protected function setNodes(array $nodes): AbstractNode {
228
        $this->nodes = $nodes;
229
        return $this;
230
    }
231
232
    /**
233
     * Set the parent.
234
     *
235
     * @param AbstractNode|null $parent The parent.
236
     * @return AbstractNode Returns this node.
237
     */
238
    protected function setParent(AbstractNode $parent = null): AbstractNode {
239
        $this->parent = $parent;
240
        return $this;
241
    }
242
243
    /**
244
     * Size.
245
     *
246
     * @return int Returns the size.
247
     */
248
    public function size(): int {
249
        return count($this->nodes);
250
    }
251
}
252