From 52802b416b986f7b36f73abbcf49a14977b01892 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bar=C3=A1=C5=A1ek?= Date: Sun, 26 Jun 2022 22:28:33 +0200 Subject: [PATCH] Serializer: Add support for PriceInterface. --- src/Serializer.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/Serializer.php b/src/Serializer.php index c324163..144b6a2 100644 --- a/src/Serializer.php +++ b/src/Serializer.php @@ -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; @@ -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; } @@ -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(), + ]; + } }