QueryParameterRequestMatcher.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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\HttpFoundation\RequestMatcher;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\RequestMatcherInterface;
  13. /**
  14. * Checks the presence of HTTP query parameters of a Request.
  15. *
  16. * @author Alexandre Daubois <alex.daubois@gmail.com>
  17. */
  18. class QueryParameterRequestMatcher implements RequestMatcherInterface
  19. {
  20. /**
  21. * @var string[]
  22. */
  23. private array $parameters;
  24. /**
  25. * @param string[]|string $parameters A parameter or a list of parameters
  26. * Strings can contain a comma-delimited list of query parameters
  27. */
  28. public function __construct(array|string $parameters)
  29. {
  30. $this->parameters = array_reduce(array_map(strtolower(...), (array) $parameters), static fn (array $parameters, string $parameter) => array_merge($parameters, preg_split('/\s*,\s*/', $parameter)), []);
  31. }
  32. public function matches(Request $request): bool
  33. {
  34. if (!$this->parameters) {
  35. return true;
  36. }
  37. return 0 === \count(array_diff_assoc($this->parameters, $request->query->keys()));
  38. }
  39. }