Spookie/src/Entity/Pages.php

153 lines
3.2 KiB
PHP

<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use App\Repository\PagesRepository;
use DateTimeImmutable;
use Doctrine\ORM\Mapping as ORM;
use Sunrise\Slugger\Slugger;
#[ORM\Entity(repositoryClass: PagesRepository::class), ORM\HasLifecycleCallbacks]
#[ApiResource]
#[ApiFilter(filterClass: SearchFilter::class, properties: ['slug' => 'exact'])]
class Pages
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[ORM\Column(type: 'string', length: 255)]
private $name;
#[ORM\Column(type: 'text')]
private $content;
#[ORM\Column(type: 'datetime_immutable')]
private $createdAt;
#[ORM\Column(type: 'datetime_immutable', nullable: true)]
private $modifiedAt;
#[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'pages')]
#[ORM\JoinColumn(nullable: false)]
private $owner;
#[ORM\Column(type: 'string', length: 255)]
private $slug;
/**
* @param $id
*/
/* public function __construct(String $name = '', String $content = '')
{
$this->name = $name;
$this->content = $content;
$owner = $userRepository->findOneBy(['username' => 'tracer']);
$this->owner = $owner;
}
*/
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getContent(): ?string
{
return $this->content;
}
public function setContent(string $content): self
{
$this->content = $content;
return $this;
}
public function getCreatedAt(): ?DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(DateTimeImmutable $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getModifiedAt(): ?DateTimeImmutable
{
return $this->modifiedAt;
}
public function setModifiedAt(?DateTimeImmutable $modifiedAt): self
{
$this->modifiedAt = $modifiedAt;
return $this;
}
public function getOwner(): ?User
{
return $this->owner;
}
public function setOwner(?User $owner): self
{
$this->owner = $owner;
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$this->slug = $slug;
return $this;
}
#[ORM\PrePersist]
public function onPrePersist(): void
{
$slugger = new Slugger();
$slug = $slugger->slugify(string: $this->name);
$this->slug = $slug;
$this->createdAt = new DateTimeImmutable(datetime: 'now');
}
#[ORM\PreUpdate]
public function onPreUpdate(): void
{
$slugger = new Slugger();
$slug = $slugger->slugify(string: $this->name);
$this->slug = $slug;
$this->modifiedAt = new DateTimeImmutable(datetime: 'now');
}
}