src/Entity/VehicleKind.php line 29

  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Doctrine\Orm\Filter\BooleanFilter;
  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 App\Repository\VehicleKindRepository;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use Doctrine\Common\Collections\Collection;
  12. use Doctrine\ORM\Mapping as ORM;
  13. use Symfony\Component\Serializer\Annotation\Groups;
  14. use Symfony\Component\String\Slugger\AsciiSlugger;
  15. #[ApiResource(
  16.     operations: [
  17.         new Get(),
  18.         new GetCollection()
  19.     ],
  20.     normalizationContext: ['groups' => ['kind:read']],
  21.     security"is_granted('PUBLIC_ACCESS')"
  22. )]
  23. #[ApiFilter(BooleanFilter::class, properties: ['vehicles.visible'])]
  24. #[ORM\Entity(repositoryClassVehicleKindRepository::class)]
  25. class VehicleKind
  26. {
  27.     #[ORM\Id]
  28.     #[ORM\GeneratedValue]
  29.     #[ORM\Column]
  30.     #[Groups(['vehicle:read''kind:read'])]
  31.     private ?int $id null;
  32.     #[Groups(['vehicle:read''kind:read'])]
  33.     #[ORM\Column(length255)]
  34.     private ?string $name null;
  35.     #[Groups(['vehicle:read''kind:read'])]
  36.     #[ORM\Column(length255uniquetrue)]
  37.     private ?string $url null;
  38.     #[ORM\OneToMany(mappedBy'kind'targetEntityVehicle::class)]
  39.     private Collection $vehicles;
  40.     public function __construct()
  41.     {
  42.         $this->vehicles = new ArrayCollection();
  43.     }
  44.     public function getUrl(): ?string
  45.     {
  46.         return $this->url;
  47.     }
  48.     public function setUrl(string $url): self
  49.     {
  50.         $slugger = new AsciiSlugger('cs');
  51.         $slug =   $slugger->slug($url'-')->lower();
  52.         $this->url $slug->toString();
  53.         return $this;
  54.     }
  55.     public function getId(): ?int
  56.     {
  57.         return $this->id;
  58.     }
  59.     public function getName(): ?string
  60.     {
  61.         return $this->name;
  62.     }
  63.     public function setName(string $name): self
  64.     {
  65.         $this->name $name;
  66.         $this->setUrl($name);
  67.         return $this;
  68.     }
  69.     /**
  70.      * @return Collection<int, Vehicle>
  71.      */
  72.     public function getVehicles(): Collection
  73.     {
  74.         return $this->vehicles;
  75.     }
  76.     public function addVehicle(Vehicle $vehicle): self
  77.     {
  78.         if (!$this->vehicles->contains($vehicle)) {
  79.             $this->vehicles->add($vehicle);
  80.             $vehicle->setKind($this);
  81.         }
  82.         return $this;
  83.     }
  84.     public function removeVehicle(Vehicle $vehicle): self
  85.     {
  86.         if ($this->vehicles->removeElement($vehicle)) {
  87.             // set the owning side to null (unless already changed)
  88.             if ($vehicle->getKind() === $this) {
  89.                 $vehicle->setKind(null);
  90.             }
  91.         }
  92.         return $this;
  93.     }
  94. }