File tree 4 files changed +51
-13
lines changed
4 files changed +51
-13
lines changed Original file line number Diff line number Diff line change @@ -36,8 +36,8 @@ public function getBody(): StreamInterface
36
36
return $ this ->dataStream ;
37
37
}
38
38
39
- if (( $ response = $ this ->formatResponse ()) !== null ) {
40
- $ this ->response = $ response ;
39
+ if ($ this ->hasResponseFormatter () ) {
40
+ $ this ->response = $ this -> formatResponse () ;
41
41
return $ this ->dataStream = $ this ->response ->getBody ();
42
42
}
43
43
@@ -165,17 +165,22 @@ public function getData()
165
165
return is_object ($ this ->data ) ? clone $ this ->data : $ this ->data ;
166
166
}
167
167
168
- private function formatResponse (): ? ResponseInterface
168
+ public function hasData (): bool
169
169
{
170
- if ($ this ->responseFormatter !== null ) {
171
- $ response = $ this ->responseFormatter ->format ($ this );
172
- if ($ response instanceof self) {
173
- throw new \RuntimeException ('DataResponseFormatterInterface should not return instance of DataResponse. ' );
174
- }
170
+ return $ this ->getData () !== null ;
171
+ }
175
172
176
- return $ response ;
177
- } else {
178
- return null ;
173
+ /**
174
+ * @return ResponseInterface
175
+ * @psalm-suppress PossiblyNullReference
176
+ */
177
+ private function formatResponse (): ResponseInterface
178
+ {
179
+ $ response = $ this ->responseFormatter ->format ($ this );
180
+ if ($ response instanceof self) {
181
+ throw new \RuntimeException ('DataResponseFormatterInterface should not return instance of DataResponse. ' );
179
182
}
183
+
184
+ return $ response ;
180
185
}
181
186
}
Original file line number Diff line number Diff line change 14
14
final class JsonDataResponseFormatter implements DataResponseFormatterInterface
15
15
{
16
16
use HasContentTypeTrait;
17
+
17
18
/**
18
19
* @var string the Content-Type header for the response
19
20
*/
@@ -23,8 +24,12 @@ final class JsonDataResponseFormatter implements DataResponseFormatterInterface
23
24
24
25
public function format (DataResponse $ dataResponse ): ResponseInterface
25
26
{
27
+ $ content = '' ;
26
28
$ jsonSerializer = new JsonSerializer ($ this ->options );
27
- $ content = $ jsonSerializer ->serialize ($ dataResponse ->getData ());
29
+ if ($ dataResponse ->hasData ()) {
30
+ $ content = $ jsonSerializer ->serialize ($ dataResponse ->getData ());
31
+ }
32
+
28
33
$ response = $ dataResponse ->getResponse ();
29
34
$ response ->getBody ()->write ($ content );
30
35
Original file line number Diff line number Diff line change @@ -59,7 +59,7 @@ public function testSetEmptyResponseFormatter(): void
59
59
$ dataResponse ->getBody ()->rewind ();
60
60
61
61
$ this ->assertTrue ($ dataResponse ->hasResponseFormatter ());
62
- $ this ->assertSame ('null ' , $ dataResponse ->getBody ()->getContents ());
62
+ $ this ->assertSame ('' , $ dataResponse ->getBody ()->getContents ());
63
63
$ this ->assertSame (['application/json ' ], $ dataResponse ->getHeader ('Content-Type ' ));
64
64
}
65
65
@@ -203,6 +203,24 @@ public function testGetData(): void
203
203
$ this ->assertEquals ('test2 ' , $ dataResponse ->getData ());
204
204
}
205
205
206
+ public function testHasData (): void
207
+ {
208
+ $ dataResponse = $ this ->createFactory ()->createResponse ('test ' );
209
+ $ dataResponse = $ dataResponse ->withResponseFormatter (new JsonDataResponseFormatter ());
210
+ $ dataResponse ->getBody ()->rewind ();
211
+
212
+ $ this ->assertTrue ($ dataResponse ->hasData ());
213
+ }
214
+
215
+ public function testHasDataWithEmptyData (): void
216
+ {
217
+ $ dataResponse = $ this ->createFactory ()->createResponse ();
218
+ $ dataResponse = $ dataResponse ->withResponseFormatter (new JsonDataResponseFormatter ());
219
+ $ dataResponse ->getBody ()->rewind ();
220
+
221
+ $ this ->assertFalse ($ dataResponse ->hasData ());
222
+ }
223
+
206
224
private function createFactory (): DataResponseFactory
207
225
{
208
226
return new DataResponseFactory (new Psr17Factory ());
Original file line number Diff line number Diff line change @@ -42,6 +42,16 @@ public function testWithOptions(): void
42
42
$ this ->assertSame (['application/json ' ], $ result ->getHeader (Header::CONTENT_TYPE ));
43
43
}
44
44
45
+ public function testWithEmptyResponse (): void
46
+ {
47
+ $ dataResponse = $ this ->createFactory ()->createResponse ();
48
+ $ result = (new JsonDataResponseFormatter ())->withOptions (JSON_FORCE_OBJECT )->format ($ dataResponse );
49
+ $ result ->getBody ()->rewind ();
50
+
51
+ $ this ->assertSame ('' , $ result ->getBody ()->getContents ());
52
+ $ this ->assertSame (['application/json ' ], $ result ->getHeader (Header::CONTENT_TYPE ));
53
+ }
54
+
45
55
private function createFactory (): DataResponseFactory
46
56
{
47
57
return new DataResponseFactory (new Psr17Factory ());
You can’t perform that action at this time.
0 commit comments