From 4e4bb0a29b7c8146cc821698515d57b344829bd5 Mon Sep 17 00:00:00 2001 From: Ambroise Maupate Date: Fri, 10 Jun 2022 15:16:39 +0200 Subject: [PATCH] fix: Nullable and strict typing AttributeValueTranslation --- src/Model/AttributeValueTranslationTrait.php | 26 +++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/Model/AttributeValueTranslationTrait.php b/src/Model/AttributeValueTranslationTrait.php index 0e9acb02..a2ad3cd4 100644 --- a/src/Model/AttributeValueTranslationTrait.php +++ b/src/Model/AttributeValueTranslationTrait.php @@ -37,11 +37,14 @@ trait AttributeValueTranslationTrait protected ?AttributeValueInterface $attributeValue = null; /** - * @return mixed + * @return mixed|null * @throws \Exception */ public function getValue() { + if (null === $this->value) { + return null; + } switch ($this->getAttributeValue()->getType()) { case AttributeInterface::DECIMAL_T: return (float) $this->value; @@ -58,33 +61,32 @@ public function getValue() } /** - * @param mixed $value + * @param mixed|null $value * - * @return mixed + * @return static */ public function setValue($value) { + if (null === $value) { + $this->value = null; + } switch ($this->getAttributeValue()->getType()) { case AttributeInterface::EMAIL_T: if (false === filter_var($value, FILTER_VALIDATE_EMAIL)) { throw new \InvalidArgumentException('Email is not valid'); } - $this->value = $value; + $this->value = (string) $value; return $this; case AttributeInterface::DATETIME_T: case AttributeInterface::DATE_T: if ($value instanceof \DateTime) { $this->value = $value->format('Y-m-d H:i:s'); } else { - $this->value = $value; + $this->value = (string) $value; } return $this; - case AttributeInterface::INTEGER_T: - case AttributeInterface::DECIMAL_T: - $this->value = (string) $value; - return $this; default: - $this->value = $value; + $this->value = (string) $value; return $this; } } @@ -92,7 +94,7 @@ public function setValue($value) /** * @param TranslationInterface $translation * - * @return mixed + * @return static */ public function setTranslation(TranslationInterface $translation) { @@ -119,7 +121,7 @@ public function getAttributeValue(): AttributeValueInterface /** * @param AttributeValueInterface $attributeValue * - * @return mixed + * @return static */ public function setAttributeValue(AttributeValueInterface $attributeValue) {