src/Entity/Profile/Comment/Comment.php line 24

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by simpson <simpsonwork@gmail.com>
  4.  * Date: 2019-03-21
  5.  * Time: 19:42
  6.  */
  7. namespace App\Entity\Profile\Comment;
  8. use App\Entity\Profile\Profile;
  9. use App\Entity\System\IpAddress;
  10. use App\Entity\User;
  11. use App\Repository\ProfileCommentRepository;
  12. use Doctrine\ORM\Mapping as ORM;
  13. use Gedmo\SoftDeleteable\Traits\SoftDeleteableEntity;
  14. use Symfony\Component\Serializer\Annotation\Groups;
  15. #[ORM\Table(name: 'profile_comments')]
  16. #[ORM\Entity(repositoryClass: ProfileCommentRepository::class)]
  17. #[ORM\InheritanceType('SINGLE_TABLE')]
  18. #[ORM\DiscriminatorColumn(name: 'account_type', type: 'string', length: 12)]
  19. #[ORM\DiscriminatorMap(['customer' => CommentByCustomer::class, 'advertiser' => CommentByAdvertiser::class, 'support' => CommentBySupport::class])]
  20. abstract class Comment
  21. {
  22.     use SoftDeleteableEntity;
  23.     #[ORM\Id]
  24.     #[ORM\Column(name: 'id', type: 'integer')]
  25.     #[ORM\GeneratedValue(strategy: 'AUTO')]
  26.     protected int $id;
  27.     #[ORM\JoinColumn(name: 'parent_id', referencedColumnName: 'id', nullable: true)]
  28.     #[ORM\ManyToOne(targetEntity: Comment::class)]
  29.     protected ?CommentByCustomer $parent;
  30.     //, inversedBy="comments"
  31.     #[ORM\JoinColumn(name: 'profile_id', referencedColumnName: 'id', nullable: true)]
  32.     #[ORM\ManyToOne(targetEntity: Profile::class, inversedBy: 'comments')]
  33.     protected ?Profile $profile;
  34.     #[ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id', nullable: true)]
  35.     #[ORM\ManyToOne(targetEntity: User::class)]
  36.     #[Groups(['preview', 'comments'])]
  37.     protected ?User $user;
  38.     #[ORM\Column(name: 'text', type: 'text')]
  39.     #[Groups(['preview', 'comments'])]
  40.     protected string $text;
  41.     #[ORM\Column(name: 'author', type: 'string', length: 64, nullable: true)]
  42.     #[Groups(['preview', 'comments'])]
  43.     protected ?string $author;
  44.     #[ORM\Column(name: 'created_at', type: 'datetimetz_immutable')]
  45.     #[Groups(['preview', 'comments'])]
  46.     protected \DateTimeImmutable $createdAt;
  47.     #[ORM\JoinColumn(name: 'ip_address_id', referencedColumnName: 'id')]
  48.     #[ORM\ManyToOne(targetEntity: IpAddress::class, inversedBy: 'profileComments')]
  49.     protected ?IpAddress $ipAddress;
  50.     public function __construct(Profile $profile, ?CommentByCustomer $parent, ?User $user, string $text, \DateTimeImmutable $createdAt, ?string $author = null)
  51.     {
  52.         $this->parent = $parent;
  53.         $this->profile = $profile;
  54.         $this->user = $user;
  55.         $this->text = $text;
  56.         $this->author = $this->normalizeAuthor($author);
  57.         $this->createdAt = $createdAt;
  58.     }
  59.     public function getId(): int
  60.     {
  61.         return $this->id;
  62.     }
  63.     public function getParent(): ?CommentByCustomer
  64.     {
  65.         return $this->parent;
  66.     }
  67.     public function getUser(): ?User
  68.     {
  69.         return $this->user;
  70.     }
  71.     public function getProfile(): Profile
  72.     {
  73.         return $this->profile;
  74.     }
  75.     public function getText(): string
  76.     {
  77.         return $this->text;
  78.     }
  79.     public function getAuthorName(): ?string
  80.     {
  81.         return $this->user?->getNickName() ?? $this->author;
  82.     }
  83.     public function setText(string $text): void
  84.     {
  85.         $this->text = $text;
  86.     }
  87.     public function getCreatedAt(): \DateTimeImmutable
  88.     {
  89.         return $this->createdAt;
  90.     }
  91.     public function setCreatedAt(\DateTimeImmutable $createdAt): void
  92.     {
  93.         $this->createdAt = $createdAt;
  94.     }
  95.     public function clearRelations(): void
  96.     {
  97.         $this->profile = null;
  98.         $this->user = null;
  99.     }
  100.     public function getIpAddress(): ?IpAddress
  101.     {
  102.         return $this->ipAddress;
  103.     }
  104.     private function normalizeAuthor(?string $author): ?string
  105.     {
  106.         if (null === $author || '' === trim($author)) {
  107.             return null;
  108.         }
  109.         $author = trim($author);
  110.         if (64 < mb_strlen($author)) {
  111.             throw new \DomainException('Comment author name must not exceed 64 characters');
  112.         }
  113.         return $author;
  114.     }
  115. }