src/Entity/NewsCategory.php line 52

  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
  4. use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
  5. use ApiPlatform\Metadata\ApiFilter;
  6. use ApiPlatform\Metadata\ApiResource;
  7. use ApiPlatform\Metadata\Get;
  8. use ApiPlatform\Metadata\GetCollection;
  9. use ApiPlatform\Serializer\Filter\PropertyFilter;
  10. use App\Repository\NewsCategoryRepository;
  11. use DateTimeImmutable;
  12. use Doctrine\Common\Collections\ArrayCollection;
  13. use Doctrine\Common\Collections\Collection;
  14. use Doctrine\DBAL\Types\Types;
  15. use Doctrine\ORM\Mapping as ORM;
  16. use Symfony\Component\Serializer\Annotation\Groups;
  17. #[ORM\Entity(repositoryClassNewsCategoryRepository::class)]
  18. #[ApiFilter(PropertyFilter::class)]
  19. #[ApiResource(
  20.     shortName'News',
  21.     description'News API',
  22.     operations: [
  23.         new Get(uriTemplate'/news-category/{id}'),
  24.         //new Patch(uriTemplate: '/media/{id}'),
  25.         new GetCollection(uriTemplate'/news->category'paginationEnabledfalse),
  26.     ],
  27.     formats: [
  28.         'jsonld',
  29.         'json',
  30.         'jsonhal',
  31.         'csv' => 'text/csv',
  32.     ],
  33.     normalizationContext: [
  34.         'groups' => ['news-category:read'],
  35.     ],
  36.     denormalizationContext: [
  37.         'groups' => ['news-category:write'],
  38.     ],
  39.     // paginationItemsPerPage: 10,
  40. ),
  41.     ApiFilter(
  42.         OrderFilter::class,
  43.         properties: [
  44.             'position',
  45.         ],
  46.     ),
  47. ]
  48. class NewsCategory
  49. {
  50.     #[ORM\Id]
  51.     #[ORM\GeneratedValue]
  52.     #[ORM\Column]
  53.     #[Groups(['news:read''news-category:read''studio:read'])]
  54.     private ?int $id null;
  55.     #[ORM\Column(length255)]
  56.     #[Groups(['news:read''news-category:read''studio:read'])]
  57.     #[ApiFilter(SearchFilter::class, strategy'exact')]
  58.     private ?string $bezeichnung null;
  59.     #[ORM\Column(typeTypes::TEXTnullabletrue)]
  60.     #[Groups(['news:read''news-category:read''studio:read'])]
  61.     private ?string $beschreibung null;
  62.     #[ORM\Column]
  63.     #[Groups(['news:read''news-category:read''studio:read'])]
  64.     private ?int $position null;
  65.     #[ORM\OneToMany(mappedBy'category'targetEntityNews::class, cascade: ['persist''remove'])]
  66.     #[ApiFilter(SearchFilter::class, strategy'exact')]
  67.     protected Collection $news;
  68.     #[ORM\Column]
  69.     private ?DateTimeImmutable $createdAt null;
  70.     public function __construct()
  71.     {
  72.         $this->createdAt = new DateTimeImmutable();
  73.         $this->position 0;
  74.         $this->news = new ArrayCollection();
  75.     }
  76.     public function getId(): ?int
  77.     {
  78.         return $this->id;
  79.     }
  80.     public function getBezeichnung(): ?string
  81.     {
  82.         return $this->bezeichnung;
  83.     }
  84.     public function setBezeichnung(string $bezeichnung): self
  85.     {
  86.         $this->bezeichnung $bezeichnung;
  87.         return $this;
  88.     }
  89.     public function getBeschreibung(): ?string
  90.     {
  91.         return $this->beschreibung;
  92.     }
  93.     public function setBeschreibung(?string $beschreibung): self
  94.     {
  95.         $this->beschreibung $beschreibung;
  96.         return $this;
  97.     }
  98.     public function getPosition(): ?int
  99.     {
  100.         return $this->position;
  101.     }
  102.     public function setPosition(int $position): self
  103.     {
  104.         $this->position $position;
  105.         return $this;
  106.     }
  107.     public function getCreatedAt(): ?DateTimeImmutable
  108.     {
  109.         return $this->createdAt;
  110.     }
  111.     public function setCreatedAt(DateTimeImmutable $createdAt): self
  112.     {
  113.         $this->createdAt $createdAt;
  114.         return $this;
  115.     }
  116.     /**
  117.      * @return Collection<int, News>
  118.      */
  119.     public function getNews(): Collection
  120.     {
  121.         return $this->news;
  122.     }
  123.     public function addNews(News $news): self
  124.     {
  125.         if (!$this->news->contains($news)) {
  126.             $this->news->add($news);
  127.             $news->setCategory($this);
  128.         }
  129.         return $this;
  130.     }
  131.     public function removeNews(News $news): self
  132.     {
  133.         if ($this->news->removeElement($news)) {
  134.             // set the owning side to null (unless already changed)
  135.             if ($news->getCategory() === $this) {
  136.                 $news->setCategory(null);
  137.             }
  138.         }
  139.         return $this;
  140.     }
  141. }