friendship ended with social-app. php is my new best friend
1---
2title: Method Enum API Reference
3description: API reference for the Method enum in the Fetch HTTP client package
4---
5
6# Method Enum
7
8The `Method` enum represents HTTP request methods supported by the Fetch package. It provides type-safe constants for HTTP methods and helper methods to work with them.
9
10## Namespace
11
12```php
13namespace Fetch\Enum;
14```
15
16## Definition
17
18```php
19enum Method: string
20{
21 case GET = 'GET';
22 case POST = 'POST';
23 case PUT = 'PUT';
24 case PATCH = 'PATCH';
25 case DELETE = 'DELETE';
26 case HEAD = 'HEAD';
27 case OPTIONS = 'OPTIONS';
28
29 /**
30 * Get the method from a string.
31 */
32 public static function fromString(string $method): self
33 {
34 return self::from(strtoupper($method));
35 }
36
37 /**
38 * Try to get the method from a string, or return default.
39 */
40 public static function tryFromString(string $method, ?self $default = null): ?self
41 {
42 return self::tryFrom(strtoupper($method)) ?? $default;
43 }
44
45 /**
46 * Determine if the method supports a request body.
47 */
48 public function supportsRequestBody(): bool
49 {
50 return in_array($this, [self::POST, self::PUT, self::PATCH, self::DELETE]);
51 }
52}
53```
54
55## Available Constants
56
57 a GET request, but without the response body. |
58| `Method::OPTIONS` | `"OPTIONS"` | The OPTIONS method describes the communication options for the target resource. |
59| `Method::TRACE` | `"TRACE"` | The TRACE method performs a message loop-back test along the path to the target resource. |
60| `Method::CONNECT` | `"CONNECT"` | The CONNECT method establishes a tunnel to the server identified by the target resource. |
61
62## Methods
63
64### fromString()
65
66Converts a string to a Method enum value. Throws an exception if the string doesn't match any valid method.
67
68```php
69public static function fromString(string $method): self
70```
71
72**Parameters:**
73
74- `$method`: A string representing an HTTP method (case-insensitive)
75
76**Returns:**
77
78- The corresponding Method enum value
79
80**Throws:**
81
82- `\ValueError` if the string doesn't represent a valid HTTP method
83
84**Example:**
85
86```php
87$method = Method::fromString('post'); // Returns Method::POST
88```
89
90### tryFromString()
91
92Attempts to convert a string to a Method enum value. Returns a default value if the string doesn't match any valid method.
93
94```php
95public static function tryFromString(string $method, ?self $default = null): ?self
96```
97
98**Parameters:**
99
100- `$method`: A string representing an HTTP method (case-insensitive)
101- `$default`: The default enum value to return if the string doesn't match (defaults to null)
102
103**Returns:**
104
105- The corresponding Method enum value or the default value
106
107**Example:**
108
109```php
110$method = Method::tryFromString('post'); // Returns Method::POST
111$method = Method::tryFromString('INVALID', Method::GET); // Returns Method::GET
112```
113
114### supportsRequestBody()
115
116Determines whether the HTTP method supports a request body.
117
118```php
119public function supportsRequestBody(): bool
120```
121
122**Returns:**
123
124- `true` for POST, PUT, PATCH, and DELETE methods
125- `false` for GET, HEAD, and OPTIONS methods
126
127**Example:**
128
129```php
130if (Method::POST->supportsRequestBody()) {
131 // Configure request body
132}
133```
134
135## Usage Examples
136
137### With ClientHandler
138
139```php
140use Fetch\Enum\Method;
141use Fetch\Http\ClientHandler;
142
143// Using enum directly
144$response = ClientHandler::handle(Method::GET->value, 'https://api.example.com/users');
145
146// Checking for request body support
147$method = Method::POST;
148if ($method->supportsRequestBody()) {
149 // Configure the request body
150}
151```
152
153### Converting from String
154
155```php
156use Fetch\Enum\Method;
157
158// From request input (safely handling potential errors)
159$methodString = $_POST['method'] ?? 'GET';
160$method = Method::tryFromString($methodString, Method::GET);
161
162// Converting when you expect the method to be valid
163try {
164 $method = Method::fromString('PATCH');
165} catch (\ValueError $e) {
166 // Handle invalid method
167}
168```
169
170### In Method Selection Logic
171
172```php
173use Fetch\Enum\Method;
174
175function processRequest(string $methodString, string $uri, ?array $body): Response
176{
177 $method = Method::tryFromString($methodString, Method::GET);
178
179 return match($method) {
180 Method::GET => fetch_client()->get($uri),
181 Method::POST => fetch_client()->post($uri, $body),
182 Method::PUT => fetch_client()->put($uri, $body),
183 Method::PATCH => fetch_client()->patch($uri, $body),
184 Method::DELETE => fetch_client()->delete($uri, $body),
185 default => throw new InvalidArgumentException("Unsupported method: {$methodString}")
186 };
187}
188```