1- <?php declare (strict_types= 1 );
1+ <?php
22
3- namespace Madewithlove ;
3+ declare (strict_types=1 );
4+
5+ namespace Madewithlove \HtaccessApiClient ;
46
57use Psr \Http \Client \ClientInterface ;
68use Psr \Http \Message \ServerRequestFactoryInterface ;
1820* }>,
1921 * 'output_status_code': int,
2022 * 'url':string,
21- * 'errors'?: array<int,array{'field': string, 'message': string}>
2223 * }
2324 */
2425final class HtaccessClient
@@ -37,7 +38,7 @@ public function test(
3738 string $ htaccess ,
3839 ?ServerVariables $ serverVariables = null
3940 ): HtaccessResult {
40- $ serverVariables = $ serverVariables ?? ServerVariables::default ();
41+ $ serverVariables ??= ServerVariables::default ();
4142 /** @var HtaccessResponseData */
4243 $ responseData = $ this ->request (
4344 'POST ' ,
@@ -52,16 +53,14 @@ public function test(
5253 return new HtaccessResult (
5354 $ responseData ['output_url ' ],
5455 array_map (
55- function (array $ line ): ResultLine {
56- return new ResultLine (
57- $ line ['value ' ],
58- $ line ['message ' ],
59- $ line ['isMet ' ],
60- $ line ['isValid ' ],
61- $ line ['wasReached ' ],
62- $ line ['isSupported ' ]
63- );
64- },
56+ fn (array $ line ): ResultLine => new ResultLine (
57+ $ line ['value ' ],
58+ $ line ['message ' ],
59+ $ line ['isMet ' ],
60+ $ line ['isValid ' ],
61+ $ line ['wasReached ' ],
62+ $ line ['isSupported ' ]
63+ ),
6564 $ responseData ['lines ' ]
6665 ),
6766 $ responseData ['output_status_code ' ]
@@ -76,15 +75,15 @@ public function share(
7675 string $ htaccess ,
7776 ?ServerVariables $ serverVariables = null
7877 ): ShareResult {
79- $ serverVariables = $ serverVariables ?? ServerVariables::default ();
78+ $ serverVariables ??= ServerVariables::default ();
8079 /** @var HtaccessResponseData */
8180 $ responseData = $ this ->request (
8281 'POST ' ,
8382 '/share ' ,
8483 [
8584 'url ' => $ url ,
8685 'htaccess ' => $ htaccess ,
87- 'serverVariables ' => $ serverVariables ->toArray ()
86+ 'serverVariables ' => $ serverVariables ->toArray (),
8887 ]
8988 );
9089
@@ -105,16 +104,14 @@ public function getShared(string $shareUuid): HtaccessResult
105104 return new HtaccessResult (
106105 $ responseData ['output_url ' ],
107106 array_map (
108- function (array $ line ) {
109- return new ResultLine (
110- $ line ['value ' ],
111- $ line ['message ' ],
112- $ line ['isMet ' ],
113- $ line ['isValid ' ],
114- $ line ['wasReached ' ],
115- $ line ['isSupported ' ]
116- );
117- },
107+ fn (array $ line ): ResultLine => new ResultLine (
108+ $ line ['value ' ],
109+ $ line ['message ' ],
110+ $ line ['isMet ' ],
111+ $ line ['isValid ' ],
112+ $ line ['wasReached ' ],
113+ $ line ['isSupported ' ]
114+ ),
118115 $ responseData ['lines ' ]
119116 ),
120117 $ responseData ['output_status_code ' ]
@@ -132,25 +129,32 @@ private function request(string $method, string $endpoint = '', array $requestDa
132129 'https://htaccess.madewithlove.com/api ' . $ endpoint
133130 );
134131
135- /** @var string $requestBody */
136- $ requestBody = json_encode ($ requestData );
132+ $ requestBody = json_encode ($ requestData , JSON_THROW_ON_ERROR );
137133
138134 $ body = $ request ->getBody ();
139135 $ body ->write ($ requestBody );
140136
141137 $ request = $ request
142138 ->withHeader ('Content-Type ' , 'application/json ' )
139+ ->withHeader ('Accept ' , 'application/json ' )
143140 ->withBody ($ body );
144141
145142 $ response = $ this ->httpClient ->sendRequest ($ request );
146143
147- /** @var HtaccessResponseData */
144+ /** @var array{errors?: array<string, array<string>>}| HtaccessResponseData|null */
148145 $ responseData = json_decode ($ response ->getBody ()->getContents (), true );
149146
147+ if (!is_array ($ responseData )) {
148+ throw new HtaccessException ('Unexpected response from API ' );
149+ }
150+
150151 if (isset ($ responseData ['errors ' ])) {
151- throw HtaccessException::fromApiErrors ($ responseData ['errors ' ]);
152+ /** @var array<string, array<string>> $errors */
153+ $ errors = $ responseData ['errors ' ];
154+ throw HtaccessException::fromApiErrors ($ errors );
152155 }
153156
157+ /** @var HtaccessResponseData $responseData */
154158 return $ responseData ;
155159 }
156160}
0 commit comments