MethodRequestMatcher.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 HTTP method of a Request.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. class MethodRequestMatcher implements RequestMatcherInterface
  19. {
  20. /**
  21. * @var string[]
  22. */
  23. private array $methods = [];
  24. /**
  25. * @param string[]|string $methods An HTTP method or an array of HTTP methods
  26. * Strings can contain a comma-delimited list of methods
  27. */
  28. public function __construct(array|string $methods)
  29. {
  30. $this->methods = array_reduce(array_map('strtoupper', (array) $methods), static fn (array $methods, string $method) => array_merge($methods, preg_split('/\s*,\s*/', $method)), []);
  31. }
  32. public function matches(Request $request): bool
  33. {
  34. if (!$this->methods) {
  35. return true;
  36. }
  37. return \in_array($request->getMethod(), $this->methods, true);
  38. }
  39. }