Dispatched.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\HttpServer\Router;
  12. use FastRoute\Dispatcher;
  13. class Dispatched
  14. {
  15. public int $status;
  16. public ?Handler $handler = null;
  17. public array $params = [];
  18. /**
  19. * Dispatches against the provided HTTP method verb and URI.
  20. *
  21. * @param array $array with one of the following formats:
  22. *
  23. * [Dispatcher::NOT_FOUND]
  24. * [Dispatcher::METHOD_NOT_ALLOWED, ['GET', 'OTHER_ALLOWED_METHODS']]
  25. * [Dispatcher::FOUND, $handler, ['varName' => 'value', ...]]
  26. */
  27. public function __construct(array $array, public ?string $serverName = null)
  28. {
  29. $this->status = $array[0];
  30. switch ($this->status) {
  31. case Dispatcher::METHOD_NOT_ALLOWED:
  32. $this->params = $array[1];
  33. break;
  34. case Dispatcher::FOUND:
  35. $this->handler = $array[1];
  36. $this->params = $array[2];
  37. break;
  38. }
  39. }
  40. public function isFound(): bool
  41. {
  42. return $this->status === Dispatcher::FOUND;
  43. }
  44. public function isNotFound(): bool
  45. {
  46. return $this->status === Dispatcher::NOT_FOUND;
  47. }
  48. }