src/Entity/ProductModelAttribute.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
  5. use App\Entity\ProductModel;
  6. /**
  7. * ProductsAttributes
  8. *
  9. * @ORM\Table(name="products_models_attributes")
  10. * @ORM\Entity
  11. * @ORM\HasLifecycleCallbacks()
  12. */
  13. class ProductModelAttribute extends TranslatedEntity {
  14. /**
  15. * @var int
  16. *
  17. * @ORM\Column(name="id", type="integer", nullable=false)
  18. * @ORM\Id
  19. * @ORM\GeneratedValue(strategy="IDENTITY")
  20. */
  21. private $id;
  22. /**
  23. * @var ProductModel
  24. *
  25. * @ORM\ManyToOne(targetEntity="App\Entity\ProductModel", inversedBy="attributes", cascade={"persist"})
  26. * @ORM\JoinColumn(name="products_models_id", referencedColumnName="id", onDelete="CASCADE")
  27. */
  28. private $productModel;
  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. * @var ?string
  38. *
  39. * @ORM\Column(name="attributes_value", type="string", length=255, nullable=true)
  40. */
  41. private $value;
  42. /**
  43. * @ORM\OneToMany(targetEntity="App\Entity\ProductModelAttributeDescription", mappedBy="productModelAttribute", cascade={"persist", "remove"})
  44. */
  45. private $descriptions = [];
  46. public function getId(): int {
  47. return $this->id;
  48. }
  49. public function getProductModel(): ProductModel {
  50. return $this->productModel;
  51. }
  52. public function getAttribute(): Attribute {
  53. return $this->attribute;
  54. }
  55. public function getValue(): ?string {
  56. return $this->value;
  57. }
  58. public function setId(int $id): void {
  59. $this->id = $id;
  60. }
  61. public function setAttribute(Attribute $attribute): void {
  62. $this->attribute = $attribute;
  63. }
  64. public function setValue(?string $value): void {
  65. $this->value = $value;
  66. }
  67. public function setProductModel(ProductModel $productModel): void {
  68. $this->productModel = $productModel;
  69. }
  70. public function getDisplayedValue(): ?string {
  71. if ($this->value === null) {
  72. return null;
  73. }
  74. if(strpos($this->getAttribute()->getCode(), '-unit')!==false) {
  75. switch($this->value) {
  76. case 'MILLIMETER' : return 'mm';
  77. case 'MILLILITER' : return 'ml';
  78. case 'GRAM' : return 'g';
  79. case 'MONTH' : return '';
  80. default:;
  81. }
  82. }else{
  83. $unit = $this->product->getAttribute($this->getAttribute()->getCode() . '-unit');
  84. if ($unit && $unit->getValue()) {
  85. if($unit->getDisplayedValue() == 'ml') {
  86. $value = intval($this->getValue());
  87. if($value >= 1000) {
  88. return intval($this->getValue()/1000) . 'l';
  89. }
  90. return intval($this->getValue()) . $unit->getDisplayedValue();
  91. }
  92. return $this->getValue() . $unit->getDisplayedValue();
  93. }
  94. }
  95. return $this->getValue();
  96. }
  97. }