HttpMethodMap.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Framework\App\Request;
  8. /**
  9. * Map of HTTP methods and interfaces that an action implements in order to process them.
  10. */
  11. class HttpMethodMap
  12. {
  13. /**
  14. * @var string[]
  15. */
  16. private $map;
  17. /**
  18. * @param string[] $map
  19. */
  20. public function __construct(array $map)
  21. {
  22. $this->map = $this->processMap($map);
  23. }
  24. /**
  25. * Filter given map.
  26. *
  27. * @param array $map
  28. * @throws \InvalidArgumentException
  29. *
  30. * @return string[]
  31. */
  32. private function processMap(array $map): array
  33. {
  34. $filtered = [];
  35. foreach ($map as $method => $interface) {
  36. $interface = trim(preg_replace('/^\\\+/', '', $interface));
  37. if (!(interface_exists($interface) || class_exists($interface))) {
  38. throw new \InvalidArgumentException(
  39. "Interface '$interface' does not exist"
  40. );
  41. }
  42. if (!$method) {
  43. throw new \InvalidArgumentException('Invalid method given');
  44. }
  45. $filtered[$method] = $interface;
  46. }
  47. return $filtered;
  48. }
  49. /**
  50. * Where keys are methods' names and values are interfaces' names.
  51. *
  52. * @return string[]
  53. *
  54. * @see \Zend\Http\Request Has list of methods as METHOD_* constants.
  55. */
  56. public function getMap(): array
  57. {
  58. return $this->map;
  59. }
  60. }