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

96 lines
3.4 KiB
PHP
Raw Normal View History

<?php
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
2022-11-29 22:28:53 +01:00
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
*
2020-02-22 18:14:36 +01:00
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 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
2020-02-22 18:14:36 +01:00
* GNU Affero General Public License for more details.
*
2020-02-22 18:14:36 +01:00
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
2019-02-23 22:41:13 +01:00
namespace App\Entity\Base;
2019-02-23 22:41:13 +01:00
2023-06-13 10:36:34 +02:00
use App\Entity\Attachments\Attachment;
use App\Entity\Parameters\AbstractParameter;
use App\Entity\Contracts\StructuralElementInterface;
use App\Entity\Contracts\HasParametersInterface;
2023-06-11 14:55:06 +02:00
use App\Repository\StructuralDBElementRepository;
use App\EntityListeners\TreeCacheInvalidationListener;
use App\Validator\Constraints\UniqueObjectCollection;
use App\Entity\Attachments\AttachmentContainingDBElement;
use App\Entity\Parameters\ParametersTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
2019-02-23 22:41:13 +01:00
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
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!
*
*
2023-06-11 15:02:59 +02:00
* @see \App\Tests\Entity\Base\AbstractStructuralDBElementTest
2023-06-13 10:36:34 +02:00
*
* @template AT of Attachment
* @template PT of AbstractParameter
2023-06-13 10:36:34 +02:00
* @template-use ParametersTrait<PT>
* @extends AttachmentContainingDBElement<AT>
* @uses ParametersTrait<PT>
2019-02-23 22:41:13 +01:00
*/
2024-03-03 19:57:31 +01:00
#[UniqueEntity(fields: ['name', 'parent'], message: 'structural.entity.unique_name', ignoreNull: false)]
2023-06-11 14:55:06 +02:00
#[ORM\MappedSuperclass(repositoryClass: StructuralDBElementRepository::class)]
#[ORM\EntityListeners([TreeCacheInvalidationListener::class])]
abstract class AbstractStructuralDBElement extends AttachmentContainingDBElement implements StructuralElementInterface, HasParametersInterface
2019-02-23 22:41:13 +01:00
{
use ParametersTrait;
use StructuralElementTrait;
2020-01-05 22:49:00 +01:00
2023-06-13 10:36:34 +02:00
/**
* Mapping done in subclasses.
*
* @var Collection<int, AbstractParameter>
* @phpstan-var Collection<int, PT>
*/
2024-03-03 19:57:31 +01:00
#[Assert\Valid]
#[UniqueObjectCollection(fields: ['name', 'group', 'element'])]
2023-06-13 10:36:34 +02:00
protected Collection $parameters;
public function __construct()
{
2019-08-20 18:39:57 +02:00
parent::__construct();
$this->initializeStructuralElement();
$this->parameters = new ArrayCollection();
}
public function __clone()
{
if ($this->id) {
//Deep clone parameters
$parameters = $this->parameters;
$this->parameters = new ArrayCollection();
foreach ($parameters as $parameter) {
$this->addParameter(clone $parameter);
}
}
parent::__clone();
}
}