1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
4
|
|
|
* |
5
|
|
|
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics) |
6
|
|
|
* |
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU Affero General Public License as published |
9
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU Affero General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU Affero General Public License |
18
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
declare(strict_types=1); |
22
|
|
|
|
23
|
|
|
namespace App\Entity\Base; |
24
|
|
|
|
25
|
|
|
use App\Entity\Attachments\AttachmentContainingDBElement; |
26
|
|
|
use App\Entity\Parameters\ParametersTrait; |
27
|
|
|
use App\Validator\Constraints\NoneOfItsChildren; |
28
|
|
|
use function count; |
29
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
30
|
|
|
use Doctrine\Common\Collections\Collection; |
31
|
|
|
use Doctrine\ORM\Mapping as ORM; |
32
|
|
|
use function get_class; |
33
|
|
|
use InvalidArgumentException; |
34
|
|
|
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; |
35
|
|
|
use Symfony\Component\Serializer\Annotation\Groups; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* All elements with the fields "id", "name" and "parent_id" (at least). |
39
|
|
|
* |
40
|
|
|
* This class is for managing all database objects with a structural design. |
41
|
|
|
* All these sub-objects must have the table columns 'id', 'name' and 'parent_id' (at least)! |
42
|
|
|
* The root node has always the ID '0'. |
43
|
|
|
* It's allowed to have instances of root elements, but if you try to change |
44
|
|
|
* an attribute of a root element, you will get an exception! |
45
|
|
|
* |
46
|
|
|
* @ORM\MappedSuperclass(repositoryClass="App\Repository\StructuralDBElementRepository") |
47
|
|
|
* |
48
|
|
|
* @ORM\EntityListeners({"App\Security\EntityListeners\ElementPermissionListener", "App\EntityListeners\TreeCacheInvalidationListener"}) |
49
|
|
|
* |
50
|
|
|
* @UniqueEntity(fields={"name", "parent"}, ignoreNull=false, message="structural.entity.unique_name") |
51
|
|
|
*/ |
52
|
|
|
abstract class AbstractStructuralDBElement extends AttachmentContainingDBElement |
53
|
|
|
{ |
54
|
|
|
use ParametersTrait; |
55
|
|
|
|
56
|
|
|
public const ID_ROOT_ELEMENT = 0; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* This is a not standard character, so build a const, so a dev can easily use it. |
60
|
|
|
*/ |
61
|
|
|
public const PATH_DELIMITER_ARROW = ' → '; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var string The comment info for this element |
65
|
|
|
* @ORM\Column(type="text") |
66
|
|
|
* @Groups({"simple", "extended", "full"}) |
67
|
|
|
*/ |
68
|
|
|
protected $comment = ''; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var bool If this property is set, this element can not be selected for part properties. |
72
|
|
|
* Useful if this element should be used only for grouping, sorting. |
73
|
|
|
* @ORM\Column(type="boolean") |
74
|
|
|
*/ |
75
|
|
|
protected $not_selectable = false; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @var int |
79
|
|
|
*/ |
80
|
|
|
protected $level = 0; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* We can not define the mapping here or we will get an exception. Unfortunately we have to do the mapping in the |
84
|
|
|
* subclasses. |
85
|
|
|
* |
86
|
|
|
* @var AbstractStructuralDBElement[]|Collection |
87
|
|
|
* @Groups({"include_children"}) |
88
|
|
|
*/ |
89
|
|
|
protected $children; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @var AbstractStructuralDBElement |
93
|
|
|
* @NoneOfItsChildren() |
94
|
|
|
* @Groups({"include_parents"}) |
95
|
|
|
*/ |
96
|
|
|
protected $parent; |
97
|
|
|
|
98
|
|
|
/** @var string[] all names of all parent elements as a array of strings, |
99
|
|
|
* the last array element is the name of the element itself |
100
|
|
|
*/ |
101
|
|
|
private $full_path_strings = []; |
102
|
|
|
|
103
|
|
|
public function __construct() |
104
|
|
|
{ |
105
|
|
|
parent::__construct(); |
106
|
|
|
$this->children = new ArrayCollection(); |
107
|
|
|
$this->parameters = new ArrayCollection(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function __clone() |
111
|
|
|
{ |
112
|
|
|
if ($this->id) { |
|
|
|
|
113
|
|
|
//Deep clone parameters |
114
|
|
|
$parameters = $this->parameters; |
115
|
|
|
$this->parameters = new ArrayCollection(); |
116
|
|
|
foreach ($parameters as $parameter) { |
117
|
|
|
$this->addParameter(clone $parameter); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
parent::__clone(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/****************************************************************************** |
124
|
|
|
* StructuralDBElement constructor. |
125
|
|
|
*****************************************************************************/ |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Check if this element is a child of another element (recursive). |
129
|
|
|
* |
130
|
|
|
* @param AbstractStructuralDBElement $another_element the object to compare |
131
|
|
|
* IMPORTANT: both objects to compare must be from the same class (for example two "Device" objects)! |
132
|
|
|
* |
133
|
|
|
* @return bool True, if this element is child of $another_element. |
134
|
|
|
* |
135
|
|
|
* @throws InvalidArgumentException if there was an error |
136
|
|
|
*/ |
137
|
|
|
public function isChildOf(self $another_element): bool |
138
|
|
|
{ |
139
|
|
|
$class_name = static::class; |
140
|
|
|
|
141
|
|
|
//Check if both elements compared, are from the same type |
142
|
|
|
// (we have to check inheritance, or we get exceptions when using doctrine entities (they have a proxy type): |
143
|
|
|
if (! is_a($another_element, $class_name) && ! is_a($this, get_class($another_element))) { |
144
|
|
|
throw new InvalidArgumentException('isChildOf() only works for objects of the same type!'); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
if (null === $this->getParent()) { // this is the root node |
148
|
|
|
return false; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
//If this' parents element, is $another_element, then we are finished |
152
|
|
|
return ($this->parent->getID() === $another_element->getID()) |
153
|
|
|
|| $this->parent->isChildOf($another_element); //Otherwise, check recursively |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Checks if this element is an root element (has no parent). |
158
|
|
|
* |
159
|
|
|
* @return bool True if the this element is an root element. |
160
|
|
|
*/ |
161
|
|
|
public function isRoot(): bool |
162
|
|
|
{ |
163
|
|
|
return null === $this->parent; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/****************************************************************************** |
167
|
|
|
* |
168
|
|
|
* Getters |
169
|
|
|
* |
170
|
|
|
******************************************************************************/ |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Get the parent of this element. |
174
|
|
|
* |
175
|
|
|
* @return AbstractStructuralDBElement|null The parent element. Null if this element, does not have a parent. |
176
|
|
|
*/ |
177
|
|
|
public function getParent(): ?self |
178
|
|
|
{ |
179
|
|
|
return $this->parent; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Get the comment of the element. |
184
|
|
|
|
185
|
|
|
* |
186
|
|
|
* @return string the comment |
187
|
|
|
*/ |
188
|
|
|
public function getComment(): ?string |
189
|
|
|
{ |
190
|
|
|
return $this->comment; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Get the level. |
195
|
|
|
* |
196
|
|
|
* The level of the root node is -1. |
197
|
|
|
* |
198
|
|
|
* @return int the level of this element (zero means a most top element |
199
|
|
|
* [a sub element of the root node]) |
200
|
|
|
*/ |
201
|
|
|
public function getLevel(): int |
202
|
|
|
{ |
203
|
|
|
/* |
204
|
|
|
* Only check for nodes that have a parent. In the other cases zero is correct. |
205
|
|
|
*/ |
206
|
|
|
if (0 === $this->level && null !== $this->parent) { |
207
|
|
|
$element = $this->parent; |
208
|
|
|
while (null !== $element) { |
209
|
|
|
/** @var AbstractStructuralDBElement $element */ |
210
|
|
|
$element = $element->parent; |
211
|
|
|
++$this->level; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
return $this->level; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Get the full path. |
220
|
|
|
* |
221
|
|
|
* @param string $delimiter the delimiter of the returned string |
222
|
|
|
* |
223
|
|
|
* @return string the full path (incl. the name of this element), delimited by $delimiter |
224
|
|
|
*/ |
225
|
|
|
public function getFullPath(string $delimiter = self::PATH_DELIMITER_ARROW): string |
226
|
|
|
{ |
227
|
|
|
if (empty($this->full_path_strings)) { |
228
|
|
|
$this->full_path_strings = []; |
229
|
|
|
$this->full_path_strings[] = $this->getName(); |
230
|
|
|
$element = $this; |
231
|
|
|
|
232
|
|
|
$overflow = 20; //We only allow 20 levels depth |
233
|
|
|
|
234
|
|
|
while (null !== $element->parent && $overflow >= 0) { |
235
|
|
|
$element = $element->parent; |
236
|
|
|
$this->full_path_strings[] = $element->getName(); |
237
|
|
|
//Decrement to prevent mem overflow. |
238
|
|
|
--$overflow; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
$this->full_path_strings = array_reverse($this->full_path_strings); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
return implode($delimiter, $this->full_path_strings); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Gets the path to this element (including the element itself). |
249
|
|
|
* |
250
|
|
|
* @return self[] An array with all (recursively) parent elements (including this one), |
251
|
|
|
* ordered from the lowest levels (root node) first to the highest level (the element itself) |
252
|
|
|
*/ |
253
|
|
|
public function getPathArray(): array |
254
|
|
|
{ |
255
|
|
|
$tmp = []; |
256
|
|
|
$tmp[] = $this; |
257
|
|
|
|
258
|
|
|
//We only allow 20 levels depth |
259
|
|
|
while (! end($tmp)->isRoot() && count($tmp) < 20) { |
260
|
|
|
$tmp[] = end($tmp)->parent; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
return array_reverse($tmp); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Get all sub elements of this element. |
268
|
|
|
* |
269
|
|
|
* @return Collection<static>|iterable all subelements as an array of objects (sorted by their full path) |
270
|
|
|
* @psalm-return Collection<int, static> |
271
|
|
|
*/ |
272
|
|
|
public function getSubelements(): iterable |
273
|
|
|
{ |
274
|
|
|
return $this->children; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @return Collection<static>|iterable |
279
|
|
|
* @psalm-return Collection<int, static> |
280
|
|
|
*/ |
281
|
|
|
public function getChildren(): iterable |
282
|
|
|
{ |
283
|
|
|
return $this->children; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @return bool |
288
|
|
|
*/ |
289
|
|
|
public function isNotSelectable(): bool |
290
|
|
|
{ |
291
|
|
|
return $this->not_selectable; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/****************************************************************************** |
295
|
|
|
* |
296
|
|
|
* Setters |
297
|
|
|
* |
298
|
|
|
******************************************************************************/ |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Sets the new parent object. |
302
|
|
|
* |
303
|
|
|
* @param self $new_parent The new parent object |
304
|
|
|
* |
305
|
|
|
* @return AbstractStructuralDBElement |
306
|
|
|
*/ |
307
|
|
|
public function setParent(?self $new_parent): self |
308
|
|
|
{ |
309
|
|
|
/* |
310
|
|
|
if ($new_parent->isChildOf($this)) { |
311
|
|
|
throw new \InvalidArgumentException('You can not use one of the element childs as parent!'); |
312
|
|
|
} */ |
313
|
|
|
|
314
|
|
|
$this->parent = $new_parent; |
315
|
|
|
|
316
|
|
|
return $this; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Set the comment. |
321
|
|
|
* |
322
|
|
|
* @param string $new_comment the new comment |
323
|
|
|
* |
324
|
|
|
* @return AbstractStructuralDBElement |
325
|
|
|
*/ |
326
|
|
|
public function setComment(?string $new_comment): self |
327
|
|
|
{ |
328
|
|
|
$this->comment = $new_comment; |
329
|
|
|
|
330
|
|
|
return $this; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* @param static[]|Collection $elements |
335
|
|
|
* |
336
|
|
|
* @return $this |
337
|
|
|
*/ |
338
|
|
|
public function setChildren($elements): self |
339
|
|
|
{ |
340
|
|
|
if (! is_array($elements) && ! $elements instanceof Collection) { |
|
|
|
|
341
|
|
|
throw new InvalidArgumentException('$elements must be an array or Collection!'); |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
$this->children = $elements; |
345
|
|
|
|
346
|
|
|
return $this; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* @return AbstractStructuralDBElement |
351
|
|
|
*/ |
352
|
|
|
public function setNotSelectable(bool $not_selectable): self |
353
|
|
|
{ |
354
|
|
|
$this->not_selectable = $not_selectable; |
355
|
|
|
|
356
|
|
|
return $this; |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
public function clearChildren(): self |
360
|
|
|
{ |
361
|
|
|
$this->children = new ArrayCollection(); |
362
|
|
|
|
363
|
|
|
return $this; |
364
|
|
|
} |
365
|
|
|
} |
366
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: