<?php
namespace App\Entity;
use App\Repository\BlogAuthorRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
#[ORM\Entity(repositoryClass: BlogAuthorRepository::class)]
#[ORM\Table(name: 'blog_author')]
#[ORM\HasLifecycleCallbacks]
#[UniqueEntity(fields: ['email'], message: 'There is already an author with this email')]
class BlogAuthor
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
#[Assert\NotBlank]
private ?string $name = null;
#[ORM\Column(type: 'text', nullable: true)]
private ?string $bio = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $avatar = null;
#[ORM\Column(length: 255, unique: true)]
#[Assert\NotBlank]
#[Assert\Email]
private ?string $email = null;
#[ORM\Column]
private ?\DateTimeImmutable $createdAt = null;
#[ORM\OneToMany(mappedBy: 'author', targetEntity: Blog::class)]
private Collection $blogPosts;
public function __construct()
{
$this->blogPosts = new ArrayCollection();
$this->createdAt = new \DateTimeImmutable();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): static
{
$this->name = $name;
return $this;
}
public function getBio(): ?string
{
return $this->bio;
}
public function setBio(?string $bio): static
{
$this->bio = $bio;
return $this;
}
public function getAvatar(): ?string
{
return $this->avatar;
}
public function setAvatar(?string $avatar): static
{
$this->avatar = $avatar;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): static
{
$this->email = $email;
return $this;
}
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeImmutable $createdAt): static
{
$this->createdAt = $createdAt;
return $this;
}
/**
* @return Collection<int, Blog>
*/
public function getBlogPosts(): Collection
{
return $this->blogPosts;
}
public function addBlogPost(Blog $blogPost): static
{
if (!$this->blogPosts->contains($blogPost)) {
$this->blogPosts->add($blogPost);
$blogPost->setAuthor($this);
}
return $this;
}
public function removeBlogPost(Blog $blogPost): static
{
if ($this->blogPosts->removeElement($blogPost)) {
// set the owning side to null (unless already changed)
if ($blogPost->getAuthor() === $this) {
$blogPost->setAuthor(null);
}
}
return $this;
}
public function __toString(): string
{
return $this->name ?? 'New Author';
}
}