@@ -46,7 +46,7 @@ abstract class AbstractProvider implements ProviderInterface
4646 *
4747 * @param array|\ArrayAccess $options Provider options array.
4848 * @param HttpFactory $httpFactory The http factory
49- *
49+ *
5050 * @throws \InvalidArgumentException
5151 * @since ___DEPLOY_VERSION___
5252 */
@@ -65,10 +65,10 @@ public function __construct(array $options = [], ?HttpFactory $httpFactory = nul
6565
6666 /**
6767 * Get an option from the AI provider.
68- *
68+ *
6969 * @param string $key The name of the option to get.
7070 * @param mixed $default The default value if the option is not set.
71- *
71+ *
7272 * @return mixed The option value.
7373 * @since ___DEPLOY_VERSION___
7474 */
@@ -92,9 +92,9 @@ protected function makeGetRequest(string $url, array $headers = [], $timeout = n
9292 {
9393 try {
9494 $ response = $ this ->httpFactory ->getHttp ([])->get ($ url , $ headers , $ timeout );
95-
95+
9696 $ this ->validateResponse ($ response );
97- } catch (AuthenticationException | RateLimitException | QuotaExceededException $ e ) {
97+ } catch (AuthenticationException | RateLimitException | QuotaExceededException $ e ) {
9898 throw $ e ;
9999 } catch (ProviderException $ e ) {
100100 throw $ e ;
@@ -106,7 +106,7 @@ protected function makeGetRequest(string $url, array $headers = [], $timeout = n
106106 /**
107107 * Make HTTP POST request.
108108 *
109- * @param string $url API endpoint URL
109+ * @param string $url API endpoint URL
110110 * @param mixed $data POST data
111111 * @param array $headers HTTP headers
112112 * @param integer $timeout Request timeout
@@ -119,10 +119,9 @@ protected function makePostRequest(string $url, $data, array $headers = [], $tim
119119 {
120120 try {
121121 $ response = $ this ->httpFactory ->getHttp ([])->post ($ url , $ data , $ headers , $ timeout );
122-
122+
123123 $ this ->validateResponse ($ response );
124-
125- } catch (AuthenticationException |RateLimitException |QuotaExceededException $ e ) {
124+ } catch (AuthenticationException | RateLimitException | QuotaExceededException $ e ) {
126125 throw $ e ;
127126 } catch (ProviderException $ e ) {
128127 throw $ e ;
@@ -134,7 +133,7 @@ protected function makePostRequest(string $url, $data, array $headers = [], $tim
134133 /**
135134 * Make multipart HTTP POST request.
136135 *
137- * @param string $url API endpoint URL
136+ * @param string $url API endpoint URL
138137 * @param array $data Form data
139138 * @param array $headers HTTP headers
140139 *
@@ -157,12 +156,12 @@ protected function makeMultipartPostRequest(string $url, array $data, array $hea
157156 $ filepath = $ data ['_filepath ' ];
158157 $ filename = $ data ['_filename ' ];
159158 $ mimeType = $ this ->detectAudioMimeType ($ filepath );
160-
159+
161160 $ fileResource = fopen ($ filepath , 'rb ' );
162161 if (!$ fileResource ) {
163162 throw new \Exception ("Cannot open file: $ filepath " );
164163 }
165-
164+
166165 $ postData .= "-- {$ boundary }\r\n" ;
167166 $ postData .= "Content-Disposition: form-data; name= \"file \"; filename= \"{$ filename }\"\r\n" ;
168167 $ postData .= "Content-Type: {$ mimeType }\r\n\r\n" ;
@@ -171,9 +170,8 @@ protected function makeMultipartPostRequest(string $url, array $data, array $hea
171170 fclose ($ fileResource );
172171
173172 $ 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
177175 if (is_array ($ value )) {
178176 foreach ($ value as $ index => $ imageData ) {
179177 $ postData .= "-- {$ boundary }\r\n" ;
@@ -188,16 +186,14 @@ protected function makeMultipartPostRequest(string $url, array $data, array $hea
188186 $ postData .= "Content-Type: image/png \r\n\r\n" ;
189187 $ postData .= $ value . "\r\n" ;
190188 }
191- }
192- // Handle mask file
193- elseif ($ key === 'mask ' ) {
189+ } elseif ($ key === 'mask ' ) {
190+ // Handle mask file
194191 $ postData .= "-- {$ boundary }\r\n" ;
195192 $ postData .= "Content-Disposition: form-data; name= \"mask \"; filename= \"mask.png \"\r\n" ;
196193 $ postData .= "Content-Type: image/png \r\n\r\n" ;
197194 $ postData .= $ value . "\r\n" ;
198- }
199- // Handle regular form fields
200- else {
195+ } else {
196+ // Handle regular form fields
201197 $ postData .= "-- {$ boundary }\r\n" ;
202198 $ postData .= "Content-Disposition: form-data; name= \"{$ key }\"\r\n\r\n" ;
203199 $ postData .= $ value . "\r\n" ;
@@ -230,7 +226,6 @@ protected function extractFilename(string $fieldName, string $data): string
230226 }
231227
232228 return "image. {$ extension }" ;
233-
234229 }
235230
236231 /**
@@ -244,22 +239,22 @@ protected function extractFilename(string $fieldName, string $data): string
244239 protected function detectImageMimeType (string $ imageData ): string
245240 {
246241 $ header = substr ($ imageData , 0 , 16 );
247-
242+
248243 // PNG signature
249244 if (substr ($ header , 0 , 8 ) === "\x89PNG \r\n\x1a\n" ) {
250245 return 'image/png ' ;
251246 }
252-
247+
253248 // JPEG signature
254249 if (substr ($ header , 0 , 2 ) === "\xFF\xD8" ) {
255250 return 'image/jpeg ' ;
256251 }
257-
252+
258253 // WebP signature
259254 if (substr ($ header , 0 , 4 ) === 'RIFF ' && substr ($ header , 8 , 4 ) === 'WEBP ' ) {
260255 return 'image/webp ' ;
261256 }
262-
257+
263258 throw new \InvalidArgumentException ('Unsupported image format. Only PNG, JPEG, and WebP are supported. ' );
264259 }
265260
@@ -275,7 +270,7 @@ protected function getExtensionFromMimeType(string $mimeType): string
275270 return 'png ' ;
276271 }
277272 }
278-
273+
279274 /**
280275 * Get audio MIME type from file path.
281276 *
@@ -289,7 +284,7 @@ protected function detectAudioMimeType(string $input): string
289284 if (strpos ($ input , '. ' ) !== false && !in_array ($ input , ['mp3 ' , 'wav ' , 'flac ' , 'mp4 ' , 'mpeg ' , 'mpga ' , 'm4a ' , 'ogg ' , 'webm ' , 'opus ' , 'aac ' , 'pcm ' ])) {
290285 $ input = strtolower (pathinfo ($ input , PATHINFO_EXTENSION ));
291286 }
292-
287+
293288 $ mimeMap = [
294289 'mp3 ' => 'audio/mpeg ' ,
295290 'wav ' => 'audio/wav ' ,
@@ -304,7 +299,7 @@ protected function detectAudioMimeType(string $input): string
304299 'aac ' => 'audio/aac ' ,
305300 'pcm ' => 'audio/pcm ' ,
306301 ];
307-
302+
308303 return $ mimeMap [$ input ];
309304 }
310305
@@ -351,7 +346,7 @@ protected function checkModelCapability(string $model, string $capability, array
351346 if (!isset ($ capabilityMap [$ capability ])) {
352347 return false ;
353348 }
354-
349+
355350 return $ this ->isModelAvailable ($ model , $ capabilityMap [$ capability ]);
356351 }
357352
@@ -377,19 +372,20 @@ protected function validateResponse($response): bool
377372 case 401 :
378373 case 403 :
379374 throw new AuthenticationException ($ this ->getName (), $ errorData , $ response ->getStatusCode ());
380-
375+
381376 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 ' )) {
383378 throw new QuotaExceededException ($ this ->getName (), $ errorData , $ response ->getStatusCode ());
384379 } elseif (str_contains (strtolower ($ message ), 'rate ' ) || str_contains (strtolower ($ message ), 'limit ' ) || str_contains (strtolower ($ message ), 'too many requests ' )) {
385380 throw new RateLimitException ($ this ->getName (), $ errorData , $ response ->getStatusCode ());
386381 }
387-
382+ break ;
383+
388384 default :
389385 throw new ProviderException ($ this ->getName (), $ errorData , $ response ->getStatusCode (), $ providerErrorCode );
390386 }
391387 }
392-
388+
393389 return true ;
394390 }
395391
@@ -402,21 +398,21 @@ protected function isJsonResponse(string $responseBody): bool
402398
403399 /**
404400 * Parse JSON response safely
405- *
401+ *
406402 * @param string $jsonString The JSON string to parse
407- *
403+ *
408404 * @return array The parsed JSON data
409405 * @throws UnserializableResponseException If JSON parsing fails
410406 * @since ___DEPLOY_VERSION___
411407 */
412408 protected function parseJsonResponse (string $ jsonString ): array
413409 {
414410 $ decoded = json_decode ($ jsonString , true );
415-
411+
416412 if (json_last_error () !== JSON_ERROR_NONE ) {
417413 throw new UnserializableResponseException ($ this ->getName (), $ jsonString , json_last_error_msg (), 422 );
418414 }
419-
415+
420416 return $ decoded ;
421417 }
422418
0 commit comments