src/Entity/Division.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiResource;
  4. use DateTime;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Symfony\Component\Serializer\Annotation\Groups;
  9. use Symfony\Component\Serializer\Annotation\Ignore;
  10. use Symfony\Component\Validator\Constraints as Assert;
  11. /**
  12.  * @ORM\Entity(repositoryClass="App\Repository\DivisionRepository")
  13.  */
  14. #[ApiResource(
  15.     attributes: ['security' => "is_granted('ROLE_USER')"],
  16.     normalizationContext: ['groups' => ['division:read']],
  17.     denormalizationContext: ['groups' => ['division: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 Division
  30. {
  31.     /**
  32.      * @ORM\Id()
  33.      * @ORM\GeneratedValue()
  34.      * @ORM\Column(type="integer")
  35.      */
  36.     private $id;
  37.     /**
  38.      * @ORM\Column(name="name", type="string", length=100)
  39.      * @Assert\NotBlank()
  40.      * @Assert\Length(max="100")
  41.      * @Groups({"division:read", "division:write"})
  42.      */
  43.     private $name;
  44.     /**
  45.      * @ORM\Column(type="boolean")
  46.      * @Assert\Type(type="boolean")
  47.      * @Groups({"division:read", "division:write"})
  48.      */
  49.     private $isActive;
  50.     /**
  51.      * @var DateTime
  52.      *
  53.      * @ORM\Column(type="datetime", nullable=false)
  54.      */
  55.     private $createdAt;
  56.     /**
  57.      * @var DateTime
  58.      *
  59.      * @ORM\Column(type="datetime", nullable=true)
  60.      */
  61.     private $updatedAt;
  62.     /**
  63.      * @var Collection
  64.      *
  65.      * @ORM\ManyToMany(targetEntity="App\Entity\Company", mappedBy="divisions")
  66.      * Ignore()
  67.      */
  68.     private $companies;
  69.     public function __construct()
  70.     {
  71.         $this->isActive true;
  72.         $this->createdAt = new \DateTime();
  73.         $this->updatedAt = new \DateTime();
  74.         $this->companies = new ArrayCollection();
  75.     }
  76.     /**
  77.      * @return string
  78.      */
  79.     public function __toString()
  80.     {
  81.         return $this->name;
  82.     }
  83.     public function getId(): ?int
  84.     {
  85.         return $this->id;
  86.     }
  87.     public function getName(): ?string
  88.     {
  89.         return $this->name;
  90.     }
  91.     public function setName(?string $name): self
  92.     {
  93.         $this->name $name;
  94.         return $this;
  95.     }
  96.     public function getIsActive(): bool
  97.     {
  98.         return $this->isActive;
  99.     }
  100.     public function setIsActive(bool $isActive): self
  101.     {
  102.         $this->isActive $isActive;
  103.         return $this;
  104.     }
  105.     public function getCreatedAt(): ?DateTime
  106.     {
  107.         return $this->createdAt;
  108.     }
  109.     public function setCreatedAt(?DateTime $createdAt): self
  110.     {
  111.         $this->createdAt $createdAt;
  112.         return $this;
  113.     }
  114.     public function getUpdatedAt(): ?DateTime
  115.     {
  116.         return $this->updatedAt;
  117.     }
  118.     public function setUpdatedAt(?DateTime $updatedAt): self
  119.     {
  120.         $this->updatedAt $updatedAt;
  121.         return $this;
  122.     }
  123.     /**
  124.      * @return Collection|Company[]
  125.      */
  126.     public function getCompanies(): Collection
  127.     {
  128.         return $this->companies;
  129.     }
  130.     public function addCompany(Company $company): self
  131.     {
  132.         if (!$this->companies->contains($company)) {
  133.             $this->companies[] = $company;
  134.         }
  135.         return $this;
  136.     }
  137.     public function removeCompany(Company $company): self
  138.     {
  139.         if ($this->companies->contains($company)) {
  140.             $this->companies->removeElement($company);
  141.         }
  142.         return $this;
  143.     }
  144. }