src/Entity/ProductAttribute.php line 143

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. return $this->getValue();
  84. }
  85. public function setId(int $id): void {
  86. $this->id = $id;
  87. }
  88. public function setProduct(Product $product): void {
  89. $this->product = $product;
  90. }
  91. public function setAttribute(Attribute $attribute): void {
  92. $this->attribute = $attribute;
  93. }
  94. public function setValue(?string $value): void {
  95. $this->value = $value;
  96. }
  97. public function getDescriptions() {
  98. return $this->descriptions;
  99. }
  100. public function setDescriptions($descriptions): void {
  101. $this->descriptions = $descriptions;
  102. }
  103. public function addDescription(ProductAttributeDescription $description) {
  104. $this->descriptions->add($description);
  105. }
  106. public function getDescription($lang = 'fr') {
  107. foreach($this->getDescriptions() as $desc){
  108. if($desc->getLanguage()->getCode() == $lang)
  109. return $desc;
  110. }
  111. return null;
  112. }
  113. /**
  114. * @ORM\PostLoad
  115. */
  116. public function postLoad(LifecycleEventArgs $args): void {
  117. if ($this->getAttribute()->getCode() === 'main_color') {
  118. $this->entity = $args->getEntityManager()->getRepository(Color::class)->findOneByCode($this->getValue());
  119. }else if($this->getAttribute()->getCode() === 'precise_color'){
  120. $this->entity = $args->getEntityManager()->getRepository(Color::class)->findOneByCode($this->getValue());
  121. }
  122. }
  123. }