src/Entity/Unit.php line 81

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiProperty;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use App\Entity\Project\Project;
  6. use App\Interfaces\SluggableInterface;
  7. use App\Interfaces\UuidKeyInterface;
  8. use App\Repository\UnitRepository;
  9. use App\Traits\SlugAsIdTrait;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use Doctrine\Common\Collections\Collection;
  12. use Doctrine\ORM\Mapping as ORM;
  13. use Gedmo\Mapping\Annotation as Gedmo;
  14. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  15. use Symfony\Component\Serializer\Annotation\Groups;
  16. use Symfony\Component\Validator\Constraints as Assert;
  17. #[ApiResource(
  18.     collectionOperations: [
  19.         'get',
  20.         'post' => ['security' => "is_granted('ROLE_MASTER_MANAGER')"],
  21.     ],
  22.     itemOperations: [
  23.         'get',
  24.         'put' => ['security' => "is_granted('ROLE_MASTER_MANAGER')",
  25.             'openapi_context' => [
  26.                 'summary' => 'Updates the Unit resource',
  27.                 'description' => 'Updates the Unit resource',
  28.                 'requestBody' => [
  29.                     'content' => [
  30.                         'application/ld+json' => [
  31.                             'schema' => [
  32.                                 'type' => 'object',
  33.                                 'properties' => [
  34.                                     'name' => ['type' => 'string''maxLength' => 255],
  35.                                     'slug' => ['type' => 'string''maxLength' => 255],
  36.                                 ],
  37.                             ],
  38.                             'example' => [
  39.                                 'name' => 'string',
  40.                                 'slug' => 'string',
  41.                             ],
  42.                         ],
  43.                     ],
  44.                 ],
  45.             ],
  46.         ],
  47.         'patch' => ['security' => "is_granted('ROLE_MASTER_MANAGER')",
  48.             'openapi_context' => [
  49.                 'summary' => 'Updates the Unit resource',
  50.                 'description' => 'Updates the Unit resource',
  51.                 'requestBody' => [
  52.                     'content' => [
  53.                         'application/merge-patch+json' => [
  54.                             'schema' => [
  55.                                 'type' => 'object',
  56.                                 'properties' => [
  57.                                     'name' => ['type' => 'string''maxLength' => 255],
  58.                                     'slug' => ['type' => 'string''maxLength' => 255],
  59.                                 ],
  60.                             ],
  61.                             'example' => [
  62.                                 'name' => 'string',
  63.                                 'slug' => 'string',
  64.                             ],
  65.                         ],
  66.                     ],
  67.                 ],
  68.             ],
  69.         ],
  70.         'delete' => ['security' => "is_granted('ROLE_MASTER_MANAGER')"],
  71.     ],
  72. )]
  73. #[ORM\Entity(repositoryClassUnitRepository::class)]
  74. #[ORM\Table(name'unit')]
  75. #[ORM\Index(name'idx_unit_slug'columns: ['slug'])]
  76. #[UniqueEntity(fields: ['name'])]
  77. #[UniqueEntity(fields: ['slug'])]
  78. class Unit implements UuidKeyInterfaceSluggableInterface
  79. {
  80.     use SlugAsIdTrait;
  81.     public const READ_GROUP 'Unit:read';
  82.     public const WRITE_GROUP 'Unit:write';
  83.     #[Assert\NotBlank]
  84.     #[Groups([self::READ_GROUPself::WRITE_GROUPProject::READ_GROUPUser::READ_GROUP])]
  85.     #[ApiProperty(attributes: ['openapi_context' => ['type' => 'string''required' => false]])]
  86.     #[ORM\Column(type'string'length255nullablefalseoptions: ['comment' => 'Name of Unit'])]
  87.     protected ?string $name null;
  88.     #[Gedmo\Slug(fields: ['name'], updatablefalseseparator'-')]
  89.     #[Assert\NotBlank]
  90.     #[Assert\Regex(pattern'/^[a-z0-9]+(?:-[a-z0-9]+)*$/i')]
  91.     #[ApiProperty(iri'https://schema.org/identifier'identifiertrue)]
  92.     #[Groups([self::READ_GROUPself::WRITE_GROUPUser::READ_GROUP])]
  93.     #[ORM\Column(type'string'length255uniquetrue)]
  94.     protected ?string $slug null;
  95.     #[ORM\OneToMany(targetEntityProject::class, mappedBy'unit')]
  96.     private Collection $projects;
  97.     public function __construct(string $name nullstring $slug null)
  98.     {
  99.         $this->name $name;
  100.         $this->slug $slug;
  101.         $this->projects = new ArrayCollection();
  102.     }
  103.     public function getName(): ?string
  104.     {
  105.         return $this->name;
  106.     }
  107.     public function setName(?string $name): self
  108.     {
  109.         $this->name $name;
  110.         return $this;
  111.     }
  112.     public function setSlug(?string $slug): self
  113.     {
  114.         $this->slug $slug;
  115.         return $this;
  116.     }
  117.     #[Groups([self::READ_GROUP])]
  118.     public function getProjectCount(): int
  119.     {
  120.         return $this->projects->count();
  121.     }
  122.     public function __toString(): string
  123.     {
  124.         return $this->getName() ?? $this->slug ?? '';
  125.     }
  126. }