33namespace Farzai \Bitkub \Responses ;
44
55use Farzai \Transport \Contracts \ResponseInterface ;
6- use Farzai \Transport \Traits \PsrResponseTrait ;
76use Psr \Http \Message \RequestInterface as PsrRequestInterface ;
7+ use Psr \Http \Message \StreamInterface ;
88
99abstract class AbstractResponseDecorator implements ResponseInterface
1010{
11- use PsrResponseTrait;
12-
1311 protected ResponseInterface $ response ;
1412
1513 /**
@@ -45,11 +43,11 @@ public function headers(): array
4543 }
4644
4745 /**
48- * Check if the response is successfull .
46+ * Check if the response is successful .
4947 */
50- public function isSuccessfull (): bool
48+ public function isSuccessful (): bool
5149 {
52- return $ this ->response ->isSuccessfull ();
50+ return $ this ->response ->isSuccessful ();
5351 }
5452
5553 /**
@@ -61,15 +59,31 @@ public function json(?string $key = null): mixed
6159 }
6260
6361 /**
64- * Throw an exception if the response is not successfull.
65- *
66- * @param callable<\Farzai\Transport\Contracts\ResponseInterface> $callback Custom callback to throw an exception.
62+ * Return the json decoded response or null.
63+ */
64+ public function jsonOrNull (?string $ key = null ): mixed
65+ {
66+ return $ this ->response ->jsonOrNull ($ key );
67+ }
68+
69+ /**
70+ * Return the response as an array.
71+ */
72+ public function toArray (): array
73+ {
74+ return $ this ->response ->toArray ();
75+ }
76+
77+ /**
78+ * Throw an exception if the response is not successful.
6779 *
6880 * @throws \Psr\Http\Client\ClientExceptionInterface
6981 */
70- public function throw (?callable $ callback = null )
82+ public function throw (?callable $ callback = null ): static
7183 {
72- return $ this ->response ->throw ($ callback );
84+ $ this ->response ->throw ($ callback );
85+
86+ return $ this ;
7387 }
7488
7589 /**
@@ -79,4 +93,84 @@ public function getPsrRequest(): PsrRequestInterface
7993 {
8094 return $ this ->response ->getPsrRequest ();
8195 }
96+
97+ // PSR-7 ResponseInterface delegation
98+
99+ public function getStatusCode (): int
100+ {
101+ return $ this ->response ->getStatusCode ();
102+ }
103+
104+ public function withStatus (int $ code , string $ reasonPhrase = '' ): static
105+ {
106+ return $ this ->cloneWithResponse ($ this ->response ->withStatus ($ code , $ reasonPhrase ));
107+ }
108+
109+ public function getReasonPhrase (): string
110+ {
111+ return $ this ->response ->getReasonPhrase ();
112+ }
113+
114+ public function getProtocolVersion (): string
115+ {
116+ return $ this ->response ->getProtocolVersion ();
117+ }
118+
119+ public function withProtocolVersion (string $ version ): static
120+ {
121+ return $ this ->cloneWithResponse ($ this ->response ->withProtocolVersion ($ version ));
122+ }
123+
124+ public function getHeaders (): array
125+ {
126+ return $ this ->response ->getHeaders ();
127+ }
128+
129+ public function hasHeader (string $ name ): bool
130+ {
131+ return $ this ->response ->hasHeader ($ name );
132+ }
133+
134+ public function getHeader (string $ name ): array
135+ {
136+ return $ this ->response ->getHeader ($ name );
137+ }
138+
139+ public function getHeaderLine (string $ name ): string
140+ {
141+ return $ this ->response ->getHeaderLine ($ name );
142+ }
143+
144+ public function withHeader (string $ name , $ value ): static
145+ {
146+ return $ this ->cloneWithResponse ($ this ->response ->withHeader ($ name , $ value ));
147+ }
148+
149+ public function withAddedHeader (string $ name , $ value ): static
150+ {
151+ return $ this ->cloneWithResponse ($ this ->response ->withAddedHeader ($ name , $ value ));
152+ }
153+
154+ public function withoutHeader (string $ name ): static
155+ {
156+ return $ this ->cloneWithResponse ($ this ->response ->withoutHeader ($ name ));
157+ }
158+
159+ public function getBody (): StreamInterface
160+ {
161+ return $ this ->response ->getBody ();
162+ }
163+
164+ public function withBody (StreamInterface $ body ): static
165+ {
166+ return $ this ->cloneWithResponse ($ this ->response ->withBody ($ body ));
167+ }
168+
169+ private function cloneWithResponse (ResponseInterface $ response ): static
170+ {
171+ $ clone = clone $ this ;
172+ $ clone ->response = $ response ;
173+
174+ return $ clone ;
175+ }
82176}
0 commit comments