File tree 1 file changed +27
-0
lines changed
1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change 16
16
use Exception ;
17
17
use Http \Promise \Promise ;
18
18
use function is_array ;
19
+ use function is_string ;
19
20
use function json_decode ;
20
21
use const JSON_THROW_ON_ERROR ;
21
22
use JsonException ;
22
23
use Psr \Http \Message \ResponseInterface ;
23
24
use Psr \Http \Message \StreamInterface ;
25
+ use SolidWorx \SimpleHttp \Exception \InvalidArgumentException ;
24
26
use SolidWorx \SimpleHttp \Exception \NotImplementedException ;
27
+ use function stream_copy_to_stream ;
25
28
26
29
final class Response implements ResponseInterface
27
30
{
@@ -176,6 +179,30 @@ public function getReasonPhrase(): string
176
179
return $ response ->getReasonPhrase ();
177
180
}
178
181
182
+ /**
183
+ * @param string|resource $path
184
+ *
185
+ * @throws Exception
186
+ */
187
+ public function saveToFile ($ path ): void
188
+ {
189
+ $ resource = $ path ;
190
+
191
+ if (is_string ($ path )) {
192
+ $ resource = fopen ($ path , 'wb ' );
193
+ }
194
+
195
+ if (!is_resource ($ resource )) {
196
+ throw new InvalidArgumentException ('Invalid path, must be string or resource ' );
197
+ }
198
+
199
+ $ body = $ this ->getBody ()->detach ();
200
+
201
+ if (is_resource ($ body )) {
202
+ stream_copy_to_stream ($ body , $ resource );
203
+ }
204
+ }
205
+
179
206
/**
180
207
* @throws Exception
181
208
*/
You can’t perform that action at this time.
0 commit comments