|
| 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\Serializer; |
| 24 | + |
| 25 | +use PrestaShopBundle\ApiPlatform\ContextParametersProvider; |
| 26 | +use PrestaShopBundle\ApiPlatform\LocalizedValueUpdater; |
| 27 | +use PrestaShopBundle\ApiPlatform\NormalizationMapper; |
| 28 | +use PrestaShopBundle\ApiPlatform\Serializer\CQRSApiSerializer; |
| 29 | +use ReflectionClass; |
| 30 | +use ReflectionException; |
| 31 | +use ReflectionNamedType; |
| 32 | +use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface; |
| 33 | +use Symfony\Component\Serializer\Serializer; |
| 34 | + |
| 35 | +/** |
| 36 | + * Extends CQRSApiSerializer to add automatic type casting for query parameters. |
| 37 | + */ |
| 38 | +class QueryParameterTypeCastSerializer extends CQRSApiSerializer |
| 39 | +{ |
| 40 | + public function __construct( |
| 41 | + Serializer $decorated, |
| 42 | + ContextParametersProvider $contextParametersProvider, |
| 43 | + ClassMetadataFactoryInterface $classMetadataFactory, |
| 44 | + LocalizedValueUpdater $localizedValueUpdater, |
| 45 | + NormalizationMapper $normalizationMapper, |
| 46 | + ) { |
| 47 | + parent::__construct($decorated, $contextParametersProvider, $classMetadataFactory, $localizedValueUpdater, $normalizationMapper); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * {@inheritdoc} |
| 52 | + */ |
| 53 | + public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed |
| 54 | + { |
| 55 | + if (is_array($data) && str_starts_with($type, 'PrestaShop\\PrestaShop\\Core\\Domain\\')) { |
| 56 | + $data = $this->castQueryParametersToExpectedTypes($data, $type); |
| 57 | + } |
| 58 | + |
| 59 | + return parent::denormalize($data, $type, $format, $context); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Cast string query parameter values to their expected types based on the CQRS query constructor. |
| 64 | + */ |
| 65 | + private function castQueryParametersToExpectedTypes(array $data, string $queryClass): array |
| 66 | + { |
| 67 | + try { |
| 68 | + $reflection = new ReflectionClass($queryClass); |
| 69 | + $constructor = $reflection->getConstructor(); |
| 70 | + |
| 71 | + if (!$constructor) { |
| 72 | + return $data; |
| 73 | + } |
| 74 | + |
| 75 | + foreach ($constructor->getParameters() as $parameter) { |
| 76 | + $paramName = $parameter->getName(); |
| 77 | + |
| 78 | + if (!array_key_exists($paramName, $data) || !is_string($data[$paramName])) { |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + $type = $parameter->getType(); |
| 83 | + |
| 84 | + if (!$type instanceof ReflectionNamedType || !$type->isBuiltin()) { |
| 85 | + continue; |
| 86 | + } |
| 87 | + |
| 88 | + $data[$paramName] = match ($type->getName()) { |
| 89 | + 'int' => (int) $data[$paramName], |
| 90 | + 'float' => (float) $data[$paramName], |
| 91 | + 'bool' => filter_var($data[$paramName], FILTER_VALIDATE_BOOLEAN), |
| 92 | + default => $data[$paramName], |
| 93 | + }; |
| 94 | + } |
| 95 | + } catch (ReflectionException $e) { |
| 96 | + return $data; |
| 97 | + } |
| 98 | + |
| 99 | + return $data; |
| 100 | + } |
| 101 | +} |
| 102 | + |
0 commit comments