RequestMapping.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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\Annotation;
  12. use Attribute;
  13. use Hyperf\Stringable\Str;
  14. #[Attribute(Attribute::TARGET_METHOD)]
  15. class RequestMapping extends Mapping
  16. {
  17. public const GET = 'GET';
  18. public const POST = 'POST';
  19. public const PUT = 'PUT';
  20. public const PATCH = 'PATCH';
  21. public const DELETE = 'DELETE';
  22. public const HEADER = 'HEADER';
  23. public const OPTIONS = 'OPTIONS';
  24. public function __construct(?string $path = null, array|string $methods = ['GET', 'POST'], array $options = [])
  25. {
  26. $formatted = [];
  27. if (is_string($methods)) {
  28. $formatted = explode(',', Str::upper(str_replace(' ', '', $methods)));
  29. } else {
  30. foreach ($methods as $method) {
  31. $formatted[] = Str::upper(str_replace(' ', '', $method));
  32. }
  33. }
  34. parent::__construct($path, $formatted, $options);
  35. }
  36. }