<?php
declare(strict_types=1);
namespace JanusHercules\JobProfessionCategory\Infrastructure\Entity;
use App\Utility\DatabaseIdGenerator;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\CustomIdGenerator;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\OneToMany;
use Doctrine\ORM\Mapping\Table;
use Doctrine\ORM\Mapping\UniqueConstraint;
use Symfony\Component\Validator\Constraints as Assert;
#[Entity]
#[Table(
name: 'jpc_customer_category_catalogs',
uniqueConstraints: [
new UniqueConstraint(
name: 'unique_customer_category_catalog',
columns: ['customer_id', 'content_distributor_id']
)
]
)]
class CustomerCategoryCatalog
{
public function __construct(
?string $customerId,
string $name,
?int $contentDistributorId = null
) {
$this->customerId = $customerId;
$this->name = $name;
$this->contentDistributorId = $contentDistributorId;
$this->createdAt = new DateTime();
$this->categories = new ArrayCollection();
}
#[Id]
#[GeneratedValue(strategy: 'CUSTOM')]
#[CustomIdGenerator(class: DatabaseIdGenerator::class)]
#[Column(
type : Types::GUID,
unique: true
)]
private ?string $id = null;
public function getId(): ?string
{
return $this->id;
}
#[Column(
name: 'customer_id',
type: Types::GUID,
length: 36,
nullable: true
)]
#[Assert\Length(
min: 36,
max: 36
)]
private ?string $customerId;
public function getCustomerId(): ?string
{
return $this->customerId;
}
public function setCustomerId(?string $customerId): void
{
$this->customerId = $customerId;
}
#[Column(
name: 'name',
type: Types::STRING,
length: 255,
nullable: false
)]
#[Assert\NotBlank]
#[Assert\Length(
min: 1,
max: 255
)]
private string $name;
public function getName(): string
{
return $this->name;
}
public function setName(string $name): void
{
$this->name = $name;
}
#[Column(
name: 'content_distributor_id',
type: Types::INTEGER,
nullable: true
)]
private ?int $contentDistributorId;
public function getContentDistributorId(): ?int
{
return $this->contentDistributorId;
}
public function setContentDistributorId(?int $contentDistributorId): void
{
$this->contentDistributorId = $contentDistributorId;
}
#[Column(
type : Types::DATETIME_MUTABLE,
nullable: false
)]
private DateTime $createdAt;
public function getCreatedAt(): DateTime
{
return $this->createdAt;
}
public function setCreatedAt(DateTime $createdAt): void
{
$this->createdAt = $createdAt;
}
/**
* @var Collection<int, Category>
*/
#[OneToMany(mappedBy: 'customerCategoryCatalog', targetEntity: Category::class, cascade: ['persist', 'remove'])]
private Collection $categories;
/** @return Collection<int, Category> */
public function getCategories(): Collection
{
return $this->categories;
}
public function addCategory(Category $category): void
{
if (!$this->categories->contains($category)) {
$this->categories->add($category);
$category->setCustomerCategoryCatalog($this);
}
}
public function removeCategory(Category $category): void
{
if ($this->categories->contains($category)) {
$this->categories->removeElement($category);
$category->setCustomerCategoryCatalog(null);
}
}
}