src/Entity/Position.php line 37

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