Completed
Push — master ( 408d98...51be17 )
by Jan
04:01
created

StructuralDBElement::getChildren()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 *
4
 * part-db version 0.1
5
 * Copyright (C) 2005 Christoph Lechner
6
 * http://www.cl-projects.de/
7
 *
8
 * part-db version 0.2+
9
 * Copyright (C) 2009 K. Jacobs and others (see authors.php)
10
 * http://code.google.com/p/part-db/
11
 *
12
 * Part-DB Version 0.4+
13
 * Copyright (C) 2016 - 2019 Jan Böhmer
14
 * https://github.com/jbtronics
15
 *
16
 * This program is free software; you can redistribute it and/or
17
 * modify it under the terms of the GNU General Public License
18
 * as published by the Free Software Foundation; either version 2
19
 * of the License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU General Public License
27
 * along with this program; if not, write to the Free Software
28
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
29
 *
30
 */
31
32
declare(strict_types=1);
33
/**
34
 * Part-DB Version 0.4+ "nextgen"
35
 * Copyright (C) 2016 - 2019 Jan Böhmer
36
 * https://github.com/jbtronics.
37
 *
38
 * This program is free software; you can redistribute it and/or
39
 * modify it under the terms of the GNU General Public License
40
 * as published by the Free Software Foundation; either version 2
41
 * of the License, or (at your option) any later version.
42
 *
43
 * This program is distributed in the hope that it will be useful,
44
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
45
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
46
 * GNU General Public License for more details.
47
 *
48
 * You should have received a copy of the GNU General Public License
49
 * along with this program; if not, write to the Free Software
50
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
51
 */
52
53
namespace App\Entity\Base;
54
55
use App\Entity\Attachments\AttachmentContainingDBElement;
56
use Doctrine\Common\Collections\ArrayCollection;
57
use Doctrine\ORM\Mapping as ORM;
58
use Doctrine\ORM\PersistentCollection;
59
use App\Validator\Constraints\NoneOfItsChildren;
60
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
61
use Symfony\Component\Serializer\Annotation\DiscriminatorMap;
62
use Symfony\Component\Serializer\Annotation\Groups;
63
64
/**
65
 * All elements with the fields "id", "name" and "parent_id" (at least).
66
 *
67
 * This class is for managing all database objects with a structural design.
68
 * All these sub-objects must have the table columns 'id', 'name' and 'parent_id' (at least)!
69
 * The root node has always the ID '0'.
70
 * It's allowed to have instances of root elements, but if you try to change
71
 * an attribute of a root element, you will get an exception!
72
 *
73
 * @ORM\MappedSuperclass(repositoryClass="App\Repository\StructuralDBElementRepository")
74
 *
75
 * @UniqueEntity(fields={"name", "parent"}, ignoreNull=false, message="structural.entity.unique_name")
76
 */
