Skip to content

Commit 6ff54c7

Browse files
Improved validation logic for HTTP client option.
1 parent 960cefc commit 6ff54c7

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

src/HttpClient.php

+26-17
Original file line numberDiff line numberDiff line change
@@ -228,24 +228,33 @@ private static function validateOptions(array $options, array $allowedOptions =
228228
if (!in_array($key, $allowedOptions)) {
229229
throw new LogicException('Invalid option: ' . $key);
230230
}
231-
if ($key === 'headers' && !is_array($value)) {
232-
throw new LogicException('Headers must be an array of key-value pairs');
233-
}
234-
235-
if ($key === 'user_agent' && !is_string($value)) {
236-
throw new LogicException('User agent must be a string');
237-
}
238-
239-
if ($key === 'timeout' && !is_int($value)) {
240-
throw new LogicException('Timeout must be a float');
241-
}
242-
243-
if ($key === 'method' && (!is_string($value) || !in_array($value, ['GET', 'POST', 'PUT', 'DELETE', 'HEAD']))) {
244-
throw new LogicException('Method must be GET, POST, PUT, DELETE or HEAD');
245-
}
246231

247-
if ($key === 'base_url' && !empty($value) && !filter_var($value, FILTER_VALIDATE_URL)) {
248-
throw new LogicException('Base URL must be a valid URL');
232+
switch ($key) {
233+
case 'headers':
234+
if (!is_array($value)) {
235+
throw new LogicException('Headers must be an array of key-value pairs');
236+
}
237+
break;
238+
case 'user_agent':
239+
if (!is_string($value)) {
240+
throw new LogicException('User agent must be a string');
241+
}
242+
break;
243+
case 'timeout':
244+
if (!is_int($value)) {
245+
throw new LogicException('Timeout must be an integer');
246+
}
247+
break;
248+
case 'method':
249+
if (!is_string($value) || !in_array($value, ['GET', 'POST', 'PUT', 'DELETE', 'HEAD'])) {
250+
throw new LogicException('Method must be GET, POST, PUT, DELETE, or HEAD');
251+
}
252+
break;
253+
case 'base_url':
254+
if (!empty($value) && !filter_var($value, FILTER_VALIDATE_URL)) {
255+
throw new LogicException('Base URL must be a valid URL');
256+
}
257+
break;
249258
}
250259
}
251260
}

0 commit comments

Comments
 (0)