src/Entity/ProductAttribute.php line 154

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. }else if($unit->getDisplayedValue() == 'mm') {
  80. return (intval($this->getValue())/10) . ' cm';
  81. }else if($unit->getDisplayedValue() == 'g') {
  82. return (intval($this->getValue())/1000) . ' kg';
  83. }
  84. return $this->getValue() . ' ' . $unit->getDisplayedValue();
  85. }
  86. }
  87. $description = $this->getDescription();
  88. return $description ? $description->getValue() : $this->getValue();
  89. }
  90. public function setId(int $id): void {
  91. $this->id = $id;
  92. }
  93. public function setProduct(Product $product): void {
  94. $this->product = $product;
  95. }
  96. public function setAttribute(Attribute $attribute): void {
  97. $this->attribute = $attribute;
  98. }
  99. public function setValue(?string $value): void {
  100. $this->value = $value;
  101. }
  102. public function getDescriptions() {
  103. return $this->descriptions;
  104. }
  105. public function setDescriptions($descriptions): void {
  106. $this->descriptions = $descriptions;
  107. }
  108. public function addDescription(ProductAttributeDescription $description) {
  109. $this->descriptions->add($description);
  110. }
  111. public function getDescription() {
  112. if(!empty($this->languageId)){
  113. $description = null;
  114. foreach($this->descriptions as $d){
  115. if($d->getLanguage()->getId()==$this->languageId){
  116. $description = $d;
  117. break;
  118. }
  119. }
  120. }
  121. if(empty($description) && count($this->descriptions)){
  122. $description = $this->descriptions[0];
  123. }
  124. return $description ?? null;
  125. }
  126. /**
  127. * @ORM\PostLoad
  128. */
  129. public function postLoad(LifecycleEventArgs $args): void {
  130. if ($this->getAttribute()->getCode() === 'main_color') {
  131. $this->entity = $args->getEntityManager()->getRepository(Color::class)->findOneByCode($this->getValue());
  132. }else if($this->getAttribute()->getCode() === 'precise_color'){
  133. $this->entity = $args->getEntityManager()->getRepository(Color::class)->findOneByCode($this->getValue());
  134. }
  135. }
  136. }