src/Entity/ProductAttribute.php line 152

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Doctrine\Persistence\Event\LifecycleEventArgs;
  5. /**
  6. * ProductsAttributes
  7. *
  8. * @ORM\Table(name="products_attributes")
  9. * @ORM\Entity
  10. * @ORM\HasLifecycleCallbacks()
  11. */
  12. class ProductAttribute extends TranslatedEntity {
  13. protected $tranlatedEntity = 'ProductAttributeDescription';
  14. /**
  15. * @var int
  16. *
  17. * @ORM\Column(name="products_attributes_id", type="integer", nullable=false)
  18. * @ORM\Id
  19. * @ORM\GeneratedValue(strategy="IDENTITY")
  20. */
  21. private $id;
  22. /**
  23. * @var Product
  24. *
  25. * @ORM\ManyToOne(targetEntity="App\Entity\Product", inversedBy="attributes")
  26. * @ORM\JoinColumn(name="products_id", referencedColumnName="products_id", onDelete="CASCADE")
  27. */
  28. private $product;
  29. /**
  30. * @var Attribute
  31. *
  32. * @ORM\ManyToOne(targetEntity="App\Entity\Attribute")
  33. * @ORM\JoinColumn(name="attributes_id", referencedColumnName="attributes_id", onDelete="CASCADE")
  34. */
  35. private $attribute;
  36. /**
  37. * @ORM\OneToMany(targetEntity="App\Entity\ProductAttributeDescription", mappedBy="productAttribute", cascade={"persist", "remove"})
  38. */
  39. private $descriptions = [];
  40. /**
  41. * @var ?string
  42. *
  43. * @ORM\Column(name="attributes_value", type="string", length=255, nullable=true)
  44. */
  45. private $value;
  46. public function getId(): int {
  47. return $this->id;
  48. }
  49. public function getProduct(): Product {
  50. return $this->product;
  51. }
  52. public function getAttribute(): Attribute {
  53. return $this->attribute;
  54. }
  55. public function getValue(): ?string {
  56. return $this->value;
  57. }
  58. public function getDisplayedValue(): ?string {
  59. if ($this->value === null) {
  60. return null;
  61. }
  62. if(strpos($this->getAttribute()->getCode(), '-unit')!==false) {
  63. switch($this->value) {
  64. case 'MILLIMETER' : return 'mm';
  65. case 'MILLILITER' : return 'ml';
  66. case 'GRAM' : return 'g';
  67. case 'MONTH' : return '';
  68. default:;
  69. }
  70. }else{
  71. $unit = $this->product->getAttribute($this->getAttribute()->getCode() . '-unit');
  72. if ($unit && $unit->getValue()) {
  73. if($unit->getDisplayedValue() == 'ml') {
  74. $value = intval($this->getValue());
  75. if($value >= 1000) {
  76. return intval($this->getValue()/1000) . 'l';
  77. }
  78. return intval($this->getValue()) . $unit->getDisplayedValue();
  79. }
  80. return $this->getValue() . $unit->getDisplayedValue();
  81. }
  82. }
  83. $description = $this->getDescription();
  84. return $description ? $description->getValue() : $this->getValue();
  85. }
  86. public function setId(int $id): void {
  87. $this->id = $id;
  88. }
  89. public function setProduct(Product $product): void {
  90. $this->product = $product;
  91. }
  92. public function setAttribute(Attribute $attribute): void {
  93. $this->attribute = $attribute;
  94. }
  95. public function setValue(?string $value): void {
  96. $this->value = $value;
  97. }
  98. public function getDescriptions() {
  99. return $this->descriptions;
  100. }
  101. public function setDescriptions($descriptions): void {
  102. $this->descriptions = $descriptions;
  103. }
  104. public function addDescription(ProductAttributeDescription $description) {
  105. $this->descriptions->add($description);
  106. }
  107. public function getDescription() {
  108. if(!empty($this->languageId)){
  109. $description = null;
  110. foreach($this->descriptions as $d){
  111. if($d->getLanguage()->getId()==$this->languageId){
  112. $description = $d;
  113. break;
  114. }
  115. }
  116. }
  117. if(empty($description) && count($this->descriptions)){
  118. $description = $this->descriptions[0];
  119. }
  120. return $description ?? null;
  121. }
  122. /**
  123. * @ORM\PostLoad
  124. */
  125. public function postLoad(LifecycleEventArgs $args): void {
  126. if ($this->getAttribute()->getCode() === 'main_color') {
  127. $this->entity = $args->getEntityManager()->getRepository(Color::class)->findOneByCode($this->getValue());
  128. }else if($this->getAttribute()->getCode() === 'precise_color'){
  129. $this->entity = $args->getEntityManager()->getRepository(Color::class)->findOneByCode($this->getValue());
  130. }
  131. }
  132. }