src/Entity/ProductAttribute.php line 195

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. $value = $this->computeDisplayedValue();
  60. $suffix = $this->getAttribute()->getSuffix();
  61. if ($value !== null && $value !== '' && !empty($suffix)) {
  62. $value .= ' '.$suffix;
  63. }
  64. return $value;
  65. }
  66. private function computeDisplayedValue(): ?string {
  67. if ($this->value === null) {
  68. return null;
  69. }
  70. if(strpos($this->getAttribute()->getCode(), '-unit')!==false) {
  71. switch($this->value) {
  72. case 'MILLIMETER' : return 'mm';
  73. case 'MILLILITER' : return 'ml';
  74. case 'GRAM' : return 'g';
  75. case 'MONTH' : return '';
  76. case 'CENTIMETER' : return 'cm';
  77. case 'KILOGRAM' : return 'kg';
  78. default:;
  79. }
  80. }else{
  81. $unit = $this->product->getAttribute($this->getAttribute()->getCode() . '-unit');
  82. if ($unit && $unit->getValue()) {
  83. if($unit->getDisplayedValue() == 'ml') {
  84. $value = intval($this->getValue());
  85. if($value >= 1000) {
  86. return intval($this->getValue()/1000) . ' l';
  87. }
  88. return intval($this->getValue()) . ' ' . $unit->getDisplayedValue();
  89. }else if($unit->getDisplayedValue() == 'mm') {
  90. return (intval($this->getValue())/10) . ' cm';
  91. }else if($unit->getDisplayedValue() == 'g') {
  92. return (intval($this->getValue())/1000) . ' kg';
  93. }
  94. return $this->stripInsignificantZeros($this->getValue()) . ' ' . $unit->getDisplayedValue();
  95. }
  96. }
  97. $description = $this->getDescription();
  98. return $this->stripInsignificantZeros($description ? $description->getValue() : $this->getValue());
  99. }
  100. /**
  101. * Si la valeur a la structure d'un nombre décimal ("3.50", "10,00", "2.5000"),
  102. * supprime les zéros non significatifs de la partie décimale ("3.5", "10", "2.5").
  103. * Laisse les autres valeurs (unités, texte, ...) inchangées.
  104. */
  105. private function stripInsignificantZeros(?string $value): ?string {
  106. if ($value === null) {
  107. return $value;
  108. }
  109. if (preg_match('/^(-?\d+)([.,])(\d+)$/', trim($value), $matches)) {
  110. $decimals = rtrim($matches[3], '0');
  111. return $decimals === '' ? $matches[1] : $matches[1] . $matches[2] . $decimals;
  112. }
  113. return $value;
  114. }
  115. public function setId(int $id): void {
  116. $this->id = $id;
  117. }
  118. public function setProduct(Product $product): void {
  119. $this->product = $product;
  120. }
  121. public function setAttribute(Attribute $attribute): void {
  122. $this->attribute = $attribute;
  123. }
  124. public function setValue(?string $value): void {
  125. $this->value = $value;
  126. }
  127. public function getDescriptions() {
  128. return $this->descriptions;
  129. }
  130. public function setDescriptions($descriptions): void {
  131. $this->descriptions = $descriptions;
  132. }
  133. public function addDescription(ProductAttributeDescription $description) {
  134. $this->descriptions->add($description);
  135. }
  136. /**
  137. * Description de la langue demandée, sans repli sur une autre langue (à utiliser en écriture)
  138. */
  139. public function getDescriptionByLanguage(int $languageId): ?ProductAttributeDescription {
  140. foreach($this->descriptions as $d){
  141. if($d->getLanguage()->getId() == $languageId){
  142. return $d;
  143. }
  144. }
  145. return null;
  146. }
  147. public function getDescription() {
  148. if(!empty($this->languageId)){
  149. $description = null;
  150. foreach($this->descriptions as $d){
  151. if($d->getLanguage()->getId()==$this->languageId){
  152. $description = $d;
  153. break;
  154. }
  155. }
  156. }
  157. if(empty($description) && count($this->descriptions)){
  158. $description = $this->descriptions[0];
  159. }
  160. return $description ?? null;
  161. }
  162. /**
  163. * @ORM\PostLoad
  164. */
  165. public function postLoad(LifecycleEventArgs $args): void {
  166. if ($this->getAttribute()->getCode() === 'main_color') {
  167. $this->entity = $args->getEntityManager()->getRepository(Color::class)->findOneByCode($this->getValue());
  168. }else if($this->getAttribute()->getCode() === 'precise_color'){
  169. $this->entity = $args->getEntityManager()->getRepository(Color::class)->findOneByCode($this->getValue());
  170. }
  171. }
  172. }