<?php
namespace App\Entity;
use App\Repository\CourseCategoryRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Doctrine\Common\Collections\Criteria;
use Iterator;
/**
* @ORM\Entity(repositoryClass=CourseCategoryRepository::class)
* @UniqueEntity(fields={"level"}, message="There is already a category with this level")
*/
class CourseCategory
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\ManyToOne(targetEntity=CourseCategory::class, inversedBy="courseCategories")
*/
private $parent;
/**
* @ORM\OneToMany(targetEntity=CourseCategory::class, mappedBy="parent")
*/
private $courseCategories;
/**
* @ORM\OneToMany(targetEntity=Cours::class, mappedBy="category", cascade={"remove"})
*/
private $cours;
/**
* @ORM\Column(type="integer")
*/
private $level;
public function __construct()
{
$this->courseCategories = new ArrayCollection();
$this->cours = new ArrayCollection();
$this->level = 1;
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getParent(): ?self
{
return $this->parent;
}
public function setParent(?self $parent): self
{
$this->parent = $parent;
return $this;
}
/**
* @return Collection<int, self>
*/
public function getCourseCategories(): Collection
{
return $this->courseCategories;
}
public function addCourseCategory(self $courseCategory): self
{
if (!$this->courseCategories->contains($courseCategory)) {
$this->courseCategories[] = $courseCategory;
$courseCategory->setParent($this);
}
return $this;
}
public function removeCourseCategory(self $courseCategory): self
{
if ($this->courseCategories->removeElement($courseCategory)) {
// set the owning side to null (unless already changed)
if ($courseCategory->getParent() === $this) {
$courseCategory->setParent(null);
}
}
return $this;
}
/**
* @return Collection<int, Cours>
*/
public function getCours(): Collection
{
return $this->cours;
}
public function addCour(Cours $cour): self
{
if (!$this->cours->contains($cour)) {
$this->cours[] = $cour;
$cour->setCategory($this);
}
return $this;
}
public function removeCour(Cours $cour): self
{
if ($this->cours->removeElement($cour)) {
// set the owning side to null (unless already changed)
if ($cour->getCategory() === $this) {
$cour->setCategory(null);
}
}
return $this;
}
public function getPublishedCourse(): Collection
{
$criteria = Criteria::create()
->where(Criteria::expr()->eq('published', true))
->orderBy(['id' => 'ASC']);
return $this->cours->matching($criteria);
}
public function __toString()
{
return $this->name;
}
public function getLevel(): ?int
{
return $this->level;
}
public function setLevel(int $level): self
{
$this->level = $level;
return $this;
}
}