QueryParameterValueResolver.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\Controller\ArgumentResolver;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpKernel\Attribute\MapQueryParameter;
  13. use Symfony\Component\HttpKernel\Controller\ValueResolverInterface;
  14. use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
  15. use Symfony\Component\HttpKernel\Exception\HttpException;
  16. /**
  17. * Resolve arguments of type: array, string, int, float, bool, \BackedEnum from query parameters.
  18. *
  19. * @author Ruud Kamphuis <ruud@ticketswap.com>
  20. * @author Nicolas Grekas <p@tchwork.com>
  21. * @author Mateusz Anders <anders_mateusz@outlook.com>
  22. * @author Ionut Enache <i.ovidiuenache@yahoo.com>
  23. */
  24. final class QueryParameterValueResolver implements ValueResolverInterface
  25. {
  26. public function resolve(Request $request, ArgumentMetadata $argument): array
  27. {
  28. if (!$attribute = $argument->getAttributesOfType(MapQueryParameter::class)[0] ?? null) {
  29. return [];
  30. }
  31. $name = $attribute->name ?? $argument->getName();
  32. $validationFailedCode = $attribute->validationFailedStatusCode;
  33. if (!$request->query->has($name)) {
  34. if ($argument->isNullable() || $argument->hasDefaultValue()) {
  35. return [];
  36. }
  37. throw HttpException::fromStatusCode($validationFailedCode, \sprintf('Missing query parameter "%s".', $name));
  38. }
  39. $value = $request->query->all()[$name];
  40. $type = $argument->getType();
  41. if (null === $attribute->filter && 'array' === $type) {
  42. if (!$argument->isVariadic()) {
  43. return [(array) $value];
  44. }
  45. $filtered = array_values(array_filter((array) $value, \is_array(...)));
  46. if ($filtered !== $value && !($attribute->flags & \FILTER_NULL_ON_FAILURE)) {
  47. throw HttpException::fromStatusCode($validationFailedCode, \sprintf('Invalid query parameter "%s".', $name));
  48. }
  49. return $filtered;
  50. }
  51. $options = [
  52. 'flags' => $attribute->flags | \FILTER_NULL_ON_FAILURE,
  53. 'options' => $attribute->options,
  54. ];
  55. if ('array' === $type || $argument->isVariadic()) {
  56. $value = (array) $value;
  57. $options['flags'] |= \FILTER_REQUIRE_ARRAY;
  58. } else {
  59. $options['flags'] |= \FILTER_REQUIRE_SCALAR;
  60. }
  61. $enumType = null;
  62. $filter = match ($type) {
  63. 'array' => \FILTER_DEFAULT,
  64. 'string' => \FILTER_DEFAULT,
  65. 'int' => \FILTER_VALIDATE_INT,
  66. 'float' => \FILTER_VALIDATE_FLOAT,
  67. 'bool' => \FILTER_VALIDATE_BOOL,
  68. default => match ($enumType = is_subclass_of($type, \BackedEnum::class) ? (new \ReflectionEnum($type))->getBackingType()->getName() : null) {
  69. 'int' => \FILTER_VALIDATE_INT,
  70. 'string' => \FILTER_DEFAULT,
  71. default => throw new \LogicException(\sprintf('#[MapQueryParameter] cannot be used on controller argument "%s$%s" of type "%s"; one of array, string, int, float, bool or \BackedEnum should be used.', $argument->isVariadic() ? '...' : '', $argument->getName(), $type ?? 'mixed')),
  72. },
  73. };
  74. $value = filter_var($value, $attribute->filter ?? $filter, $options);
  75. if (null !== $enumType && null !== $value) {
  76. $enumFrom = static function ($value) use ($type) {
  77. if (!\is_string($value) && !\is_int($value)) {
  78. return null;
  79. }
  80. try {
  81. return $type::from($value);
  82. } catch (\ValueError) {
  83. return null;
  84. }
  85. };
  86. $value = \is_array($value) ? array_map($enumFrom, $value) : $enumFrom($value);
  87. }
  88. if (null === $value && !($attribute->flags & \FILTER_NULL_ON_FAILURE)) {
  89. throw HttpException::fromStatusCode($validationFailedCode, \sprintf('Invalid query parameter "%s".', $name));
  90. }
  91. if (!\is_array($value)) {
  92. return [$value];
  93. }
  94. $filtered = array_filter($value, static fn ($v) => null !== $v);
  95. if ($argument->isVariadic()) {
  96. $filtered = array_values($filtered);
  97. }
  98. if ($filtered !== $value && !($attribute->flags & \FILTER_NULL_ON_FAILURE)) {
  99. throw HttpException::fromStatusCode($validationFailedCode, \sprintf('Invalid query parameter "%s".', $name));
  100. }
  101. return $argument->isVariadic() ? $filtered : [$filtered];
  102. }
  103. }