src/Entity/News.php line 61

  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM,
  4.     Doctrine\DBAL\Types\Types,
  5.     Doctrine\Common\Collections\Collection,
  6.     Doctrine\Common\Collections\ArrayCollection;
  7. use Symfony\Component\Serializer\Annotation\Groups,
  8.     Symfony\Component\Validator\Constraints as Assert;
  9. use ApiPlatform\Metadata\ApiResource,
  10.     ApiPlatform\Metadata\ApiProperty,
  11.     ApiPlatform\Metadata\Get,
  12.     ApiPlatform\Metadata\GetCollection,
  13.     ApiPlatform\Metadata\Post,
  14.     ApiPlatform\Metadata\Put,
  15.     ApiPlatform\Metadata\Delete,
  16.     ApiPlatform\Metadata\ApiFilter,
  17.     ApiPlatform\Doctrine\Orm\Filter\OrderFilter,
  18.     ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
  19. use App\Entity\Trait\IdTrait,
  20.     App\Entity\Trait\UuidTrait,
  21.     App\Entity\Trait\OrdStatTrait,
  22.     App\Entity\Trait\TimestampableTrait,
  23.     App\Entity\Trait\TranslatableTrait,
  24.     App\Entity\Trait\ImportIdTrait,
  25.     App\Entity\Trait\Languages\SiteLanguagesTrait,
  26.     App\Entity\Interface\TranslatableInterface,
  27.     App\Entity\Interface\OrdStatableInterface,
  28.     App\Entity\Interface\LanguageableInterface,
  29.     App\Entity\Interface\BlockableInterface,
  30.     App\Entity\Interface\BlockInterface,
  31.     App\Repository\NewsRepository,
  32.     App\Enum\Language;
  33. #[ApiResource(
  34.     description'News',
  35.     normalizationContext: ['groups' => [
  36.         'read',
  37.         'read:' self::class,
  38.         'read:' self::class . 'Translation',
  39.         'read:' AbstractThumb::class
  40.     ]],
  41.     denormalizationContext: ['groups' => ['write']],
  42.     security'is_granted("' News::class . '")',
  43.     order: ['ord' => 'desc'],
  44.     operations: [
  45.         new GetCollection(),
  46.         new Post(denormalizationContext: ['groups' => ['write',  'post']]),
  47.         new Get(),
  48.         new Put(),
  49.         new Delete(),
  50.     ],
  51.     extraProperties: ['standard_put' => false],
  52. )]
  53. #[ApiFilter(SearchFilter::class, properties: ['idName' => 'exact'])]
  54. #[ApiFilter(OrderFilter::class, properties: ['ord'])]
  55. #[ORM\Entity(repositoryClassNewsRepository::class)]
  56. class News implements
  57.     TranslatableInterface,
  58.     OrdStatableInterface,
  59.     LanguageableInterface,
  60.     BlockableInterface
  61. {
  62.     use IdTrait,
  63.         UuidTrait,
  64.         OrdStatTrait,
  65.         TimestampableTrait,
  66.         TranslatableTrait,
  67.         SiteLanguagesTrait,
  68.         ImportIdTrait;
  69.     #[ApiProperty(description'Working title')]
  70.     #[Groups(['read''write'])]
  71.     #[Assert\NotBlank]
  72.     #[ORM\Column(typeTypes::STRINGlength511uniquetrue)]
  73.     private string $workingTitle;
  74.     #[ApiProperty(description'Is UJ')]
  75.     #[Groups(['read:' self::class, 'write'])]
  76.     #[ORM\Column(typeTypes::BOOLEANoptions: ['default' => false])]
  77.     private bool $isUj false;
  78.     #[ApiProperty(description'Published at')]
  79.     #[Groups(['read:' self::class, 'write'])]
  80.     #[ORM\Column(typeTypes::DATE_IMMUTABLEoptions: ['default' => '1970-01-01'])]
  81.     private \DateTimeImmutable $publishedAt;
  82.     #[ApiProperty(description'Blocks'writableLinkfalse)]
  83.     #[ORM\OneToMany(
  84.         targetEntityNewsBlock::class,
  85.         mappedBy'parent',
  86.         cascade: ['persist''remove']
  87.     )]
  88.     #[ORM\OrderBy(['ord' => 'desc'])]
  89.     private Collection $blocks;
  90.     public function __construct(string $workingTitle, ?array $languages)
  91.     {
  92.         $this->setUuid();
  93.         $this->workingTitle $workingTitle;
  94.         $this->translations = new ArrayCollection();
  95.         $this->blocks = new ArrayCollection();
  96.         $this->setLanguages($languages ?? Language::DEFAULT_SITE);
  97.         $this->publishedAt = new \DateTimeImmutable();
  98.         $this->createdAt = new \DateTimeImmutable();
  99.         $this->updatedAt = new \DateTimeImmutable();
  100.     }
  101.     public function getWorkingTitle(): string
  102.     {
  103.         return $this->workingTitle;
  104.     }
  105.     public function setWorkingTitle(string $workingTitle): self
  106.     {
  107.         $this->workingTitle $workingTitle;
  108.         return $this;
  109.     }
  110.     public function getIsUj(): bool
  111.     {
  112.         return $this->isUj;
  113.     }
  114.     public function setIsUj(bool $isUj): self
  115.     {
  116.         $this->isUj $isUj;
  117.         return $this;
  118.     }
  119.     public function getPublishedAt(): \DateTimeImmutable
  120.     {
  121.         return $this->publishedAt;
  122.     }
  123.     public function setPublishedAt(\DateTimeImmutable $publishedAt): self
  124.     {
  125.         $this->publishedAt $publishedAt;
  126.         return $this;
  127.     }
  128.     /**
  129.      * @return Collection|NewsBlock[]
  130.      */
  131.     public function getBlocks(): Collection
  132.     {
  133.         return $this->blocks;
  134.     }
  135.     public function addBlock(BlockInterface $block): self
  136.     {
  137.         if ($this->blocks->contains($block)) {
  138.             return $this;
  139.         }
  140.         $this->blocks[] = $block;
  141.         return $this;
  142.     }
  143.     public function removeBlock(BlockInterface $block): self
  144.     {
  145.         $this->blocks->removeElement($block);
  146.         return $this;
  147.     }
  148.     public function resetBlocks(): self
  149.     {
  150.         $this->blocks = new ArrayCollection();
  151.         return $this;
  152.     }
  153. }