Skip to content

Commit

Permalink
fix: Nullable and strict typing AttributeValueTranslation
Browse files Browse the repository at this point in the history
  • Loading branch information
ambroisemaupate committed Jun 10, 2022
1 parent 0d0dacc commit 4e4bb0a
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/Model/AttributeValueTranslationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -58,41 +61,40 @@ 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;
}
}

/**
* @param TranslationInterface $translation
*
* @return mixed
* @return static
*/
public function setTranslation(TranslationInterface $translation)
{
Expand All @@ -119,7 +121,7 @@ public function getAttributeValue(): AttributeValueInterface
/**
* @param AttributeValueInterface $attributeValue
*
* @return mixed
* @return static
*/
public function setAttributeValue(AttributeValueInterface $attributeValue)
{
Expand Down

0 comments on commit 4e4bb0a

Please sign in to comment.