src/Entity/Department.php line 37

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