Part-DB.Part-DB-server/src/Entity/Base/StructuralDBElement.php

338 lines
10 KiB
PHP
Raw Normal View History

<?php
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
2019-11-01 13:40:30 +01:00
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
declare(strict_types=1);
2019-02-23 22:41:13 +01:00
namespace App\Entity\Base;
2019-02-23 22:41:13 +01:00
use App\Entity\Attachments\AttachmentContainingDBElement;
use App\Validator\Constraints\NoneOfItsChildren;
2020-01-05 22:49:00 +01:00
use function count;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
2019-02-23 22:41:13 +01:00
use Doctrine\ORM\Mapping as ORM;
2020-01-05 22:49:00 +01:00
use function get_class;
use InvalidArgumentException;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Annotation\Groups;
2019-02-23 22:41:13 +01:00
/**
* All elements with the fields "id", "name" and "parent_id" (at least).
2019-02-23 22:41:13 +01:00
*
* This class is for managing all database objects with a structural design.
* All these sub-objects must have the table columns 'id', 'name' and 'parent_id' (at least)!
* The root node has always the ID '0'.
* It's allowed to have instances of root elements, but if you try to change
* an attribute of a root element, you will get an exception!
*
2019-03-25 12:44:44 +01:00
* @ORM\MappedSuperclass(repositoryClass="App\Repository\StructuralDBElementRepository")
*
* @ORM\EntityListeners({"App\Security\EntityListeners\ElementPermissionListener", "App\EntityListeners\TreeCacheInvalidationListener"})
*
* @UniqueEntity(fields={"name", "parent"}, ignoreNull=false, message="structural.entity.unique_name")
2019-02-23 22:41:13 +01:00
*/
abstract class StructuralDBElement extends AttachmentContainingDBElement
{
2019-03-20 23:24:20 +01:00
public const ID_ROOT_ELEMENT = 0;
2019-02-23 22:41:13 +01:00
/**
2020-02-01 16:17:20 +01:00
* This is a not standard character, so build a const, so a dev can easily use it.
2019-02-23 22:41:13 +01:00
*/
2020-01-05 22:49:00 +01:00
public const PATH_DELIMITER_ARROW = ' → ';
2019-02-23 22:41:13 +01:00
/**
* @var string The comment info for this element
* @ORM\Column(type="text")
* @Groups({"simple", "extended", "full"})
2019-02-23 22:41:13 +01:00
*/
2019-08-20 18:39:57 +02:00
protected $comment = '';
2019-02-23 22:41:13 +01:00
/**
* @var bool If this property is set, this element can not be selected for part properties.
* Useful if this element should be used only for grouping, sorting.
* @ORM\Column(type="boolean")
*/
protected $not_selectable = false;
2019-02-23 22:41:13 +01:00
/**
* @var int
*/
protected $level = 0;
2019-02-23 22:41:13 +01:00
2020-01-05 22:49:00 +01:00
/**
* We can not define the mapping here or we will get an exception. Unfortunately we have to do the mapping in the
* subclasses.
*
* @var StructuralDBElement[]
* @Groups({"include_children"})
*/
protected $children = [];
/**
* @var StructuralDBElement
* @NoneOfItsChildren()
* @Groups({"include_parents"})
*/
protected $parent;
2019-02-23 22:41:13 +01:00
/** @var string[] all names of all parent elements as a array of strings,
* the last array element is the name of the element itself
*/
2020-01-05 22:49:00 +01:00
private $full_path_strings = [];
2019-02-23 22:41:13 +01:00
public function __construct()
{
2019-08-20 18:39:57 +02:00
parent::__construct();
$this->children = new ArrayCollection();
}
2019-02-23 22:41:13 +01:00
/******************************************************************************
* StructuralDBElement constructor.
*****************************************************************************/
/**
* Check if this element is a child of another element (recursive).
2019-02-23 22:41:13 +01:00
*
* @param StructuralDBElement $another_element the object to compare
* IMPORTANT: both objects to compare must be from the same class (for example two "Device" objects)!
2019-02-23 22:41:13 +01:00
*
* @return bool True, if this element is child of $another_element.
*
2020-01-05 22:49:00 +01:00
* @throws InvalidArgumentException if there was an error
2019-02-23 22:41:13 +01:00
*/
public function isChildOf(self $another_element): bool
2019-02-23 22:41:13 +01:00
{
2020-01-05 15:46:58 +01:00
$class_name = static::class;
2019-02-23 22:41:13 +01:00
//Check if both elements compared, are from the same type
// (we have to check inheritance, or we get exceptions when using doctrine entities (they have a proxy type):
2020-01-05 22:49:00 +01:00
if (! is_a($another_element, $class_name) && ! is_a($this, get_class($another_element))) {
throw new InvalidArgumentException('isChildOf() only works for objects of the same type!');
2019-02-23 22:41:13 +01:00
}
2019-08-20 18:39:57 +02:00
if (null === $this->getParent()) { // this is the root node
2019-02-23 22:41:13 +01:00
return false;
}
2019-03-20 22:53:06 +01:00
//If this' parents element, is $another_element, then we are finished
2019-08-20 18:39:57 +02:00
return ($this->parent->getID() === $another_element->getID())
2019-11-10 14:00:56 +01:00
|| $this->parent->isChildOf($another_element); //Otherwise, check recursively
2019-02-23 22:41:13 +01:00
}
/**
* Checks if this element is an root element (has no parent).
*
* @return bool True if the this element is an root element.
*/
public function isRoot(): bool
{
return null === $this->parent;
}
2019-02-23 22:41:13 +01:00
/******************************************************************************
*
* Getters
*
******************************************************************************/
/**
* Get the parent of this element.
*
* @return StructuralDBElement|null The parent element. Null if this element, does not have a parent.
*/
public function getParent(): ?self
{
return $this->parent;
}
2019-02-23 22:41:13 +01:00
/**
* Get the comment of the element.
*
* @return string the comment
2019-02-23 22:41:13 +01:00
*/
public function getComment(): ?string
2019-02-23 22:41:13 +01:00
{
return $this->comment;
2019-02-23 22:41:13 +01:00
}
/**
* Get the level.
2019-02-23 22:41:13 +01:00
*
* The level of the root node is -1.
2019-02-23 22:41:13 +01:00
*
* @return int the level of this element (zero means a most top element
2019-11-10 14:00:56 +01:00
* [a sub element of the root node])
2019-02-23 22:41:13 +01:00
*/
public function getLevel(): int
2019-02-23 22:41:13 +01:00
{
/*
* Only check for nodes that have a parent. In the other cases zero is correct.
*/
if (0 === $this->level && null !== $this->parent) {
2019-02-23 22:41:13 +01:00
$element = $this->parent;
while (null !== $element) {
2019-02-23 22:41:13 +01:00
/** @var StructuralDBElement $element */
$element = $element->parent;
++$this->level;
2019-02-23 22:41:13 +01:00
}
}
2019-02-23 22:41:13 +01:00
return $this->level;
}
/**
* Get the full path.
2019-02-23 22:41:13 +01:00
*
2019-11-10 14:00:56 +01:00
* @param string $delimiter the delimiter of the returned string
2019-02-23 22:41:13 +01:00
*
2019-11-10 14:00:56 +01:00
* @return string the full path (incl. the name of this element), delimited by $delimiter
2019-02-23 22:41:13 +01:00
*/
2019-11-10 14:00:56 +01:00
public function getFullPath(string $delimiter = self::PATH_DELIMITER_ARROW): string
2019-02-23 22:41:13 +01:00
{
2020-01-05 22:49:00 +01:00
if (empty($this->full_path_strings)) {
$this->full_path_strings = [];
2019-02-23 22:41:13 +01:00
$this->full_path_strings[] = $this->getName();
$element = $this;
$overflow = 20; //We only allow 20 levels depth
2019-08-20 18:39:57 +02:00
while (null !== $element->parent && $overflow >= 0) {
$element = $element->parent;
2019-02-23 22:41:13 +01:00
$this->full_path_strings[] = $element->getName();
//Decrement to prevent mem overflow.
--$overflow;
2019-02-23 22:41:13 +01:00
}
2019-02-23 22:41:13 +01:00
$this->full_path_strings = array_reverse($this->full_path_strings);
}
2019-11-10 14:00:56 +01:00
return implode($delimiter, $this->full_path_strings);
2019-02-23 22:41:13 +01:00
}
/**
* Gets the path to this element (including the element itself).
*
2019-11-10 14:00:56 +01:00
* @return self[] An array with all (recursively) parent elements (including this one),
* ordered from the lowest levels (root node) first to the highest level (the element itself)
*/
public function getPathArray(): array
{
$tmp = [];
$tmp[] = $this;
//We only allow 20 levels depth
2020-01-05 22:49:00 +01:00
while (! end($tmp)->isRoot() && count($tmp) < 20) {
$tmp[] = end($tmp)->parent;
}
return array_reverse($tmp);
}
2019-02-23 22:41:13 +01:00
/**
2019-11-10 14:00:56 +01:00
* Get all sub elements of this element.
2019-02-23 22:41:13 +01:00
*
* @return Collection<static> all subelements as an array of objects (sorted by their full path)
2019-02-23 22:41:13 +01:00
*/
public function getSubelements(): iterable
2019-02-23 22:41:13 +01:00
{
2019-03-24 15:25:40 +01:00
return $this->children;
2019-02-23 22:41:13 +01:00
}
/**
* @return Collection<static>
*/
public function getChildren(): iterable
{
return $this->children;
}
/**
* @return bool
*/
public function isNotSelectable(): bool
{
return $this->not_selectable;
}
2019-02-23 22:41:13 +01:00
/******************************************************************************
*
* Setters
*
******************************************************************************/
/**
* Sets the new parent object.
*
* @param self $new_parent The new parent object
*
* @return StructuralDBElement
2019-02-23 22:41:13 +01:00
*/
public function setParent(?self $new_parent): self
2019-02-23 22:41:13 +01:00
{
/*
if ($new_parent->isChildOf($this)) {
throw new \InvalidArgumentException('You can not use one of the element childs as parent!');
} */
$this->parent = $new_parent;
return $this;
2019-02-23 22:41:13 +01:00
}
/**
* Set the comment.
*
* @param string $new_comment the new comment
*
* @return StructuralDBElement
2019-02-23 22:41:13 +01:00
*/
public function setComment(?string $new_comment): self
2019-02-23 22:41:13 +01:00
{
$this->comment = $new_comment;
return $this;
2019-02-23 22:41:13 +01:00
}
public function setChildren(array $element): self
{
$this->children = $element;
return $this;
}
/**
* @return StructuralDBElement
*/
public function setNotSelectable(bool $not_selectable): self
{
$this->not_selectable = $not_selectable;
return $this;
}
public function clearChildren(): self
{
$this->children = new ArrayCollection();
return $this;
}
}