Files
chill-bundles/src/Bundle/ChillCustomFieldsBundle/Entity/CustomFieldsGroup.php
Julien Fastré 5be85a4fc6 Refactored code to use PHP8 attributes instead of annotations
In this change, Doctrine and validation annotations have been replaced with PHP8 Attributes. The Rector tool has been configured with a list of annotations to convert to attributes. As a consequence, the PHPStan's rules have been updated to reflect these changes. The PHP8's nullable operator (?) has been added where required, and comments in field declaration have been replaced with #[Attribute] syntax.
2024-04-08 12:11:29 +02:00

205 lines
4.1 KiB
PHP

<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\CustomFieldsBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* CustomFieldGroup.
*/
#[ORM\Entity]
#[ORM\Table(name: 'customfieldsgroup')]
class CustomFieldsGroup
{
/**
* The custom fields of the group that are active.
* This variable if null, if this informations has not been computed.
*/
private ?array $activeCustomFields = null;
/**
* The custom fields of the group.
* The custom fields are asc-ordered regarding to their property "ordering".
*
* @var Collection<CustomField>
*/
#[ORM\OneToMany(targetEntity: CustomField::class, mappedBy: 'customFieldGroup')]
#[ORM\OrderBy(['ordering' => \Doctrine\Common\Collections\Criteria::ASC])]
private Collection $customFields;
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::STRING, length: 255)]
private ?string $entity = null;
#[ORM\Id]
#[ORM\Column(name: 'id', type: \Doctrine\DBAL\Types\Types::INTEGER)]
#[ORM\GeneratedValue(strategy: 'AUTO')]
private ?int $id = null;
/**
* @var array
*/
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::JSON)]
private $name;
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::JSON)]
private array $options = [];
/**
* CustomFieldsGroup constructor.
*/
public function __construct()
{
$this->customFields = new ArrayCollection();
}
/**
* Add customField.
*
* @return CustomFieldsGroup
*/
public function addCustomField(CustomField $customField)
{
$this->customFields[] = $customField;
return $this;
}
/**
* Get all the custom.
*/
public function getActiveCustomFields(): array
{
if (null === $this->activeCustomFields) {
$this->activeCustomFields = [];
foreach ($this->customFields as $cf) {
if ($cf->isActive()) {
$this->activeCustomFields[] = $cf;
}
}
}
return $this->activeCustomFields;
}
/**
* @return Collection
*/
public function getCustomFields()
{
return $this->customFields;
}
/**
* Get entity.
*
* @return string
*/
public function getEntity()
{
return $this->entity;
}
/**
* Get id.
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Get name.
*/
public function getName(?string $language = null): array|string
{
// TODO set this in a service, PLUS twig function
if (null !== $language) {
if (isset($this->name[$language])) {
return $this->name[$language];
}
foreach ($this->name as $name) {
if (!empty($name)) {
return $name;
}
}
return '';
}
return $this->name;
}
/**
* get options array.
*
* @return array
*/
public function getOptions()
{
return $this->options;
}
/**
* Remove customField.
*/
public function removeCustomField(CustomField $customField)
{
$this->customFields->removeElement($customField);
}
/**
* Set entity.
*
* @param string $entity
*
* @return CustomFieldsGroup
*/
public function setEntity($entity)
{
$this->entity = $entity;
return $this;
}
/**
* Set name.
*
* @param array $name
*
* @return CustomFieldsGroup
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* set options array.
*
* @return CustomFieldsGroup
*/
public function setOptions(array $options)
{
$this->options = $options;
return $this;
}
}