<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\Ignore;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
* @ORM\HasLifecycleCallbacks()
* @UniqueEntity(
* fields={"company", "title"},
* errorPath="title",
* message="This title is already in use on that company."
* )
*/
#[ApiResource(
attributes: ['security' => "is_granted('ROLE_USER')"],
normalizationContext: ['groups' => ['category:read']],
denormalizationContext: ['groups' => ['category:write']],
collectionOperations: [
'get',
'post' => [
'security' => "is_granted('ROLE_ADMIN')",
],
],
itemOperations: [
'get',
'put' => ['security' => "is_granted('ROLE_ADMIN') or object.owner == user"],
],
)]
#[ApiFilter(SearchFilter::class, properties: ['id', 'title' => 'partial'])]
#[ApiFilter(PropertyFilter::class)]
#[ApiFilter(OrderFilter::class, properties: ['id', 'title', 'isActive', 'createdAt'], arguments: ['orderParameterName' => 'order'])]
class Category
{
use TimestampableEntity;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
* @Groups({"pinpointCampaign:list"})
*/
private $id;
/**
* @var Company
*
* @ORM\ManyToOne(targetEntity="App\Entity\Company")
* @Ignore()
*/
private $company;
/**
* @var string
*
* @ORM\Column(type="string", length=256)
* @Assert\Length(max=256)
* @Assert\NotBlank()
* @Groups({"category:read","pinpointCampaign:item", "category:write"})
*/
private $title;
/**
* @var string
*
* @ORM\Column(type="text", nullable=true)
* @Groups({"category:read","pinpointCampaign:item", "category:write"})
*/
private $description;
/**
* @ORM\Column(type="boolean")
* @Assert\Type(type="boolean")
* @Groups({"category:read", "category:write"})
*/
private $isActive;
/**
* @var ArrayCollection
*
* @ORM\OneToMany(targetEntity="App\Entity\SurveyFormQuestion", mappedBy="category")
* @ORM\JoinColumn(name="id", referencedColumnName="category_id",onDelete="SET NULL")
* @Ignore()
*/
private $surveyFormQuestions;
/**
* @var ArrayCollection
*
* @ORM\OneToMany(targetEntity="App\Entity\Recommendation", mappedBy="category")
* @ORM\JoinColumn(name="id", referencedColumnName="category_id", onDelete="SET NULL")
* @Ignore()
*/
private $recommendations;
public function __construct()
{
$this->isActive = true;
$this->createdAt = new \DateTime();
$this->updatedAt = new \DateTime();
$this->surveyFormQuestions = new ArrayCollection();
$this->recommendations = new ArrayCollection();
}
/**
* @return string
*/
public function __toString()
{
return $this->title;
}
public function getId(): ?int
{
return $this->id;
}
public function getCompany(): ?Company
{
return $this->company;
}
public function setCompany(?Company $company): self
{
$this->company = $company;
return $this;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(?string $title): self
{
$this->title = $title;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getIsActive(): bool
{
return $this->isActive;
}
public function setIsActive(bool $isActive): self
{
$this->isActive = $isActive;
return $this;
}
public function getCreatedAt(): ?DateTime
{
return $this->createdAt;
}
public function setCreatedAt(?DateTime $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?DateTime
{
return $this->updatedAt;
}
public function setUpdatedAt(?DateTime $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* @return Collection|SurveyFormQuestion[]
*/
public function getSurveyFormQuestions(): Collection
{
return $this->surveyFormQuestions;
}
public function addSurveyFormQuestion(SurveyFormQuestion $surveyFormQuestion): self
{
if (!$this->surveyFormQuestions->contains($surveyFormQuestion)) {
$this->surveyFormQuestions[] = $surveyFormQuestion;
$surveyFormQuestion->setCategory($this);
}
return $this;
}
public function removeSurveyFormQuestion(SurveyFormQuestion $surveyFormQuestion): self
{
if ($this->surveyFormQuestions->contains($surveyFormQuestion)) {
$this->surveyFormQuestions->removeElement($surveyFormQuestion);
// set the owning side to null (unless already changed)
if ($surveyFormQuestion->getCategory() === $this) {
$surveyFormQuestion->setCategory(null);
}
}
return $this;
}
/**
* @return Collection|Recommendation[]
*/
public function getRecommendations(): Collection
{
return $this->surveyFormQuestions;
}
public function addRecommendation(Recommendation $recommendation): self
{
if (!$this->recommendations->contains($recommendation)) {
$this->recommendations[] = $recommendation;
$recommendation->setCategory($this);
}
return $this;
}
public function removeRecommendation(Recommendation $recommendation): self
{
if ($this->recommendations->contains($recommendation)) {
$this->recommendations->removeElement($recommendation);
// set the owning side to null (unless already changed)
if ($recommendation->getCategory() === $this) {
$recommendation->setCategory(null);
}
}
return $this;
}
}