@@ -46,7 +46,7 @@ abstract class AbstractProvider implements ProviderInterface
46
46
*
47
47
* @param array|\ArrayAccess $options Provider options array.
48
48
* @param HttpFactory $httpFactory The http factory
49
- *
49
+ *
50
50
* @throws \InvalidArgumentException
51
51
* @since ___DEPLOY_VERSION___
52
52
*/
@@ -65,10 +65,10 @@ public function __construct(array $options = [], ?HttpFactory $httpFactory = nul
65
65
66
66
/**
67
67
* Get an option from the AI provider.
68
- *
68
+ *
69
69
* @param string $key The name of the option to get.
70
70
* @param mixed $default The default value if the option is not set.
71
- *
71
+ *
72
72
* @return mixed The option value.
73
73
* @since ___DEPLOY_VERSION___
74
74
*/
@@ -92,9 +92,9 @@ protected function makeGetRequest(string $url, array $headers = [], $timeout = n
92
92
{
93
93
try {
94
94
$ response = $ this ->httpFactory ->getHttp ([])->get ($ url , $ headers , $ timeout );
95
-
95
+
96
96
$ this ->validateResponse ($ response );
97
- } catch (AuthenticationException | RateLimitException | QuotaExceededException $ e ) {
97
+ } catch (AuthenticationException | RateLimitException | QuotaExceededException $ e ) {
98
98
throw $ e ;
99
99
} catch (ProviderException $ e ) {
100
100
throw $ e ;
@@ -106,7 +106,7 @@ protected function makeGetRequest(string $url, array $headers = [], $timeout = n
106
106
/**
107
107
* Make HTTP POST request.
108
108
*
109
- * @param string $url API endpoint URL
109
+ * @param string $url API endpoint URL
110
110
* @param mixed $data POST data
111
111
* @param array $headers HTTP headers
112
112
* @param integer $timeout Request timeout
@@ -119,10 +119,9 @@ protected function makePostRequest(string $url, $data, array $headers = [], $tim
119
119
{
120
120
try {
121
121
$ response = $ this ->httpFactory ->getHttp ([])->post ($ url , $ data , $ headers , $ timeout );
122
-
122
+
123
123
$ this ->validateResponse ($ response );
124
-
125
- } catch (AuthenticationException |RateLimitException |QuotaExceededException $ e ) {
124
+ } catch (AuthenticationException | RateLimitException | QuotaExceededException $ e ) {
126
125
throw $ e ;
127
126
} catch (ProviderException $ e ) {
128
127
throw $ e ;
@@ -134,7 +133,7 @@ protected function makePostRequest(string $url, $data, array $headers = [], $tim
134
133
/**
135
134
* Make multipart HTTP POST request.
136
135
*
137
- * @param string $url API endpoint URL
136
+ * @param string $url API endpoint URL
138
137
* @param array $data Form data
139
138
* @param array $headers HTTP headers
140
139
*
@@ -157,12 +156,12 @@ protected function makeMultipartPostRequest(string $url, array $data, array $hea
157
156
$ filepath = $ data ['_filepath ' ];
158
157
$ filename = $ data ['_filename ' ];
159
158
$ mimeType = $ this ->detectAudioMimeType ($ filepath );
160
-
159
+
161
160
$ fileResource = fopen ($ filepath , 'rb ' );
162
161
if (!$ fileResource ) {
163
162
throw new \Exception ("Cannot open file: $ filepath " );
164
163
}
165
-
164
+
166
165
$ postData .= "-- {$ boundary }\r\n" ;
167
166
$ postData .= "Content-Disposition: form-data; name= \"file \"; filename= \"{$ filename }\"\r\n" ;
168
167
$ postData .= "Content-Type: {$ mimeType }\r\n\r\n" ;
@@ -171,9 +170,8 @@ protected function makeMultipartPostRequest(string $url, array $data, array $hea
171
170
fclose ($ fileResource );
172
171
173
172
$ postData .= $ fileContent . "\r\n" ;
174
- }
175
- // To do: Currently strict format
176
- elseif ($ key === 'image ' ) {
173
+ } elseif ($ key === 'image ' ) {
174
+ // To do: Currently strict format
177
175
if (is_array ($ value )) {
178
176
foreach ($ value as $ index => $ imageData ) {
179
177
$ postData .= "-- {$ boundary }\r\n" ;
@@ -188,16 +186,14 @@ protected function makeMultipartPostRequest(string $url, array $data, array $hea
188
186
$ postData .= "Content-Type: image/png \r\n\r\n" ;
189
187
$ postData .= $ value . "\r\n" ;
190
188
}
191
- }
192
- // Handle mask file
193
- elseif ($ key === 'mask ' ) {
189
+ } elseif ($ key === 'mask ' ) {
190
+ // Handle mask file
194
191
$ postData .= "-- {$ boundary }\r\n" ;
195
192
$ postData .= "Content-Disposition: form-data; name= \"mask \"; filename= \"mask.png \"\r\n" ;
196
193
$ postData .= "Content-Type: image/png \r\n\r\n" ;
197
194
$ postData .= $ value . "\r\n" ;
198
- }
199
- // Handle regular form fields
200
- else {
195
+ } else {
196
+ // Handle regular form fields
201
197
$ postData .= "-- {$ boundary }\r\n" ;
202
198
$ postData .= "Content-Disposition: form-data; name= \"{$ key }\"\r\n\r\n" ;
203
199
$ postData .= $ value . "\r\n" ;
@@ -230,7 +226,6 @@ protected function extractFilename(string $fieldName, string $data): string
230
226
}
231
227
232
228
return "image. {$ extension }" ;
233
-
234
229
}
235
230
236
231
/**
@@ -244,22 +239,22 @@ protected function extractFilename(string $fieldName, string $data): string
244
239
protected function detectImageMimeType (string $ imageData ): string
245
240
{
246
241
$ header = substr ($ imageData , 0 , 16 );
247
-
242
+
248
243
// PNG signature
249
244
if (substr ($ header , 0 , 8 ) === "\x89PNG \r\n\x1a\n" ) {
250
245
return 'image/png ' ;
251
246
}
252
-
247
+
253
248
// JPEG signature
254
249
if (substr ($ header , 0 , 2 ) === "\xFF\xD8" ) {
255
250
return 'image/jpeg ' ;
256
251
}
257
-
252
+
258
253
// WebP signature
259
254
if (substr ($ header , 0 , 4 ) === 'RIFF ' && substr ($ header , 8 , 4 ) === 'WEBP ' ) {
260
255
return 'image/webp ' ;
261
256
}
262
-
257
+
263
258
throw new \InvalidArgumentException ('Unsupported image format. Only PNG, JPEG, and WebP are supported. ' );
264
259
}
265
260
@@ -275,7 +270,7 @@ protected function getExtensionFromMimeType(string $mimeType): string
275
270
return 'png ' ;
276
271
}
277
272
}
278
-
273
+
279
274
/**
280
275
* Get audio MIME type from file path.
281
276
*
@@ -289,7 +284,7 @@ protected function detectAudioMimeType(string $input): string
289
284
if (strpos ($ input , '. ' ) !== false && !in_array ($ input , ['mp3 ' , 'wav ' , 'flac ' , 'mp4 ' , 'mpeg ' , 'mpga ' , 'm4a ' , 'ogg ' , 'webm ' , 'opus ' , 'aac ' , 'pcm ' ])) {
290
285
$ input = strtolower (pathinfo ($ input , PATHINFO_EXTENSION ));
291
286
}
292
-
287
+
293
288
$ mimeMap = [
294
289
'mp3 ' => 'audio/mpeg ' ,
295
290
'wav ' => 'audio/wav ' ,
@@ -304,7 +299,7 @@ protected function detectAudioMimeType(string $input): string
304
299
'aac ' => 'audio/aac ' ,
305
300
'pcm ' => 'audio/pcm ' ,
306
301
];
307
-
302
+
308
303
return $ mimeMap [$ input ];
309
304
}
310
305
@@ -351,7 +346,7 @@ protected function checkModelCapability(string $model, string $capability, array
351
346
if (!isset ($ capabilityMap [$ capability ])) {
352
347
return false ;
353
348
}
354
-
349
+
355
350
return $ this ->isModelAvailable ($ model , $ capabilityMap [$ capability ]);
356
351
}
357
352
@@ -377,19 +372,20 @@ protected function validateResponse($response): bool
377
372
case 401 :
378
373
case 403 :
379
374
throw new AuthenticationException ($ this ->getName (), $ errorData , $ response ->getStatusCode ());
380
-
375
+
381
376
case 429 :
382
- if (str_contains (strtolower ($ message ), 'quota ' ) || str_contains (strtolower ($ message ), 'credits ' ) ||str_contains (strtolower ($ message ), 'billing ' )) {
377
+ if (str_contains (strtolower ($ message ), 'quota ' ) || str_contains (strtolower ($ message ), 'credits ' ) || str_contains (strtolower ($ message ), 'billing ' )) {
383
378
throw new QuotaExceededException ($ this ->getName (), $ errorData , $ response ->getStatusCode ());
384
379
} elseif (str_contains (strtolower ($ message ), 'rate ' ) || str_contains (strtolower ($ message ), 'limit ' ) || str_contains (strtolower ($ message ), 'too many requests ' )) {
385
380
throw new RateLimitException ($ this ->getName (), $ errorData , $ response ->getStatusCode ());
386
381
}
387
-
382
+ break ;
383
+
388
384
default :
389
385
throw new ProviderException ($ this ->getName (), $ errorData , $ response ->getStatusCode (), $ providerErrorCode );
390
386
}
391
387
}
392
-
388
+
393
389
return true ;
394
390
}
395
391
@@ -402,21 +398,21 @@ protected function isJsonResponse(string $responseBody): bool
402
398
403
399
/**
404
400
* Parse JSON response safely
405
- *
401
+ *
406
402
* @param string $jsonString The JSON string to parse
407
- *
403
+ *
408
404
* @return array The parsed JSON data
409
405
* @throws UnserializableResponseException If JSON parsing fails
410
406
* @since ___DEPLOY_VERSION___
411
407
*/
412
408
protected function parseJsonResponse (string $ jsonString ): array
413
409
{
414
410
$ decoded = json_decode ($ jsonString , true );
415
-
411
+
416
412
if (json_last_error () !== JSON_ERROR_NONE ) {
417
413
throw new UnserializableResponseException ($ this ->getName (), $ jsonString , json_last_error_msg (), 422 );
418
414
}
419
-
415
+
420
416
return $ decoded ;
421
417
}
422
418
0 commit comments