src/JanusHercules/JobProfessionCategory/Infrastructure/Entity/CustomerCategoryCatalog.php line 32

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace JanusHercules\JobProfessionCategory\Infrastructure\Entity;
  4. use App\Utility\DatabaseIdGenerator;
  5. use DateTime;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\DBAL\Types\Types;
  9. use Doctrine\ORM\Mapping\Column;
  10. use Doctrine\ORM\Mapping\CustomIdGenerator;
  11. use Doctrine\ORM\Mapping\Entity;
  12. use Doctrine\ORM\Mapping\GeneratedValue;
  13. use Doctrine\ORM\Mapping\Id;
  14. use Doctrine\ORM\Mapping\OneToMany;
  15. use Doctrine\ORM\Mapping\Table;
  16. use Doctrine\ORM\Mapping\UniqueConstraint;
  17. use Symfony\Component\Validator\Constraints as Assert;
  18. #[Entity]
  19. #[Table(
  20. name: 'jpc_customer_category_catalogs',
  21. uniqueConstraints: [
  22. new UniqueConstraint(
  23. name: 'unique_customer_category_catalog',
  24. columns: ['customer_id', 'content_distributor_id']
  25. )
  26. ]
  27. )]
  28. class CustomerCategoryCatalog
  29. {
  30. public function __construct(
  31. ?string $customerId,
  32. string $name,
  33. ?int $contentDistributorId = null
  34. ) {
  35. $this->customerId = $customerId;
  36. $this->name = $name;
  37. $this->contentDistributorId = $contentDistributorId;
  38. $this->createdAt = new DateTime();
  39. $this->categories = new ArrayCollection();
  40. }
  41. #[Id]
  42. #[GeneratedValue(strategy: 'CUSTOM')]
  43. #[CustomIdGenerator(class: DatabaseIdGenerator::class)]
  44. #[Column(
  45. type : Types::GUID,
  46. unique: true
  47. )]
  48. private ?string $id = null;
  49. public function getId(): ?string
  50. {
  51. return $this->id;
  52. }
  53. #[Column(
  54. name: 'customer_id',
  55. type: Types::GUID,
  56. length: 36,
  57. nullable: true
  58. )]
  59. #[Assert\Length(
  60. min: 36,
  61. max: 36
  62. )]
  63. private ?string $customerId;
  64. public function getCustomerId(): ?string
  65. {
  66. return $this->customerId;
  67. }
  68. public function setCustomerId(?string $customerId): void
  69. {
  70. $this->customerId = $customerId;
  71. }
  72. #[Column(
  73. name: 'name',
  74. type: Types::STRING,
  75. length: 255,
  76. nullable: false
  77. )]
  78. #[Assert\NotBlank]
  79. #[Assert\Length(
  80. min: 1,
  81. max: 255
  82. )]
  83. private string $name;
  84. public function getName(): string
  85. {
  86. return $this->name;
  87. }
  88. public function setName(string $name): void
  89. {
  90. $this->name = $name;
  91. }
  92. #[Column(
  93. name: 'content_distributor_id',
  94. type: Types::INTEGER,
  95. nullable: true
  96. )]
  97. private ?int $contentDistributorId;
  98. public function getContentDistributorId(): ?int
  99. {
  100. return $this->contentDistributorId;
  101. }
  102. public function setContentDistributorId(?int $contentDistributorId): void
  103. {
  104. $this->contentDistributorId = $contentDistributorId;
  105. }
  106. #[Column(
  107. type : Types::DATETIME_MUTABLE,
  108. nullable: false
  109. )]
  110. private DateTime $createdAt;
  111. public function getCreatedAt(): DateTime
  112. {
  113. return $this->createdAt;
  114. }
  115. public function setCreatedAt(DateTime $createdAt): void
  116. {
  117. $this->createdAt = $createdAt;
  118. }
  119. /**
  120. * @var Collection<int, Category>
  121. */
  122. #[OneToMany(mappedBy: 'customerCategoryCatalog', targetEntity: Category::class, cascade: ['persist', 'remove'])]
  123. private Collection $categories;
  124. /** @return Collection<int, Category> */
  125. public function getCategories(): Collection
  126. {
  127. return $this->categories;
  128. }
  129. public function addCategory(Category $category): void
  130. {
  131. if (!$this->categories->contains($category)) {
  132. $this->categories->add($category);
  133. $category->setCustomerCategoryCatalog($this);
  134. }
  135. }
  136. public function removeCategory(Category $category): void
  137. {
  138. if ($this->categories->contains($category)) {
  139. $this->categories->removeElement($category);
  140. $category->setCustomerCategoryCatalog(null);
  141. }
  142. }
  143. }