src/Entity/VehicleBody.php line 23

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