start_sf5_project_reboot/app/src/Entity/Post.php

227 lines
4.9 KiB
PHP

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Repository\PostRepository")
* @ORM\Table(name="symfony_demo_post")
* @UniqueEntity(fields={"slug"}, errorPath="title", message="post.slug_unique")
*
* Defines the properties of the Post entity to represent the blog posts.
*
* See https://symfony.com/doc/current/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/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
{
/**
* @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")
* @Assert\Length(max=255)
*/
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")
*/
private $publishedAt;
/**
* @var User
*
* @ORM\ManyToOne(targetEntity="App\Entity\User")
* @ORM\JoinColumn(nullable=false)
*/
private $author;
/**
* @var Comment[]|Collection
*
* @ORM\OneToMany(
* targetEntity="Comment",
* mappedBy="post",
* orphanRemoval=true,
* cascade={"persist"}
* )
* @ORM\OrderBy({"publishedAt": "DESC"})
*/
private $comments;
/**
* @var Tag[]|Collection
*
* @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 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 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
{
$this->comments->removeElement($comment);
}
public function getSummary(): ?string
{
return $this->summary;
}
public function setSummary(?string $summary): void
{
$this->summary = $summary;
}
public function addTag(Tag ...$tags): void
{
foreach ($tags as $tag) {
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;
}
}