vendor/roothirsch/pim-bundle/Entity/AttributeGroup.php line 16

Open in your IDE?
  1. <?php
  2. namespace Roothirsch\PimBundle\Entity;
  3. use ApiPlatform\Core\Annotation\ApiResource;
  4. use Roothirsch\PimBundle\Repository\AttributeGroupRepository;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. /**
  9. * @ApiResource( shortName="Pim/AttributeGroup")
  10. * @ORM\Entity(repositoryClass=AttributeGroupRepository::class)
  11. * @ORM\Table(name="pim_attribute_group")
  12. */
  13. class AttributeGroup
  14. {
  15. /**
  16. * @ORM\Id
  17. * @ORM\GeneratedValue
  18. * @ORM\Column(type="integer")
  19. */
  20. private $id;
  21. /**
  22. * @ORM\Column(type="string", length=255)
  23. */
  24. private $title;
  25. /**
  26. * @ORM\OneToMany(targetEntity=Attribute::class, mappedBy="attributeGroup", orphanRemoval=true)
  27. */
  28. private $attributes;
  29. /**
  30. * @ORM\ManyToMany(targetEntity=Category::class, mappedBy="attributeGroups")
  31. */
  32. private $categories;
  33. public function __construct($title = null)
  34. {
  35. $this->attributes = new ArrayCollection();
  36. $this->categories = new ArrayCollection();
  37. $this->title = $title;
  38. }
  39. public function __toString()
  40. {
  41. return $this->title;
  42. }
  43. public function getId(): ?int
  44. {
  45. return $this->id;
  46. }
  47. public function getTitle(): ?string
  48. {
  49. return $this->title;
  50. }
  51. public function setTitle(string $title): self
  52. {
  53. $this->title = $title;
  54. return $this;
  55. }
  56. /**
  57. * @return Collection|Attribute[]
  58. */
  59. public function getAttributes(): Collection
  60. {
  61. return $this->attributes;
  62. }
  63. public function addAttribute(Attribute $attribute): self
  64. {
  65. if (!$this->attributes->contains($attribute)) {
  66. $this->attributes[] = $attribute;
  67. $attribute->setAttributeGroup($this);
  68. }
  69. return $this;
  70. }
  71. public function removeAttribute(Attribute $attribute): self
  72. {
  73. if ($this->attributes->removeElement($attribute)) {
  74. // set the owning side to null (unless already changed)
  75. if ($attribute->getAttributeGroup() === $this) {
  76. $attribute->setAttributeGroup(null);
  77. }
  78. }
  79. return $this;
  80. }
  81. /**
  82. * @return Collection|Category[]
  83. */
  84. public function getCategories(): Collection
  85. {
  86. return $this->categories;
  87. }
  88. public function addCategory(Category $category): self
  89. {
  90. if (!$this->categories->contains($category)) {
  91. $this->categories[] = $category;
  92. $category->addAttributeGroup($this);
  93. }
  94. return $this;
  95. }
  96. public function removeCategory(Category $category): self
  97. {
  98. if ($this->categories->removeElement($category)) {
  99. $category->removeAttributeGroup($this);
  100. }
  101. return $this;
  102. }
  103. public function getAttributeByName($name) {
  104. foreach($this->attributes as $attribute) {
  105. if ($attribute->getName() == $name){
  106. return $attribute;
  107. }
  108. }
  109. return null;
  110. }
  111. }