src/Entity/Category.php line 47

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiFilter;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
  6. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  7. use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
  8. use DateTime;
  9. use Doctrine\Common\Collections\ArrayCollection;
  10. use Doctrine\Common\Collections\Collection;
  11. use Doctrine\ORM\Mapping as ORM;
  12. use Gedmo\Timestampable\Traits\TimestampableEntity;
  13. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  14. use Symfony\Component\Serializer\Annotation\Groups;
  15. use Symfony\Component\Serializer\Annotation\Ignore;
  16. use Symfony\Component\Validator\Constraints as Assert;
  17. /**
  18.  * @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
  19.  * @ORM\HasLifecycleCallbacks()
  20.  * @UniqueEntity(
  21.  *     fields={"company", "title"},
  22.  *     errorPath="title",
  23.  *     message="This title is already in use on that company."
  24.  * )
  25.  */
  26. #[ApiResource(
  27.     attributes: ['security' => "is_granted('ROLE_USER')"],
  28.     normalizationContext: ['groups' => ['category:read']],
  29.     denormalizationContext: ['groups' => ['category:write']],
  30.     collectionOperations: [
  31.         'get',
  32.         'post' => [
  33.             'security' => "is_granted('ROLE_ADMIN')",
  34.         ],
  35.     ],
  36.     itemOperations: [
  37.         'get',
  38.         'put' => ['security' => "is_granted('ROLE_ADMIN') or object.owner == user"],
  39.     ],
  40. )]
  41. #[ApiFilter(SearchFilter::class, properties: ['id''title' => 'partial'])]
  42. #[ApiFilter(PropertyFilter::class)]
  43. #[ApiFilter(OrderFilter::class, properties: ['id''title''isActive''createdAt'], arguments: ['orderParameterName' => 'order'])]
  44. class Category
  45. {
  46.     use TimestampableEntity;
  47.     /**
  48.      * @ORM\Id()
  49.      * @ORM\GeneratedValue()
  50.      * @ORM\Column(type="integer")
  51.      * @Groups({"pinpointCampaign:list"})
  52.      */
  53.     private $id;
  54.     /**
  55.      * @var Company
  56.      *
  57.      * @ORM\ManyToOne(targetEntity="App\Entity\Company")
  58.      * @Ignore()
  59.      */
  60.     private $company;
  61.     /**
  62.      * @var string
  63.      *
  64.      * @ORM\Column(type="string", length=256)
  65.      * @Assert\Length(max=256)
  66.      * @Assert\NotBlank()
  67.      * @Groups({"category:read","pinpointCampaign:item", "category:write"})
  68.      */
  69.     private $title;
  70.     /**
  71.      * @var string
  72.      *
  73.      * @ORM\Column(type="text", nullable=true)
  74.      * @Groups({"category:read","pinpointCampaign:item", "category:write"})
  75.      */
  76.     private $description;
  77.     /**
  78.      * @ORM\Column(type="boolean")
  79.      * @Assert\Type(type="boolean")
  80.      * @Groups({"category:read", "category:write"})
  81.      */
  82.     private $isActive;
  83.     /**
  84.      * @var ArrayCollection
  85.      *
  86.      * @ORM\OneToMany(targetEntity="App\Entity\SurveyFormQuestion", mappedBy="category")
  87.      * @ORM\JoinColumn(name="id", referencedColumnName="category_id",onDelete="SET NULL")
  88.      * @Ignore()
  89.      */
  90.     private $surveyFormQuestions;
  91.     /**
  92.      * @var ArrayCollection
  93.      *
  94.      * @ORM\OneToMany(targetEntity="App\Entity\Recommendation", mappedBy="category")
  95.      * @ORM\JoinColumn(name="id", referencedColumnName="category_id", onDelete="SET NULL")
  96.      * @Ignore()
  97.      */
  98.     private $recommendations;
  99.     public function __construct()
  100.     {
  101.         $this->isActive true;
  102.         $this->createdAt = new \DateTime();
  103.         $this->updatedAt = new \DateTime();
  104.         $this->surveyFormQuestions = new ArrayCollection();
  105.         $this->recommendations = new ArrayCollection();
  106.     }
  107.     /**
  108.      * @return string
  109.      */
  110.     public function __toString()
  111.     {
  112.         return $this->title;
  113.     }
  114.     public function getId(): ?int
  115.     {
  116.         return $this->id;
  117.     }
  118.     public function getCompany(): ?Company
  119.     {
  120.         return $this->company;
  121.     }
  122.     public function setCompany(?Company $company): self
  123.     {
  124.         $this->company $company;
  125.         return $this;
  126.     }
  127.     public function getTitle(): ?string
  128.     {
  129.         return $this->title;
  130.     }
  131.     public function setTitle(?string $title): self
  132.     {
  133.         $this->title $title;
  134.         return $this;
  135.     }
  136.     public function getDescription(): ?string
  137.     {
  138.         return $this->description;
  139.     }
  140.     public function setDescription(?string $description): self
  141.     {
  142.         $this->description $description;
  143.         return $this;
  144.     }
  145.     public function getIsActive(): bool
  146.     {
  147.         return $this->isActive;
  148.     }
  149.     public function setIsActive(bool $isActive): self
  150.     {
  151.         $this->isActive $isActive;
  152.         return $this;
  153.     }
  154.     public function getCreatedAt(): ?DateTime
  155.     {
  156.         return $this->createdAt;
  157.     }
  158.     public function setCreatedAt(?DateTime $createdAt): self
  159.     {
  160.         $this->createdAt $createdAt;
  161.         return $this;
  162.     }
  163.     public function getUpdatedAt(): ?DateTime
  164.     {
  165.         return $this->updatedAt;
  166.     }
  167.     public function setUpdatedAt(?DateTime $updatedAt): self
  168.     {
  169.         $this->updatedAt $updatedAt;
  170.         return $this;
  171.     }
  172.     /**
  173.      * @return Collection|SurveyFormQuestion[]
  174.      */
  175.     public function getSurveyFormQuestions(): Collection
  176.     {
  177.         return $this->surveyFormQuestions;
  178.     }
  179.     public function addSurveyFormQuestion(SurveyFormQuestion $surveyFormQuestion): self
  180.     {
  181.         if (!$this->surveyFormQuestions->contains($surveyFormQuestion)) {
  182.             $this->surveyFormQuestions[] = $surveyFormQuestion;
  183.             $surveyFormQuestion->setCategory($this);
  184.         }
  185.         return $this;
  186.     }
  187.     public function removeSurveyFormQuestion(SurveyFormQuestion $surveyFormQuestion): self
  188.     {
  189.         if ($this->surveyFormQuestions->contains($surveyFormQuestion)) {
  190.             $this->surveyFormQuestions->removeElement($surveyFormQuestion);
  191.             // set the owning side to null (unless already changed)
  192.             if ($surveyFormQuestion->getCategory() === $this) {
  193.                 $surveyFormQuestion->setCategory(null);
  194.             }
  195.         }
  196.         return $this;
  197.     }
  198.     /**
  199.      * @return Collection|Recommendation[]
  200.      */
  201.     public function getRecommendations(): Collection
  202.     {
  203.         return $this->surveyFormQuestions;
  204.     }
  205.     public function addRecommendation(Recommendation $recommendation): self
  206.     {
  207.         if (!$this->recommendations->contains($recommendation)) {
  208.             $this->recommendations[] = $recommendation;
  209.             $recommendation->setCategory($this);
  210.         }
  211.         return $this;
  212.     }
  213.     public function removeRecommendation(Recommendation $recommendation): self
  214.     {
  215.         if ($this->recommendations->contains($recommendation)) {
  216.             $this->recommendations->removeElement($recommendation);
  217.             // set the owning side to null (unless already changed)
  218.             if ($recommendation->getCategory() === $this) {
  219.                 $recommendation->setCategory(null);
  220.             }
  221.         }
  222.         return $this;
  223.     }
  224. }