src/Entity/BaseProductVariant.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\BaseProductVariantRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Gedmo\Timestampable\Traits\TimestampableEntity;
  8. use Symfony\Component\String\Slugger\AsciiSlugger;
  9. #[ORM\Entity(repositoryClass: BaseProductVariantRepository::class)]
  10. class BaseProductVariant
  11. {
  12. use TimestampableEntity;
  13. #[ORM\Id]
  14. #[ORM\GeneratedValue]
  15. #[ORM\Column]
  16. private ?int $id = null;
  17. #[ORM\ManyToOne(inversedBy: 'baseProductVariants')]
  18. #[ORM\JoinColumn(nullable: false)]
  19. private ?BaseProduct $baseProduct = null;
  20. #[ORM\ManyToMany(targetEntity: BaseProductVariantAttribute::class, inversedBy: 'baseProductVariants')]
  21. #[ORM\OrderBy(["variantAttribute" => "ASC"])]
  22. private Collection $baseProductVariantAttributes;
  23. #[ORM\Column(length: 255, nullable: true)]
  24. private ?string $sku = null;
  25. #[ORM\Column(nullable: true)]
  26. private ?float $price = 0;
  27. #[ORM\Column]
  28. private ?bool $active = true;
  29. #[ORM\OneToMany(mappedBy: 'baseProductVariant', targetEntity: BaseProductSide::class)]
  30. private Collection $baseProductSides;
  31. #[ORM\Column(length: 255, nullable: true)]
  32. private ?string $name = null;
  33. #[ORM\Column(type:"integer", nullable:true)]
  34. private ?int $weight = null;
  35. #[ORM\Column(type:"integer", nullable:true)]
  36. private ?int $length = null;
  37. #[ORM\Column(type:"integer", nullable:true)]
  38. private ?int $width = null;
  39. #[ORM\Column(type:"integer", nullable:true)]
  40. private ?int $height = null;
  41. public function __construct()
  42. {
  43. $this->baseProductVariantAttributes = new ArrayCollection();
  44. $this->baseProductSides = new ArrayCollection();
  45. }
  46. public function __toString(): string
  47. {
  48. if ($this->name) {
  49. return $this->getName();
  50. }
  51. return $this->getGeneratedName();
  52. }
  53. public function getGeneratedSku(): string {
  54. $slugger = new AsciiSlugger();
  55. return $slugger->slug($this->getGeneratedName(), '_');
  56. }
  57. public function getGeneratedPrice(): float {
  58. return 0;
  59. }
  60. public function getGeneratedName(): string
  61. {
  62. $nameArray = [];
  63. $attributes = $this->getBaseProductVariantAttributes();
  64. foreach ($attributes as $attribute) {
  65. $name = $attribute->getVariantAttribute()->getName();
  66. $nameArray[$name] = $attribute->getValue();
  67. }
  68. ksort($nameArray);
  69. $name = implode(' ', $nameArray);
  70. $name = trim($name);
  71. if ($name === "") {
  72. $name = "New Variant";
  73. }
  74. return $name;
  75. }
  76. public function getId(): ?int
  77. {
  78. return $this->id;
  79. }
  80. public function getBaseProduct(): ?BaseProduct
  81. {
  82. return $this->baseProduct;
  83. }
  84. public function setBaseProduct(?BaseProduct $baseProduct): self
  85. {
  86. $this->baseProduct = $baseProduct;
  87. return $this;
  88. }
  89. /**
  90. * @return Collection<int, BaseProductVariantAttribute>
  91. */
  92. public function getBaseProductVariantAttributes(): Collection
  93. {
  94. return $this->baseProductVariantAttributes;
  95. }
  96. public function addBaseProductVariantAttribute(BaseProductVariantAttribute $baseProductVariantAttribute): self
  97. {
  98. if (!$this->baseProductVariantAttributes->contains($baseProductVariantAttribute)) {
  99. $this->baseProductVariantAttributes->add($baseProductVariantAttribute);
  100. }
  101. return $this;
  102. }
  103. public function removeBaseProductVariantAttribute(BaseProductVariantAttribute $baseProductVariantAttribute): self
  104. {
  105. $this->baseProductVariantAttributes->removeElement($baseProductVariantAttribute);
  106. return $this;
  107. }
  108. public function getSku(): ?string
  109. {
  110. return $this->sku;
  111. }
  112. public function setSku(?string $sku): self
  113. {
  114. $this->sku = $sku;
  115. return $this;
  116. }
  117. public function getPrice(): ?float
  118. {
  119. return $this->price;
  120. }
  121. public function setPrice(?float $price): self
  122. {
  123. $this->price = $price;
  124. return $this;
  125. }
  126. public function isActive(): ?bool
  127. {
  128. return $this->active;
  129. }
  130. public function setActive(bool $active): self
  131. {
  132. $this->active = $active;
  133. return $this;
  134. }
  135. /**
  136. * @return Collection<int, BaseProductSide>
  137. */
  138. public function getBaseProductSides(): Collection
  139. {
  140. return $this->baseProductSides;
  141. }
  142. public function addBaseProductSide(BaseProductSide $baseProductSide): self
  143. {
  144. if (!$this->baseProductSides->contains($baseProductSide)) {
  145. $this->baseProductSides->add($baseProductSide);
  146. $baseProductSide->setBaseProductVariant($this);
  147. }
  148. return $this;
  149. }
  150. public function removeBaseProductSide(BaseProductSide $baseProductSide): self
  151. {
  152. if ($this->baseProductSides->removeElement($baseProductSide)) {
  153. // set the owning side to null (unless already changed)
  154. if ($baseProductSide->getBaseProductVariant() === $this) {
  155. $baseProductSide->setBaseProductVariant(null);
  156. }
  157. }
  158. return $this;
  159. }
  160. public function getName(): ?string
  161. {
  162. return $this->name;
  163. }
  164. public function setName(?string $name): self
  165. {
  166. $this->name = $name;
  167. return $this;
  168. }
  169. public function getAttributeByValue($value) {
  170. foreach ( $this->baseProductVariantAttributes as $bpvAttribute) {
  171. if (htmlspecialchars(ucfirst($bpvAttribute->getValue())) === $value ||
  172. $bpvAttribute->getValue() === $value
  173. ) {
  174. return $bpvAttribute;
  175. }
  176. }
  177. return null;
  178. }
  179. public function getAttributesAssoc($encode = false) {
  180. $arr = [];
  181. foreach ($this->baseProductVariantAttributes as $bpvAttribute) {
  182. $arr[$bpvAttribute->getVariantAttribute()->getId()] = $bpvAttribute->getValue();
  183. }
  184. return $encode ? json_encode($arr) : $arr;
  185. }
  186. public function getWeight(): ?int
  187. {
  188. return $this->weight ?? $this->baseProduct->getDefaultWeight();
  189. }
  190. public function setWeight(?int $weight): self
  191. {
  192. $this->weight = $weight ?: $this->baseProduct->getDefaultWeight();
  193. return $this;
  194. }
  195. public function getLength(): ?int
  196. {
  197. return $this->length ?? $this->baseProduct->getDefaultLength();
  198. }
  199. public function setLength(?int $length): self
  200. {
  201. $this->length = $length;
  202. return $this;
  203. }
  204. public function getWidth(): ?int
  205. {
  206. return $this->width ?? $this->baseProduct->getDefaultWidth();
  207. }
  208. public function setWidth(?int $width): self
  209. {
  210. $this->width = $width;
  211. return $this;
  212. }
  213. public function getHeight(): ?int
  214. {
  215. return $this->height ?? $this->baseProduct->getDefaultHeight();
  216. }
  217. public function setHeight(?int $height): self
  218. {
  219. $this->height = $height;
  220. return $this;
  221. }
  222. }