src/Entity/City.php line 33

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Gedmo\Timestampable\Traits\TimestampableEntity;
  9. use Symfony\Component\Serializer\Annotation\Groups;
  10. use Symfony\Component\Validator\Constraints as Assert;
  11. /**
  12.  * @ORM\Entity(repositoryClass="App\Repository\CityRepository")
  13.  */
  14. #[ApiResource(
  15.     attributes: ['security' => "is_granted('ROLE_USER')"],
  16.     normalizationContext: ['groups' => ['city:read']],
  17.     denormalizationContext: ['groups' => ['city:write']],
  18.     collectionOperations: [
  19.         'get',
  20.         'post' => [
  21.             'security' => "is_granted('ROLE_ADMIN')",
  22.         ],
  23.     ],
  24.     itemOperations: [
  25.         'get',
  26.         'put' => ['security' => "is_granted('ROLE_ADMIN') or object.owner == user"],
  27.     ],
  28. )]
  29. class City
  30. {
  31.     use TimestampableEntity;
  32.     /**
  33.      * @ORM\Id()
  34.      * @ORM\GeneratedValue()
  35.      * @ORM\Column(type="integer")
  36.      */
  37.     private $id;
  38.     /**
  39.      * @var string
  40.      *
  41.      * @ORM\Column(name="name", type="string", length=100)
  42.      * @Assert\NotBlank()
  43.      * @Assert\Length(max="100")
  44.      * @Groups({"city:read", "city:write"})
  45.      */
  46.     private $name;
  47.     /**
  48.      * @ORM\Column(type="boolean")
  49.      * @Assert\Type(type="boolean")
  50.      * @Groups({"city:read", "city:write"})
  51.      */
  52.     private $isActive;
  53.     /**
  54.      * @var Collection
  55.      *
  56.      * @ORM\ManyToMany(targetEntity="App\Entity\Company", mappedBy="cities")
  57.      * Ignore()
  58.      */
  59.     private $companies;
  60.     public function __construct()
  61.     {
  62.         $this->isActive true;
  63.         $this->createdAt = new \DateTime();
  64.         $this->updatedAt = new \DateTime();
  65.         $this->companies = new ArrayCollection();
  66.     }
  67.     /**
  68.      * @return string
  69.      */
  70.     public function __toString()
  71.     {
  72.         return $this->name;
  73.     }
  74.     public function getId(): ?int
  75.     {
  76.         return $this->id;
  77.     }
  78.     public function getName(): ?string
  79.     {
  80.         return $this->name;
  81.     }
  82.     public function setName(?string $name): self
  83.     {
  84.         $this->name $name;
  85.         return $this;
  86.     }
  87.     public function getIsActive(): bool
  88.     {
  89.         return $this->isActive;
  90.     }
  91.     public function setIsActive(bool $isActive): self
  92.     {
  93.         $this->isActive $isActive;
  94.         return $this;
  95.     }
  96.     /**
  97.      * @return Collection|Company[]
  98.      */
  99.     public function getCompanies(): Collection
  100.     {
  101.         return $this->companies;
  102.     }
  103.     public function addCompany(Company $company): self
  104.     {
  105.         if (!$this->companies->contains($company)) {
  106.             $this->companies[] = $company;
  107.         }
  108.         return $this;
  109.     }
  110.     public function removeCompany(Company $company): self
  111.     {
  112.         if ($this->companies->contains($company)) {
  113.             $this->companies->removeElement($company);
  114.         }
  115.         return $this;
  116.     }
  117. }