Skip to content

Commit

Permalink
Serializer: Add support for PriceInterface.
Browse files Browse the repository at this point in the history
  • Loading branch information
janbarasek authored Jun 26, 2022
1 parent 9e77fd8 commit 52802b4
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Baraja\StructuredApi;


use Baraja\EcommerceStandard\DTO\PriceInterface;
use Baraja\Localization\Translation;
use Baraja\StructuredApi\Entity\Convention;
use Baraja\StructuredApi\Entity\ItemsList;
Expand Down Expand Up @@ -69,6 +70,9 @@ private function process(mixed $haystack, int $level): float|null|int|bool|array
if ($haystack instanceof \UnitEnum) {
return $this->processEnum($haystack);
}
if (interface_exists(PriceInterface::class) && $haystack instanceof PriceInterface) {
return $this->processPrice($haystack);
}
if ($this->convention->isRewriteTooStringMethod() && \method_exists($haystack, '__toString') === true) {
return (string) $haystack;
}
Expand Down Expand Up @@ -157,4 +161,18 @@ private function processEnum(\UnitEnum $enum): string|int
{
return $enum->value ?? $enum->name;
}


/**
* @return array{value: string, currency: string, html: string, isFree: bool}
*/
private function processPrice(PriceInterface $price): array
{
return [
'value' => $price->getValue(),
'currency' => $price->getCurrency()->getSymbol(),
'html' => $price->render(true),
'isFree' => $price->isFree(),
];
}
}

0 comments on commit 52802b4

Please sign in to comment.