<?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 Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Repository\PageRepository")
* @ORM\HasLifecycleCallbacks()
*/
#[ApiResource(
collectionOperations: ['get', 'post' => ['security' => "is_granted('ROLE_ADMIN')"]],
itemOperations: ['get', 'put' => ['security' => "is_granted('ROLE_ADMIN') or object.owner == user"], 'delete' => ['security' => "is_granted('ROLE_ADMIN')"]],
)]
#[ApiFilter(PropertyFilter::class)]
#[ApiFilter(OrderFilter::class, properties: ['id', 'pageTitle', 'isActive', 'createdAt'], arguments: ['orderParameterName' => 'order'])]
class Page
{
use TimestampableEntity;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @Groups({"data"})
* @ORM\Column(type="text", nullable=true)
*/
#[ApiFilter(SearchFilter::class, strategy: SearchFilter::STRATEGY_PARTIAL)]
protected $pageTitle;
/**
* @Groups({"data"})
* @ORM\Column(type="string", length=255)
* @Gedmo\Slug(fields={"pageTitle"})
*/
#[ApiFilter(SearchFilter::class, strategy: SearchFilter::STRATEGY_EXACT)]
private $slug;
/**
* @Groups({"data"})
* @ORM\OneToMany(targetEntity="App\Entity\PageSection", cascade={"persist"}, mappedBy="page", orphanRemoval=true)
* @ORM\OrderBy({"position" = "ASC"})
* @Assert\Valid()
*/
private $pageSections;
public function __construct()
{
$this->pageSections = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
/**
* @return Collection|PageSection[]
*/
public function getPageSections(): Collection
{
return $this->pageSections;
}
public function addPageSection(PageSection $pageSection): self
{
if (!$this->pageSections->contains($pageSection)) {
$this->pageSections[] = $pageSection;
$pageSection->setPage($this);
}
return $this;
}
public function removePageSection(PageSection $pageSection): self
{
if ($this->pageSections->contains($pageSection)) {
$this->pageSections->removeElement($pageSection);
// set the owning side to null (unless already changed)
if ($pageSection->getPage() === $this) {
$pageSection->setPage(null);
}
}
return $this;
}
public function getPageTitle(): ?string
{
return $this->pageTitle;
}
public function setPageTitle(?string $pageTitle): self
{
$this->pageTitle = $pageTitle;
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$this->slug = $slug;
return $this;
}
}