77
abstract class StructuralDBElement extends AttachmentContainingDBElement
78
{
79
    public const ID_ROOT_ELEMENT = 0;
80
81
    //This is a not standard character, so build a const, so a dev can easily use it
82
    public const PATH_DELIMITER_ARROW = ' → ';
83
84
    // We can not define the mapping here or we will get an exception. Unfortunatly we have to do the mapping in the
85
    // subclasses
86
    /**
87
     * @var StructuralDBElement[]
88
     * @Groups({"include_children"})
89
     */
90
    protected $children;
91
    /**
92
     * @var StructuralDBElement
93
     * @NoneOfItsChildren()
94
     * @Groups({"include_parents"})
95
     */
96
    protected $parent;
97
98
    /**
99
     * @var string The comment info for this element
100
     * @ORM\Column(type="text")
101
     * @Groups({"simple", "extended", "full"})
102
     */
103
    protected $comment = "";
104
105
    /**
106
     * @var int
107
     * @ORM\Column(type="integer", nullable=true)
108
     */
109
    protected $parent_id;
110
111
    /**
112
     * @var bool If this property is set, this element can not be selected for part properties.
113
     * Useful if this element should be used only for grouping, sorting.
114
     * @ORM\Column(type="boolean")
115
     */
116
    protected $not_selectable = false;
117
118
    /**
119
     * @var int
120
     */
121
    protected $level = 0;
122
123
    /** @var string[] all names of all parent elements as a array of strings,
124
     *  the last array element is the name of the element itself
125
     *
126
     */
127
    private $full_path_strings;
128
129
130
131
    public function __construct()
132
    {
133
        $this->children = new ArrayCollection();
0 ignored issues
show
Documentation Bug introduced by
It seems like new Doctrine\Common\Collections\ArrayCollection() of type Doctrine\Common\Collections\ArrayCollection is incompatible with the declared type App\Entity\Base\StructuralDBElement[] of property $children.

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..

Loading history...
134
    }
135
136
    /******************************************************************************
137
     * StructuralDBElement constructor.
138
     *****************************************************************************/
139
140
    /**
141
     * Check if this element is a child of another element (recursive).
142
     *
143
     * @param StructuralDBElement $another_element the object to compare
144
     *     IMPORTANT: both objects to compare must be from the same class (for example two "Device" objects)!
145
     *
146
     * @return bool True, if this element is child of $another_element.
147
     *
148
     * @throws \InvalidArgumentException if there was an error
149
     */
150
    public function isChildOf(StructuralDBElement $another_element)
151
    {
152
        $class_name = \get_class($this);
153
154
        //Check if both elements compared, are from the same type:
155
        if ($class_name != \get_class($another_element)) {
156
            throw new \InvalidArgumentException('isChildOf() only works for objects of the same type!');
157
        }
158
159
        if (null == $this->getParent()) { // this is the root node
160
            return false;
161
        }
162
163
        //If this' parents element, is $another_element, then we are finished
164
        return ($this->parent->getID() == $another_element->getID())
165
            || $this->parent->isChildOf($another_element); //Otherwise, check recursivley
166
    }
167
168
    /******************************************************************************
169
     *
170
     * Getters
171
     *
172
     ******************************************************************************/
173
174
    /**
175
     * Get the parent-ID
176
     *
177
     * @return integer          * the ID of the parent element
178
     *                          * NULL means, the parent is the root node
179
     *                          * the parent ID of the root node is -1
180
     */
181
    protected function getParentID(): int
182
    {
183
        return $this->parent_id ?? self::ID_ROOT_ELEMENT; //Null means root element
184
    }
185
186
    /**
187
     * Get the parent of this element.
188
     *
189
     * @return StructuralDBElement|null The parent element. Null if this element, does not have a parent.
190
     */
191
    public function getParent(): ?self
192
    {
193
        return $this->parent;
194
    }
195
196
    /**
197
     *  Get the comment of the element.
198
199
     * @return string the comment
200
     */
201
    public function getComment(): ?string
202
    {
203
        return $this->comment;
204
    }
205
206
    /**
207
     * Get the level.
208
     *
209
     * The level of the root node is -1.
210
     *
211
     * @return int the level of this element (zero means a most top element
212
     *             [a subelement of the root node])
213
     *
214
     */
215
    public function getLevel(): int
216
    {
217
        /**
218
         * Only check for nodes that have a parent. In the other cases zero is correct.
219
         */
220
        if (0 === $this->level && $this->parent !== null) {
221
            $element = $this->parent;
222
            while ($element !== null) {
223
                /** @var StructuralDBElement $element */
224
                $element = $element->parent;
225
                ++$this->level;
226
            }
227
        }
228
        return $this->level;
229
    }
230
231
    /**
232
     * Get the full path.
233
     *
234
     * @param string $delimeter the delimeter of the returned string
235
     *
236
     * @return string the full path (incl. the name of this element), delimeted by $delimeter
237
     *
238
     */
239
    public function getFullPath(string $delimeter = self::PATH_DELIMITER_ARROW): string
240
    {
241
        if (!\is_array($this->full_path_strings)) {
0 ignored issues
show
introduced by
The condition is_array($this->full_path_strings) is always true.
Loading history...
242
            $this->full_path_strings = array();
243
            $this->full_path_strings[] = $this->getName();
244
            $element = $this;
245
246
            $overflow = 20; //We only allow 20 levels depth
247
248
            while (null != $element->parent && $overflow >= 0) {
249
                $element = $element->parent;
250
                $this->full_path_strings[] = $element->getName();
251
                //Decrement to prevent mem overflow.
252
                $overflow--;
253
            }
254
255
            $this->full_path_strings = array_reverse($this->full_path_strings);
256
        }
257
258
        return implode($delimeter, $this->full_path_strings);
259
    }
260
261
    /**
262
     * Get all subelements of this element.
263
     *
264
     * @param bool $recursive if true, the search is recursive
265
     *
266
     * @return static[] all subelements as an array of objects (sorted by their full path)
267
     */
268
    public function getSubelements(): iterable
269
    {
270
        return $this->children;
271
    }
272
273
    public function getChildren(): iterable
274
    {
275
        return $this->children;
276
    }
277
278
    /**
279
     * @return bool
280
     */
281
    public function isNotSelectable(): bool
282
    {
283
        return $this->not_selectable;
284
    }
285
286
    /******************************************************************************
287
     *
288
     * Setters
289
     *
290
     ******************************************************************************/
291
292
    /**
293
     * Sets the new parent object
294
     * @param self $new_parent The new parent object
295
     * @return StructuralDBElement
296
     */
297
    public function setParent(?self $new_parent) : self
298
    {
299
        /*
300
        if ($new_parent->isChildOf($this)) {
301
            throw new \InvalidArgumentException('You can not use one of the element childs as parent!');
302
        } */
303
304
        $this->parent = $new_parent;
305
306
        return $this;
307
    }
308
309
    /**
310
     *  Set the comment.
311
     * @param string $new_comment the new comment
312
     * @return StructuralDBElement
313
     */
314
    public function setComment(?string $new_comment): self
315
    {
316
        $this->comment = $new_comment;
317
318
        return $this;
319
    }
320
321
    public function setChildren(array $element) : self
322
    {
323
        $this->children = $element;
324
325
        return $this;
326
    }
327
328
    /**
329
     * @param bool $not_selectable
330
     * @return StructuralDBElement
331
     */
332
    public function setNotSelectable(bool $not_selectable): StructuralDBElement
333
    {
334
        $this->not_selectable = $not_selectable;
335
        return $this;
336
    }
337
338
    public function clearChildren() : self
339
    {
340
        $this->children = new ArrayCollection();
0 ignored issues
show
Documentation Bug introduced by
It seems like new Doctrine\Common\Collections\ArrayCollection() of type Doctrine\Common\Collections\ArrayCollection is incompatible with the declared type App\Entity\Base\StructuralDBElement[] of property $children.

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..

Loading history...
341
342
        return $this;
343
    }
344
}
345