QueryParameterValueResolver.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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\NotFoundHttpException;
  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. */
  23. final class QueryParameterValueResolver implements ValueResolverInterface
  24. {
  25. public function resolve(Request $request, ArgumentMetadata $argument): array
  26. {
  27. if (!$attribute = $argument->getAttributesOfType(MapQueryParameter::class)[0] ?? null) {
  28. return [];
  29. }
  30. $name = $attribute->name ?? $argument->getName();
  31. if (!$request->query->has($name)) {
  32. if ($argument->isNullable() || $argument->hasDefaultValue()) {
  33. return [];
  34. }
  35. throw new NotFoundHttpException(\sprintf('Missing query parameter "%s".', $name));
  36. }
  37. $value = $request->query->all()[$name];
  38. $type = $argument->getType();
  39. if (null === $attribute->filter && 'array' === $type) {
  40. if (!$argument->isVariadic()) {
  41. return [(array) $value];
  42. }
  43. $filtered = array_values(array_filter((array) $value, \is_array(...)));
  44. if ($filtered !== $value && !($attribute->flags & \FILTER_NULL_ON_FAILURE)) {
  45. throw new NotFoundHttpException(\sprintf('Invalid query parameter "%s".', $name));
  46. }
  47. return $filtered;
  48. }
  49. $options = [
  50. 'flags' => $attribute->flags | \FILTER_NULL_ON_FAILURE,
  51. 'options' => $attribute->options,
  52. ];
  53. if ('array' === $type || $argument->isVariadic()) {
  54. $value = (array) $value;
  55. $options['flags'] |= \FILTER_REQUIRE_ARRAY;
  56. } else {
  57. $options['flags'] |= \FILTER_REQUIRE_SCALAR;
  58. }
  59. $enumType = null;
  60. $filter = match ($type) {
  61. 'array' => \FILTER_DEFAULT,
  62. 'string' => \FILTER_DEFAULT,
  63. 'int' => \FILTER_VALIDATE_INT,
  64. 'float' => \FILTER_VALIDATE_FLOAT,
  65. 'bool' => \FILTER_VALIDATE_BOOL,
  66. default => match ($enumType = is_subclass_of($type, \BackedEnum::class) ? (new \ReflectionEnum($type))->getBackingType()->getName() : null) {
  67. 'int' => \FILTER_VALIDATE_INT,
  68. 'string' => \FILTER_DEFAULT,
  69. 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')),
  70. },
  71. };
  72. $value = filter_var($value, $attribute->filter ?? $filter, $options);
  73. if (null !== $enumType && null !== $value) {
  74. $enumFrom = static function ($value) use ($type) {
  75. if (!\is_string($value) && !\is_int($value)) {
  76. return null;
  77. }
  78. try {
  79. return $type::from($value);
  80. } catch (\ValueError) {
  81. return null;
  82. }
  83. };
  84. $value = \is_array($value) ? array_map($enumFrom, $value) : $enumFrom($value);
  85. }
  86. if (null === $value && !($attribute->flags & \FILTER_NULL_ON_FAILURE)) {
  87. throw new NotFoundHttpException(\sprintf('Invalid query parameter "%s".', $name));
  88. }
  89. if (!\is_array($value)) {
  90. return [$value];
  91. }
  92. $filtered = array_filter($value, static fn ($v) => null !== $v);
  93. if ($argument->isVariadic()) {
  94. $filtered = array_values($filtered);
  95. }
  96. if ($filtered !== $value && !($attribute->flags & \FILTER_NULL_ON_FAILURE)) {
  97. throw new NotFoundHttpException(\sprintf('Invalid query parameter "%s".', $name));
  98. }
  99. return $argument->isVariadic() ? $filtered : [$filtered];
  100. }
  101. }