src/Entity/VehicleModel.php line 30

  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\VehicleModelRepository;
  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' => ['model:read']],
  21.     security"is_granted('PUBLIC_ACCESS')"
  22. )]
  23. #[ApiFilter(BooleanFilter::class, properties: ['vehicles.visible'])]
  24. #[ApiFilter(SearchFilter::class, properties: ['vehicles.manufacturer' => 'exact''vehicles.manufacturer.url' => 'exact''vehicles.category.url' => 'exact''vehicles.kind' => 'exact'  ])]
  25. #[ORM\Entity(repositoryClassVehicleModelRepository::class)]
  26. class VehicleModel
  27. {
  28.     #[ORM\Id]
  29.     #[ORM\GeneratedValue]
  30.     #[ORM\Column]
  31.     #[Groups(['vehicle:read''model:read'])]
  32.     private ?int $id null;
  33.     #[Groups(['vehicle:read''model:read'])]
  34.     #[ORM\Column(length255)]
  35.     private ?string $name null;
  36.     #[ORM\OneToMany(mappedBy'model'targetEntityVehicle::class)]
  37.     private Collection $vehicles;
  38.     #[Groups(['vehicle:read''model:read'])]
  39.     #[ORM\Column(length255)]
  40.     private ?string $url null;
  41.     #[ORM\OneToMany(mappedBy'vehicleModel'targetEntityBlog::class)]
  42.     private Collection $blogs;
  43.     #[ORM\ManyToOne(inversedBy'vehicleModels')]
  44.     private ?VehicleManufacturer $vehicleManufacturer null;
  45.     public function __construct()
  46.     {
  47.         $this->vehicles = new ArrayCollection();
  48.         $this->blogs = new ArrayCollection();
  49.     }
  50.     public function getId(): ?int
  51.     {
  52.         return $this->id;
  53.     }
  54.     public function getName(): ?string
  55.     {
  56.         return $this->name;
  57.     }
  58.     public function setName(string $name): self
  59.     {
  60.         $this->name $name;
  61.         $this->setUrl($this->name);
  62.         return $this;
  63.     }
  64.     /**
  65.      * @return Collection<int, Vehicle>
  66.      */
  67.     public function getVehicles(): Collection
  68.     {
  69.         return $this->vehicles;
  70.     }
  71.     public function addVehicle(Vehicle $vehicle): self
  72.     {
  73.         if (!$this->vehicles->contains($vehicle)) {
  74.             $this->vehicles->add($vehicle);
  75.             $vehicle->setModel($this);
  76.         }
  77.         return $this;
  78.     }
  79.     public function removeVehicle(Vehicle $vehicle): self
  80.     {
  81.         if ($this->vehicles->removeElement($vehicle)) {
  82.             // set the owning side to null (unless already changed)
  83.             if ($vehicle->getModel() === $this) {
  84.                 $vehicle->setModel(null);
  85.             }
  86.         }
  87.         return $this;
  88.     }
  89.     public function getUrl(): ?string
  90.     {
  91.         return $this->url;
  92.     }
  93.     public function setUrl(string $url): self
  94.     {
  95.         $slugger = new AsciiSlugger('cs');
  96.         $slug =   $slugger->slug($url'-')->lower();
  97.         $this->url $slug->toString();
  98.         return $this;
  99.     }
  100.     /**
  101.      * @return Collection<int, Blog>
  102.      */
  103.     public function getBlogs(): Collection
  104.     {
  105.         return $this->blogs;
  106.     }
  107.     public function addBlog(Blog $blog): self
  108.     {
  109.         if (!$this->blogs->contains($blog)) {
  110.             $this->blogs->add($blog);
  111.             $blog->setVehicleModel($this);
  112.         }
  113.         return $this;
  114.     }
  115.     public function removeBlog(Blog $blog): self
  116.     {
  117.         if ($this->blogs->removeElement($blog)) {
  118.             // set the owning side to null (unless already changed)
  119.             if ($blog->getVehicleModel() === $this) {
  120.                 $blog->setVehicleModel(null);
  121.             }
  122.         }
  123.         return $this;
  124.     }
  125.     public function getVehicleManufacturer(): ?VehicleManufacturer
  126.     {
  127.         return $this->vehicleManufacturer;
  128.     }
  129.     public function setVehicleManufacturer(?VehicleManufacturer $vehicleManufacturer): self
  130.     {
  131.         $this->vehicleManufacturer $vehicleManufacturer;
  132.         return $this;
  133.     }
  134. }