<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Cocur\Slugify\Slugify;
use App\Repository\CategoryRepository;
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=CategoryRepository::class)
* @UniqueEntity(
* fields="title",
* message="Cette catégorie existe ! "
* )
* @ApiResource()
*/
class Category
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Assert\Length(
* min = 3,
* max = 70
* )
* @Assert\NotBlank()
*/
private $title;
/**
* @ORM\OneToMany(targetEntity=Article::class, mappedBy="category", orphanRemoval=true)
*/
private $articles;
/**
* @ORM\Column(type="boolean")
*/
private $is_active;
/**
* @ORM\ManyToOne(targetEntity=Category2::class, inversedBy="categorys")
*/
private $category2;
/**
* @ORM\Column(type="string", length=255)
*/
private $slug;
public function __construct()
{
$this->articles = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function getSlug()
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$Slugify = new Slugify();
$slug = $Slugify->slugify($this->getTitle());
$this->slug = $slug;
return $this;
}
public function setTitle(string $title): self
{
$this->title = ucfirst($title);
return $this;
}
/**
* @return Collection|Article[]
*/
public function getArticles(): Collection
{
return $this->articles;
}
public function addArticle(Article $article): self
{
if (!$this->articles->contains($article)) {
$this->articles[] = $article;
$article->setCategory($this);
}
return $this;
}
public function removeArticle(Article $article): self
{
if ($this->articles->removeElement($article)) {
// set the owning side to null (unless already changed)
if ($article->getCategory() === $this) {
$article->setCategory(null);
}
}
return $this;
}
public function getIsActive(): ?bool
{
return $this->is_active;
}
public function setIsActive(bool $is_active): self
{
$this->is_active = $is_active;
return $this;
}
public function getCategory2(): ?Category2
{
return $this->category2;
}
public function setCategory2(?Category2 $category2): self
{
$this->category2 = $category2;
return $this;
}
}