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