<?php
namespace Roothirsch\Tuer24Bundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: "Roothirsch\Tuer24Bundle\Repository\Tuer24CategoryRepository")]
#[ORM\Table(name: 'roothirsch_tuer24_category')]
#[Assert\Callback(['Roothirsch\Tuer24Bundle\Validator\TranslationValidator', 'validateCategoryTranslations'])]
class Tuer24Category
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[ORM\ManyToOne(targetEntity: "Tuer24Category", inversedBy: "children")]
#[ORM\JoinColumn(name: "parent_id", referencedColumnName: "id", onDelete: "SET NULL")]
private $parent;
#[ORM\OneToMany(targetEntity: "Tuer24Category", mappedBy: "parent")]
#[ORM\OrderBy(["position" => "ASC"])]
private $children;
#[ORM\Column(type: 'string', length: 255)]
#[Assert\Length(max: 255)]
private $name = '';
#[ORM\Column(type: 'string', length: 100, unique: true)]
#[Assert\NotBlank]
#[Assert\Length(max: 100)]
private $code;
#[ORM\Column(type: 'text', nullable: true)]
private $description;
#[ORM\Column(type: 'integer')]
private $position = 0;
#[ORM\Column(type: 'boolean')]
private $enabled = true;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private $imagePath;
#[ORM\OneToMany(targetEntity: "Tuer24CategoryTranslation", mappedBy: "category", cascade: ["persist", "remove"], orphanRemoval: true)]
private $translations;
#[ORM\ManyToMany(targetEntity: "Tuer24Product", mappedBy: "categories")]
private $products;
/**
* Constructor
*/
public function __construct()
{
$this->children = new ArrayCollection();
$this->translations = new ArrayCollection();
$this->products = new ArrayCollection();
$this->name = ''; // Initialize with empty string
$this->code = ''; // Initialize with empty string
}
/**
* Get id
*
* @return int|null
*/
public function getId(): ?int
{
return $this->id;
}
/**
* Get parent
*
* @return Tuer24Category|null
*/
public function getParent(): ?self
{
return $this->parent;
}
/**
* Set parent
*
* @param Tuer24Category|null $parent
* @return $this
*/
public function setParent(?self $parent): self
{
$this->parent = $parent;
return $this;
}
/**
* Get children
*
* @return Collection|Tuer24Category[]
*/
public function getChildren(): Collection
{
return $this->children;
}
/**
* Add child
*
* @param Tuer24Category $child
* @return $this
*/
public function addChild(self $child): self
{
if (!$this->children->contains($child)) {
$this->children[] = $child;
$child->setParent($this);
}
return $this;
}
/**
* Remove child
*
* @param Tuer24Category $child
* @return $this
*/
public function removeChild(self $child): self
{
if ($this->children->removeElement($child)) {
// set the owning side to null (unless already changed)
if ($child->getParent() === $this) {
$child->setParent(null);
}
}
return $this;
}
/**
* Get name
*
* @return string|null
*/
public function getName(): ?string
{
return $this->name;
}
/**
* Set name
*
* @param string $name
* @return $this
*/
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
/**
* Get code
*
* @return string|null
*/
public function getCode(): ?string
{
return $this->code;
}
/**
* Set code
*
* @param string $code
* @return $this
*/
public function setCode(string $code): self
{
$this->code = $code;
return $this;
}
/**
* Get description
*
* @return string|null
*/
public function getDescription(): ?string
{
return $this->description;
}
/**
* Set description
*
* @param string|null $description
* @return $this
*/
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
/**
* Get position
*
* @return int|null
*/
public function getPosition(): ?int
{
return $this->position;
}
/**
* Set position
*
* @param int $position
* @return $this
*/
public function setPosition(int $position): self
{
$this->position = $position;
return $this;
}
/**
* Is enabled
*
* @return bool|null
*/
public function isEnabled(): ?bool
{
return $this->enabled;
}
/**
* Set enabled
*
* @param bool $enabled
* @return $this
*/
public function setEnabled(bool $enabled): self
{
$this->enabled = $enabled;
return $this;
}
/**
* Get translations
*
* @return Collection|Tuer24CategoryTranslation[]
*/
public function getTranslations(): Collection
{
return $this->translations;
}
/**
* Add translation
*
* @param Tuer24CategoryTranslation $translation
* @return $this
*/
public function addTranslation(Tuer24CategoryTranslation $translation): self
{
if (!$this->translations->contains($translation)) {
$this->translations[] = $translation;
$translation->setCategory($this);
}
return $this;
}
/**
* Remove translation
*
* @param Tuer24CategoryTranslation $translation
* @return $this
*/
public function removeTranslation(Tuer24CategoryTranslation $translation): self
{
if ($this->translations->removeElement($translation)) {
// set the owning side to null (unless already changed)
if ($translation->getCategory() === $this) {
$translation->setCategory(null);
}
}
return $this;
}
/**
* Get products
*
* @return Collection|Tuer24Product[]
*/
public function getProducts(): Collection
{
return $this->products;
}
/**
* Add product
*
* @param Tuer24Product $product
* @return $this
*/
public function addProduct(Tuer24Product $product): self
{
if (!$this->products->contains($product)) {
$this->products[] = $product;
}
return $this;
}
/**
* Remove product
*
* @param Tuer24Product $product
* @return $this
*/
public function removeProduct(Tuer24Product $product): self
{
$this->products->removeElement($product);
return $this;
}
/**
* Get translation by locale
*
* @param string $locale
* @return Tuer24CategoryTranslation|null
*/
public function getTranslation(string $locale): ?Tuer24CategoryTranslation
{
foreach ($this->translations as $translation) {
if ($translation->getLocale() === $locale) {
return $translation;
}
}
return null;
}
/**
* Returns a string representation of this category
*
* @return string
*/
public function __toString(): string
{
return $this->getName() ?: 'New Category';
}
/**
* Get image path
*
* @return string|null
*/
public function getImagePath(): ?string
{
return $this->imagePath;
}
/**
* Set image path
*
* @param string|null $imagePath
* @return $this
*/
public function setImagePath(?string $imagePath): self
{
$this->imagePath = $imagePath;
return $this;
}
}