Last active
June 28, 2023 23:33
-
-
Save julienbourdeau/3d17304951028cf370ed5fe95d104911 to your computer and use it in GitHub Desktop.
Symfony Post entity for Algolia's documentation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* This file is part of the Symfony/demo project | |
* https://github.com/symfony/demo | |
* | |
*/ | |
namespace App\Entity; | |
use Doctrine\Common\Collections\ArrayCollection; | |
use Doctrine\Common\Collections\Collection; | |
use Doctrine\ORM\Mapping as ORM; | |
use Symfony\Component\Serializer\Normalizer\scalar; | |
use Symfony\Component\Serializer\SerializerInterface; | |
use Symfony\Component\Validator\Constraints as Assert; | |
/** | |
* @ORM\Entity(repositoryClass="App\Repository\PostRepository") | |
* @ORM\Table(name="symfony_demo_post") | |
* | |
* Defines the properties of the Post entity to represent the blog posts. | |
* | |
* See https://symfony.com/doc/current/book/doctrine.html#creating-an-entity-class | |
* | |
* Tip: if you have an existing database, you can generate these entity class automatically. | |
* See https://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html | |
* | |
* @author Ryan Weaver <weaverryan@gmail.com> | |
* @author Javier Eguiluz <javier.eguiluz@gmail.com> | |
* @author Yonel Ceruto <yonelceruto@gmail.com> | |
*/ | |
class Post | |
{ | |
/** | |
* Use constants to define configuration options that rarely change instead | |
* of specifying them in app/config/config.yml. | |
* | |
* See https://symfony.com/doc/current/best_practices/configuration.html#constants-vs-configuration-options | |
*/ | |
const NUM_ITEMS = 10; | |
/** | |
* @var int | |
* | |
* @ORM\Id | |
* @ORM\GeneratedValue | |
* @ORM\Column(type="integer") | |
*/ | |
private $id; | |
/** | |
* @var string | |
* | |
* @ORM\Column(type="string") | |
* @Assert\NotBlank | |
*/ | |
private $title; | |
/** | |
* @var string | |
* | |
* @ORM\Column(type="string") | |
*/ | |
private $slug; | |
/** | |
* @var string | |
* | |
* @ORM\Column(type="string") | |
* @Assert\NotBlank(message="post.blank_summary") | |
*/ | |
private $summary; | |
/** | |
* @var string | |
* | |
* @ORM\Column(type="text") | |
* @Assert\NotBlank(message="post.blank_content") | |
* @Assert\Length(min=10, minMessage="post.too_short_content") | |
*/ | |
private $content; | |
/** | |
* @var \DateTime | |
* | |
* @ORM\Column(type="datetime") | |
* @Assert\DateTime | |
*/ | |
private $publishedAt; | |
/** | |
* @var User | |
* | |
* @ORM\ManyToOne(targetEntity="App\Entity\User") | |
* @ORM\JoinColumn(nullable=false) | |
*/ | |
private $author; | |
/** | |
* @var Comment[]|ArrayCollection | |
* | |
* @ORM\OneToMany( | |
* targetEntity="Comment", | |
* mappedBy="post", | |
* orphanRemoval=true | |
* ) | |
* @ORM\OrderBy({"publishedAt": "DESC"}) | |
*/ | |
private $comments; | |
/** | |
* @var Tag[]|ArrayCollection | |
* | |
* @ORM\ManyToMany(targetEntity="App\Entity\Tag", cascade={"persist"}) | |
* @ORM\JoinTable(name="symfony_demo_post_tag") | |
* @ORM\OrderBy({"name": "ASC"}) | |
* @Assert\Count(max="4", maxMessage="post.too_many_tags") | |
*/ | |
private $tags; | |
public function __construct() | |
{ | |
$this->publishedAt = new \DateTime(); | |
$this->comments = new ArrayCollection(); | |
$this->tags = new ArrayCollection(); | |
} | |
public function getId(): int | |
{ | |
return $this->id; | |
} | |
public function getTitle(): ?string | |
{ | |
return $this->title; | |
} | |
public function setTitle(string $title): void | |
{ | |
$this->title = $title; | |
} | |
public function getSlug(): ?string | |
{ | |
return $this->slug; | |
} | |
public function getCommentCount(): ?int | |
{ | |
return count($this->comments); | |
} | |
public function setSlug(string $slug): void | |
{ | |
$this->slug = $slug; | |
} | |
public function getContent(): ?string | |
{ | |
return $this->content; | |
} | |
public function setContent(string $content): void | |
{ | |
$this->content = $content; | |
} | |
public function isPublished(): bool | |
{ | |
return true; | |
} | |
public function getPublishedAt(): \DateTime | |
{ | |
return $this->publishedAt; | |
} | |
public function setPublishedAt(\DateTime $publishedAt): void | |
{ | |
$this->publishedAt = $publishedAt; | |
} | |
public function getAuthor(): User | |
{ | |
return $this->author; | |
} | |
public function setAuthor(User $author): void | |
{ | |
$this->author = $author; | |
} | |
public function getComments(): Collection | |
{ | |
return $this->comments; | |
} | |
public function addComment(Comment $comment): void | |
{ | |
$comment->setPost($this); | |
if (!$this->comments->contains($comment)) { | |
$this->comments->add($comment); | |
} | |
} | |
public function removeComment(Comment $comment): void | |
{ | |
$comment->setPost(null); | |
$this->comments->removeElement($comment); | |
} | |
public function getSummary(): ?string | |
{ | |
return $this->summary; | |
} | |
public function setSummary(string $summary): void | |
{ | |
$this->summary = $summary; | |
} | |
public function addTag(Tag $tag): void | |
{ | |
if (!$this->tags->contains($tag)) { | |
$this->tags->add($tag); | |
} | |
} | |
public function removeTag(Tag $tag): void | |
{ | |
$this->tags->removeElement($tag); | |
} | |
public function getTags(): Collection | |
{ | |
return $this->tags; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment