mirror of
https://github.com/Sylius/Sylius.git
synced 2026-07-14 09:02:12 +00:00
[API] Add ProductOptionValueNormalizer class
This commit is contained in:
parent
bd4c2e0353
commit
a3114a81dc
3 changed files with 151 additions and 0 deletions
|
|
@ -64,6 +64,11 @@
|
|||
<tag name="serializer.normalizer" priority="64" />
|
||||
</service>
|
||||
|
||||
<service id="sylius_api.normalizer.product_option_value" class="Sylius\Bundle\ApiBundle\Serializer\ProductOptionValueNormalizer">
|
||||
<argument type="service" id="Sylius\Component\Resource\Translation\TranslatableEntityLocaleAssignerInterface"/>
|
||||
<tag name="serializer.normalizer" priority="64" />
|
||||
</service>
|
||||
|
||||
<service id="Sylius\Bundle\ApiBundle\Serializer\ImageNormalizer">
|
||||
<argument type="service" id="liip_imagine.cache.manager" />
|
||||
<argument type="service" id="request_stack" />
|
||||
|
|
|
|||
|
|
@ -0,0 +1,56 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Sylius package.
|
||||
*
|
||||
* (c) Sylius Sp. z o.o.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Sylius\Bundle\ApiBundle\Serializer;
|
||||
|
||||
use Sylius\Component\Product\Model\ProductOptionValueInterface;
|
||||
use Sylius\Component\Resource\Translation\TranslatableEntityLocaleAssignerInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
use Webmozart\Assert\Assert;
|
||||
|
||||
final class ProductOptionValueNormalizer implements NormalizerInterface, NormalizerAwareInterface
|
||||
{
|
||||
use NormalizerAwareTrait;
|
||||
|
||||
private const ALREADY_CALLED = 'sylius_product_option_value_normalizer_already_called';
|
||||
|
||||
public function __construct(private readonly TranslatableEntityLocaleAssignerInterface $translatableEntityLocaleAssigner)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ProductOptionValueInterface $object
|
||||
*/
|
||||
public function normalize($object, $format = null, array $context = []): array
|
||||
{
|
||||
Assert::isInstanceOf($object, ProductOptionValueInterface::class);
|
||||
Assert::keyNotExists($context, self::ALREADY_CALLED);
|
||||
|
||||
$context[self::ALREADY_CALLED] = true;
|
||||
|
||||
$this->translatableEntityLocaleAssigner->assignLocale($object);
|
||||
|
||||
return $this->normalizer->normalize($object, $format, $context);
|
||||
}
|
||||
|
||||
public function supportsNormalization($data, $format = null, $context = []): bool
|
||||
{
|
||||
if (isset($context[self::ALREADY_CALLED])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $data instanceof ProductOptionValueInterface;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,90 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Sylius package.
|
||||
*
|
||||
* (c) Sylius Sp. z o.o.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace spec\Sylius\Bundle\ApiBundle\Serializer;
|
||||
|
||||
use PhpSpec\ObjectBehavior;
|
||||
use Sylius\Component\Core\Model\OrderInterface;
|
||||
use Sylius\Component\Product\Model\ProductOptionValueInterface;
|
||||
use Sylius\Component\Resource\Translation\TranslatableEntityLocaleAssignerInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
|
||||
final class ProductOptionValueNormalizerSpec extends ObjectBehavior
|
||||
{
|
||||
function let(
|
||||
NormalizerInterface $normalizer,
|
||||
TranslatableEntityLocaleAssignerInterface $translatableEntityLocaleAssigner,
|
||||
): void {
|
||||
$this->beConstructedWith($translatableEntityLocaleAssigner);
|
||||
|
||||
$this->setNormalizer($normalizer);
|
||||
}
|
||||
|
||||
function it_is_an_aware_normalizer(): void
|
||||
{
|
||||
$this->shouldImplement(NormalizerAwareInterface::class);
|
||||
}
|
||||
|
||||
function it_supports_only_product_option_value_interface(
|
||||
ProductOptionValueInterface $productOptionValue,
|
||||
OrderInterface $order,
|
||||
): void {
|
||||
$this->supportsNormalization($productOptionValue)->shouldReturn(true);
|
||||
$this->supportsNormalization($order)->shouldReturn(false);
|
||||
}
|
||||
|
||||
function it_supports_the_normalizer_has_not_called_yet(ProductOptionValueInterface $productOptionValue): void
|
||||
{
|
||||
$this
|
||||
->supportsNormalization($productOptionValue, null, [])
|
||||
->shouldReturn(true)
|
||||
;
|
||||
|
||||
$this
|
||||
->supportsNormalization($productOptionValue, null, ['sylius_product_option_value_normalizer_already_called' => true])
|
||||
->shouldReturn(false)
|
||||
;
|
||||
}
|
||||
|
||||
function it_assigns_locale_to_translatable_entity(
|
||||
NormalizerInterface $normalizer,
|
||||
ProductOptionValueInterface $productOptionValue,
|
||||
TranslatableEntityLocaleAssignerInterface $translatableEntityLocaleAssigner,
|
||||
): void {
|
||||
$normalizer
|
||||
->normalize($productOptionValue, null, ['sylius_product_option_value_normalizer_already_called' => true])
|
||||
->willReturn([])
|
||||
;
|
||||
|
||||
$translatableEntityLocaleAssigner->assignLocale($productOptionValue)->shouldBeCalled();
|
||||
|
||||
$this->normalize($productOptionValue, null, [])->shouldReturn([]);
|
||||
}
|
||||
|
||||
function it_throws_an_exception_if_the_given_object_is_not_a_product_option_value_interface(): void
|
||||
{
|
||||
$this
|
||||
->shouldThrow(\InvalidArgumentException::class)
|
||||
->during('normalize', [new \stdClass()])
|
||||
;
|
||||
}
|
||||
|
||||
function it_throws_an_exception_if_the_normalizer_was_already_called(ProductOptionValueInterface $productOptionValue): void
|
||||
{
|
||||
$this
|
||||
->shouldThrow(\InvalidArgumentException::class)
|
||||
->during('normalize', [$productOptionValue, null, ['sylius_product_option_value_normalizer_already_called' => true]])
|
||||
;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue