| 1: | <?php |
| 2: | |
| 3: | namespace LaravelUi5\Core\Enums; |
| 4: | |
| 5: | /** |
| 6: | * Enum representing supported HTTP methods. |
| 7: | * |
| 8: | * This enum is used across the LaravelUi5 core to reason about |
| 9: | * request semantics, especially in the context of Ui5Actions. |
| 10: | * |
| 11: | * - GET/HEAD/OPTIONS are considered safe/read-only methods. |
| 12: | * - POST/PUT/PATCH/DELETE are considered write/unsafe methods. |
| 13: | * |
| 14: | * Note on Ui5Actions: |
| 15: | * Ui5Actions intentionally support only POST, PATCH and DELETE. |
| 16: | * PUT is included in the enum for completeness, but is excluded |
| 17: | * from Ui5Actions because full-resource replacement semantics |
| 18: | * are not practical in typical SAP/OpenUI5 scenarios. |
| 19: | */ |
| 20: | enum HttpMethod: int |
| 21: | { |
| 22: | case GET = 1; |
| 23: | case POST = 2; |
| 24: | case PUT = 3; |
| 25: | case PATCH = 4; |
| 26: | case DELETE = 5; |
| 27: | case OPTIONS = 6; |
| 28: | case HEAD = 7; |
| 29: | |
| 30: | /** |
| 31: | * Returns the canonical string label for the HTTP method. |
| 32: | * |
| 33: | * @return string |
| 34: | */ |
| 35: | public function label(): string |
| 36: | { |
| 37: | return match ($this) { |
| 38: | self::GET => 'GET', |
| 39: | self::POST => 'POST', |
| 40: | self::PUT => 'PUT', |
| 41: | self::PATCH => 'PATCH', |
| 42: | self::DELETE => 'DELETE', |
| 43: | self::OPTIONS => 'OPTIONS', |
| 44: | self::HEAD => 'HEAD', |
| 45: | }; |
| 46: | } |
| 47: | |
| 48: | /** |
| 49: | * Whether this method is considered read-only and safe. |
| 50: | * |
| 51: | * GET, HEAD and OPTIONS are classified as read operations |
| 52: | * according to HTTP/1.1 semantics (RFC 7231). |
| 53: | * |
| 54: | * @return boolean |
| 55: | */ |
| 56: | public function isRead(): bool |
| 57: | { |
| 58: | return in_array($this, [self::GET, self::HEAD, self::OPTIONS], true); |
| 59: | } |
| 60: | |
| 61: | /** |
| 62: | * Whether this method is considered write/unsafe. |
| 63: | * |
| 64: | * POST, PUT, PATCH and DELETE are classified as write operations. |
| 65: | * |
| 66: | * @return boolean |
| 67: | */ |
| 68: | public function isWrite(): bool |
| 69: | { |
| 70: | return in_array($this, [self::POST, self::PUT, self::PATCH, self::DELETE], true); |
| 71: | } |
| 72: | |
| 73: | /** |
| 74: | * Whether this method is valid for Ui5Actions. |
| 75: | * |
| 76: | * Ui5Actions explicitly allow only POST, PATCH and DELETE: |
| 77: | * - POST: execute a business action or create resource(s) |
| 78: | * - PATCH: apply (partial) updates |
| 79: | * - DELETE: remove a resource |
| 80: | * |
| 81: | * PUT is intentionally excluded because full-resource |
| 82: | * replacement semantics are not useful in Ui5 contexts. |
| 83: | * |
| 84: | * @return boolean |
| 85: | */ |
| 86: | public function isValidUi5ActionMethod(): bool |
| 87: | { |
| 88: | return in_array($this, [self::POST, self::PATCH, self::DELETE], true); |
| 89: | } |
| 90: | } |
| 91: |