src/Entity/VehicleCategory.php line 24

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