1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
/** |
5
|
|
|
* Part-DB Version 0.4+ "nextgen" |
6
|
|
|
* Copyright (C) 2016 - 2019 Jan Böhmer |
7
|
|
|
* https://github.com/jbtronics. |
8
|
|
|
* |
9
|
|
|
* This program is free software; you can redistribute it and/or |
10
|
|
|
* modify it under the terms of the GNU General Public License |
11
|
|
|
* as published by the Free Software Foundation; either version 2 |
12
|
|
|
* of the License, or (at your option) any later version. |
13
|
|
|
* |
14
|
|
|
* This program is distributed in the hope that it will be useful, |
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
* GNU General Public License for more details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU General Public License |
20
|
|
|
* along with this program; if not, write to the Free Software |
21
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace App\Entity; |
25
|
|
|
|
26
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
27
|
|
|
use Doctrine\ORM\Mapping as ORM; |
28
|
|
|
use Doctrine\ORM\PersistentCollection; |
29
|
|
|
use App\Validator\Constraints\NoneOfItsChildren; |
30
|
|
|
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; |
31
|
|
|
use Symfony\Component\Serializer\Annotation\DiscriminatorMap; |
32
|
|
|
use Symfony\Component\Serializer\Annotation\Groups; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* All elements with the fields "id", "name" and "parent_id" (at least). |
36
|
|
|
* |
37
|
|
|
* This class is for managing all database objects with a structural design. |
38
|
|
|
* All these sub-objects must have the table columns 'id', 'name' and 'parent_id' (at least)! |
39
|
|
|
* The root node has always the ID '0'. |
40
|
|
|
* It's allowed to have instances of root elements, but if you try to change |
41
|
|
|
* an attribute of a root element, you will get an exception! |
42
|
|
|
* |
43
|
|
|
* @ORM\MappedSuperclass(repositoryClass="App\Repository\StructuralDBElementRepository") |
44
|
|
|
* |
45
|
|
|
* @UniqueEntity(fields={"name", "parent"}, ignoreNull=false, message="structural.entity.unique_name") |
46
|
|
|
*/ |
47
|
|
|
abstract class StructuralDBElement extends AttachmentContainingDBElement |
48
|
|
|
{ |
49
|
|
|
public const ID_ROOT_ELEMENT = 0; |
50
|
|
|
|
51
|
|
|
//This is a not standard character, so build a const, so a dev can easily use it |
52
|
|
|
public const PATH_DELIMITER_ARROW = ' → '; |
53
|
|
|
|
54
|
|
|
// We can not define the mapping here or we will get an exception. Unfortunatly we have to do the mapping in the |
55
|
|
|
// subclasses |
56
|
|
|
/** |
57
|
|
|
* @var StructuralDBElement[] |
58
|
|
|
* @Groups({"include_children"}) |
59
|
|
|
*/ |
60
|
|
|
protected $children; |
61
|
|
|
/** |
62
|
|
|
* @var StructuralDBElement |
63
|
|
|
* @NoneOfItsChildren() |
64
|
|
|
* @Groups({"include_parents"}) |
65
|
|
|
*/ |
66
|
|
|
protected $parent; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var string The comment info for this element |
70
|
|
|
* @ORM\Column(type="string", nullable=true) |
71
|
|
|
* @Groups({"simple", "extended", "full"}) |
72
|
|
|
*/ |
73
|
|
|
protected $comment; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @var int |
77
|
|
|
* @ORM\Column(type="integer", nullable=true) |
78
|
|
|
*/ |
79
|
|
|
protected $parent_id; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var int |
83
|
|
|
*/ |
84
|
|
|
protected $level = 0; |
85
|
|
|
|
86
|
|
|
/** @var string[] all names of all parent elements as a array of strings, |
87
|
|
|
* the last array element is the name of the element itself |
88
|
|
|
* |
89
|
|
|
*/ |
90
|
|
|
private $full_path_strings; |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
|
94
|
|
|
public function __construct() |
95
|
|
|
{ |
96
|
|
|
$this->children = new ArrayCollection(); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/****************************************************************************** |
100
|
|
|
* StructuralDBElement constructor. |
101
|
|
|
*****************************************************************************/ |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Check if this element is a child of another element (recursive). |
105
|
|
|
* |
106
|
|
|
* @param StructuralDBElement $another_element the object to compare |
107
|
|
|
* IMPORTANT: both objects to compare must be from the same class (for example two "Device" objects)! |
108
|
|
|
* |
109
|
|
|
* @return bool True, if this element is child of $another_element. |
110
|
|
|
* |
111
|
|
|
* @throws \InvalidArgumentException if there was an error |
112
|
|
|
*/ |
113
|
|
|
public function isChildOf(StructuralDBElement $another_element) |
114
|
|
|
{ |
115
|
|
|
$class_name = \get_class($this); |
116
|
|
|
|
117
|
|
|
//Check if both elements compared, are from the same type: |
118
|
|
|
if ($class_name != \get_class($another_element)) { |
119
|
|
|
throw new \InvalidArgumentException('isChildOf() only works for objects of the same type!'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if (null == $this->getParent()) { // this is the root node |
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
//If this' parents element, is $another_element, then we are finished |
127
|
|
|
return ($this->parent->getID() == $another_element->getID()) |
128
|
|
|
|| $this->parent->isChildOf($another_element); //Otherwise, check recursivley |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/****************************************************************************** |
132
|
|
|
* |
133
|
|
|
* Getters |
134
|
|
|
* |
135
|
|
|
******************************************************************************/ |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get the parent-ID |
139
|
|
|
* |
140
|
|
|
* @return integer * the ID of the parent element |
141
|
|
|
* * NULL means, the parent is the root node |
142
|
|
|
* * the parent ID of the root node is -1 |
143
|
|
|
*/ |
144
|
|
|
protected function getParentID(): int |
145
|
|
|
{ |
146
|
|
|
return $this->parent_id ?? self::ID_ROOT_ELEMENT; //Null means root element |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get the parent of this element. |
151
|
|
|
* |
152
|
|
|
* @return StructuralDBElement|null The parent element. Null if this element, does not have a parent. |
153
|
|
|
*/ |
154
|
|
|
public function getParent(): ?self |
155
|
|
|
{ |
156
|
|
|
return $this->parent; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Get the comment of the element. |
161
|
|
|
|
162
|
|
|
* @return string the comment |
163
|
|
|
*/ |
164
|
|
|
public function getComment(): ?string |
165
|
|
|
{ |
166
|
|
|
return $this->comment; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Get the level. |
171
|
|
|
* |
172
|
|
|
* The level of the root node is -1. |
173
|
|
|
* |
174
|
|
|
* @return int the level of this element (zero means a most top element |
175
|
|
|
* [a subelement of the root node]) |
176
|
|
|
* |
177
|
|
|
*/ |
178
|
|
|
public function getLevel(): int |
179
|
|
|
{ |
180
|
|
|
/** |
181
|
|
|
* Only check for nodes that have a parent. In the other cases zero is correct. |
182
|
|
|
*/ |
183
|
|
|
if (0 === $this->level && $this->parent !== null) { |
184
|
|
|
$element = $this->parent; |
185
|
|
|
while ($element !== null) { |
186
|
|
|
/** @var StructuralDBElement $element */ |
187
|
|
|
$element = $element->parent; |
188
|
|
|
++$this->level; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
return $this->level; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Get the full path. |
196
|
|
|
* |
197
|
|
|
* @param string $delimeter the delimeter of the returned string |
198
|
|
|
* |
199
|
|
|
* @return string the full path (incl. the name of this element), delimeted by $delimeter |
200
|
|
|
* |
201
|
|
|
*/ |
202
|
|
|
public function getFullPath(string $delimeter = self::PATH_DELIMITER_ARROW): string |
203
|
|
|
{ |
204
|
|
|
if (!\is_array($this->full_path_strings)) { |
|
|
|
|
205
|
|
|
$this->full_path_strings = array(); |
206
|
|
|
$this->full_path_strings[] = $this->getName(); |
207
|
|
|
$element = $this; |
208
|
|
|
|
209
|
|
|
$overflow = 20; //We only allow 20 levels depth |
210
|
|
|
|
211
|
|
|
while (null != $element->parent && $overflow >= 0) { |
212
|
|
|
$element = $element->parent; |
213
|
|
|
$this->full_path_strings[] = $element->getName(); |
214
|
|
|
//Decrement to prevent mem overflow. |
215
|
|
|
$overflow--; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
$this->full_path_strings = array_reverse($this->full_path_strings); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return implode($delimeter, $this->full_path_strings); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Get all subelements of this element. |
226
|
|
|
* |
227
|
|
|
* @param bool $recursive if true, the search is recursive |
228
|
|
|
* |
229
|
|
|
* @return static[] all subelements as an array of objects (sorted by their full path) |
230
|
|
|
*/ |
231
|
|
|
public function getSubelements(): iterable |
232
|
|
|
{ |
233
|
|
|
return $this->children; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
public function getChildren(): iterable |
237
|
|
|
{ |
238
|
|
|
return $this->children; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/****************************************************************************** |
242
|
|
|
* |
243
|
|
|
* Setters |
244
|
|
|
* |
245
|
|
|
******************************************************************************/ |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Sets the new parent object |
249
|
|
|
* @param self $new_parent The new parent object |
250
|
|
|
* @return StructuralDBElement |
251
|
|
|
*/ |
252
|
|
|
public function setParent(?self $new_parent) : self |
253
|
|
|
{ |
254
|
|
|
/* |
255
|
|
|
if ($new_parent->isChildOf($this)) { |
256
|
|
|
throw new \InvalidArgumentException('You can not use one of the element childs as parent!'); |
257
|
|
|
} */ |
258
|
|
|
|
259
|
|
|
$this->parent = $new_parent; |
260
|
|
|
|
261
|
|
|
return $this; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Set the comment. |
266
|
|
|
* @param string $new_comment the new comment |
267
|
|
|
* @return StructuralDBElement |
268
|
|
|
*/ |
269
|
|
|
public function setComment(?string $new_comment): self |
270
|
|
|
{ |
271
|
|
|
$this->comment = $new_comment; |
272
|
|
|
|
273
|
|
|
return $this; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
public function setChildren(array $element) : self |
277
|
|
|
{ |
278
|
|
|
$this->children = $element; |
279
|
|
|
|
280
|
|
|
return $this; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
public function clearChildren() : self |
284
|
|
|
{ |
285
|
|
|
$this->children = new ArrayCollection(); |
|
|
|
|
286
|
|
|
|
287
|
|
|
return $this; |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..