src/Entity/Comment.php line 31

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace App\Entity;
  11. use Doctrine\ORM\Mapping as ORM;
  12. use function Symfony\Component\String\u;
  13. use Symfony\Component\Validator\Constraints as Assert;
  14. /**
  15.  * @ORM\Entity
  16.  * @ORM\Table(name="symfony_demo_comment")
  17.  *
  18.  * Defines the properties of the Comment entity to represent the blog comments.
  19.  * See https://symfony.com/doc/current/doctrine.html#creating-an-entity-class
  20.  *
  21.  * Tip: if you have an existing database, you can generate these entity class automatically.
  22.  * See https://symfony.com/doc/current/doctrine/reverse_engineering.html
  23.  *
  24.  * @author Ryan Weaver <weaverryan@gmail.com>
  25.  * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  26.  */
  27. class Comment
  28. {
  29.     /**
  30.      * @ORM\Id
  31.      * @ORM\GeneratedValue
  32.      * @ORM\Column(type="integer")
  33.      */
  34.     private ?int $id null;
  35.     /**
  36.      * @ORM\ManyToOne(targetEntity="Post", inversedBy="comments")
  37.      * @ORM\JoinColumn(nullable=false)
  38.      */
  39.     private ?Post $post null;
  40.     /**
  41.      * @ORM\Column(type="text")
  42.      */
  43.     #[
  44.         Assert\NotBlank(message'comment.blank'),
  45.         Assert\Length(
  46.             min5,
  47.             minMessage'comment.too_short',
  48.             max10000,
  49.             maxMessage'comment.too_long',
  50.         )
  51.     ]
  52.     private ?string $content null;
  53.     /**
  54.      * @ORM\Column(type="datetime")
  55.      */
  56.     private \DateTime $publishedAt;
  57.     /**
  58.      * @ORM\ManyToOne(targetEntity="App\Entity\User")
  59.      * @ORM\JoinColumn(nullable=false)
  60.      */
  61.     private ?User $author null;
  62.     public function __construct()
  63.     {
  64.         $this->publishedAt = new \DateTime();
  65.     }
  66.     #[Assert\IsTrue(message'comment.is_spam')]
  67.     public function isLegitComment(): bool
  68.     {
  69.         $containsInvalidCharacters null !== u($this->content)->indexOf('@');
  70.         return !$containsInvalidCharacters;
  71.     }
  72.     public function getId(): ?int
  73.     {
  74.         return $this->id;
  75.     }
  76.     public function getContent(): ?string
  77.     {
  78.         return $this->content;
  79.     }
  80.     public function setContent(string $content): void
  81.     {
  82.         $this->content $content;
  83.     }
  84.     public function getPublishedAt(): \DateTime
  85.     {
  86.         return $this->publishedAt;
  87.     }
  88.     public function setPublishedAt(\DateTime $publishedAt): void
  89.     {
  90.         $this->publishedAt $publishedAt;
  91.     }
  92.     public function getAuthor(): ?User
  93.     {
  94.         return $this->author;
  95.     }
  96.     public function setAuthor(User $author): void
  97.     {
  98.         $this->author $author;
  99.     }
  100.     public function getPost(): ?Post
  101.     {
  102.         return $this->post;
  103.     }
  104.     public function setPost(Post $post): void
  105.     {
  106.         $this->post $post;
  107.     }
  108. }