AttributesRequestMatcher.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 Request attributes matches all regular expressions.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. class AttributesRequestMatcher implements RequestMatcherInterface
  19. {
  20. /**
  21. * @param array<string, string> $regexps
  22. */
  23. public function __construct(private array $regexps)
  24. {
  25. }
  26. public function matches(Request $request): bool
  27. {
  28. foreach ($this->regexps as $key => $regexp) {
  29. $attribute = $request->attributes->get($key);
  30. if (!\is_string($attribute)) {
  31. return false;
  32. }
  33. if (!preg_match('{'.$regexp.'}', $attribute)) {
  34. return false;
  35. }
  36. }
  37. return true;
  38. }
  39. }