|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright since 2007 PrestaShop SA and Contributors |
| 4 | + * PrestaShop is an International Registered Trademark & Property of PrestaShop SA |
| 5 | + * |
| 6 | + * NOTICE OF LICENSE |
| 7 | + * |
| 8 | + * This source file is subject to the Academic Free License version 3.0 |
| 9 | + * that is bundled with this package in the file LICENSE.md. |
| 10 | + * It is also available through the world-wide-web at this URL: |
| 11 | + * https://opensource.org/licenses/AFL-3.0 |
| 12 | + * If you did not receive a copy of the license and are unable to |
| 13 | + * obtain it through the world-wide-web, please send an email |
| 14 | + * to license@prestashop.com so we can send you a copy immediately. |
| 15 | + * |
| 16 | + * @author PrestaShop SA and Contributors <contact@prestashop.com> |
| 17 | + * @copyright Since 2007 PrestaShop SA and Contributors |
| 18 | + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 |
| 19 | + */ |
| 20 | + |
| 21 | +declare(strict_types=1); |
| 22 | + |
| 23 | +namespace PrestaShop\Module\APIResources\Normalizer; |
| 24 | + |
| 25 | +use PrestaShop\PrestaShop\Core\Domain\Product\Query\SearchProducts; |
| 26 | +use PrestaShopBundle\ApiPlatform\DomainObjectDetector; |
| 27 | +use PrestaShopBundle\ApiPlatform\LocalizedValueUpdater; |
| 28 | +use PrestaShopBundle\ApiPlatform\Normalizer\CQRSApiNormalizer; |
| 29 | +use PrestaShopBundle\ApiPlatform\Validator\CQRSApiValidator; |
| 30 | +use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
| 31 | +use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface; |
| 32 | +use Symfony\Component\Serializer\Mapping\ClassDiscriminatorResolverInterface; |
| 33 | +use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface; |
| 34 | +use Symfony\Component\Serializer\NameConverter\NameConverterInterface; |
| 35 | + |
| 36 | +class SearchProductsDenormalizer extends CQRSApiNormalizer |
| 37 | +{ |
| 38 | + public function __construct( |
| 39 | + DomainObjectDetector $domainObjectDetector, |
| 40 | + LocalizedValueUpdater $localizedValueUpdater, |
| 41 | + CQRSApiValidator $CQRSApiValidator, |
| 42 | + ?ClassMetadataFactoryInterface $classMetadataFactory = null, |
| 43 | + ?NameConverterInterface $nameConverter = null, |
| 44 | + ?PropertyAccessorInterface $propertyAccessor = null, |
| 45 | + ?PropertyTypeExtractorInterface $propertyTypeExtractor = null, |
| 46 | + ?ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null, |
| 47 | + ?callable $objectClassResolver = null, |
| 48 | + array $defaultContext = [], |
| 49 | + ) { |
| 50 | + parent::__construct( |
| 51 | + $domainObjectDetector, |
| 52 | + $localizedValueUpdater, |
| 53 | + $CQRSApiValidator, |
| 54 | + $classMetadataFactory, |
| 55 | + $nameConverter, |
| 56 | + $propertyAccessor, |
| 57 | + $propertyTypeExtractor, |
| 58 | + $classDiscriminatorResolver, |
| 59 | + $objectClassResolver, |
| 60 | + $defaultContext |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed |
| 65 | + { |
| 66 | + if ($type === SearchProducts::class && is_array($data)) { |
| 67 | + if (isset($data['resultsLimit']) && is_string($data['resultsLimit'])) { |
| 68 | + $data['resultsLimit'] = (int) $data['resultsLimit']; |
| 69 | + } |
| 70 | + |
| 71 | + if (isset($data['orderId']) && is_string($data['orderId'])) { |
| 72 | + $data['orderId'] = (int) $data['orderId']; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return parent::denormalize($data, $type, $format, $context); |
| 77 | + } |
| 78 | + |
| 79 | + public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool |
| 80 | + { |
| 81 | + if ($type === SearchProducts::class) { |
| 82 | + return parent::supportsDenormalization($data, $type, $format, $context); |
| 83 | + } |
| 84 | + |
| 85 | + return false; |
| 86 | + } |
| 87 | + |
| 88 | + public function getSupportedTypes(?string $format): array |
| 89 | + { |
| 90 | + return [ |
| 91 | + SearchProducts::class => true, |
| 92 | + ]; |
| 93 | + } |
| 94 | +} |
0 commit comments