@@ -25,26 +25,26 @@ class Response implements ResponseInterface
2525 private string $ reason ;
2626
2727 /**
28- * @param int $statusCode Normally one of the status codes defined by RFC 7231 section 6
29- * @param StreamInterface $body
30- * @param array $headers Associative array of header strings or arrays of header strings
31- * @param array $params Associative array with following keys and its default values
32- * when key is not present or its value is null:
33- * - version - http protocol version (default: '1.1')
34- * - reason - reason phrase normally associated with $statusCode, so by
35- * default it will be resolved from it.
28+ * @param int $statusCode Normally one of the status codes defined by RFC 7231 section 6
29+ * @param ? StreamInterface $body
30+ * @param array $headers Associative array of header strings or arrays of header strings
31+ * @param array $params Associative array with following keys and its default values
32+ * when key is not present or its value is null:
33+ * - version - http protocol version (default: '1.1')
34+ * - reason - reason phrase normally associated with $statusCode, so by
35+ * default it will be resolved from it.
3636 *
3737 * @see https://tools.ietf.org/html/rfc7231#section-6
3838 * @see StatusCodesTrait
3939 */
4040 public function __construct (
4141 int $ statusCode ,
42- StreamInterface $ body ,
42+ ? StreamInterface $ body = null ,
4343 array $ headers = [],
4444 array $ params = []
4545 ) {
4646 $ this ->status = $ this ->validStatusCode ($ statusCode );
47- $ this ->body = $ body ;
47+ $ this ->body = $ body ?? Stream:: fromBodyString ( '' ) ;
4848 $ this ->reason = $ this ->validReasonPhrase ($ params ['reason ' ] ?? '' );
4949 $ this ->version = isset ($ params ['version ' ]) ? $ this ->validProtocolVersion ($ params ['version ' ]) : '1.1 ' ;
5050 $ this ->loadHeaders ($ headers );
@@ -66,8 +66,8 @@ public static function xml(string $xml, int $statusCode = 200): self
6666 }
6767
6868 /**
69- * There's a XOR operator between $defaultEncode and $encodeOptions,
70- * which means that if option is set in both provided and default it
69+ * There's a XOR operator between $defaultOptions and $encodeOptions,
70+ * which means that if an option is set in both provided and default it
7171 * will be switched off.
7272 *
7373 * @param array $data
@@ -76,11 +76,10 @@ public static function xml(string $xml, int $statusCode = 200): self
7676 *
7777 * @return self
7878 */
79- public static function json (array $ data , int $ statusCode = 200 , $ encodeOptions = 0 ): self
79+ public static function json (array $ data , int $ statusCode = 200 , int $ encodeOptions = 0 ): self
8080 {
81- $ defaultEncode = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT |
82- JSON_UNESCAPED_SLASHES | JSON_FORCE_OBJECT ;
83- $ serialized = json_encode ($ data , $ defaultEncode ^ $ encodeOptions );
81+ $ defaultOptions = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_SLASHES ;
82+ $ serialized = $ data ? json_encode ($ data , $ defaultOptions ^ $ encodeOptions ) : '{} ' ;
8483
8584 return new self ($ statusCode , Stream::fromBodyString ($ serialized ), ['Content-Type ' => 'application/json ' ]);
8685 }
@@ -90,12 +89,22 @@ public static function redirect($uri, int $status = 303): self
9089 if ($ status < 300 || $ status > 399 ) {
9190 throw new InvalidArgumentException ('Invalid status code for redirect response ' );
9291 }
93- return new self ($ status , new Stream (fopen ('php://temp ' , 'r ' )), ['Location ' => (string ) $ uri ]);
92+ return new self ($ status , null , ['Location ' => (string ) $ uri ]);
93+ }
94+
95+ public static function badRequest (StreamInterface $ body = null ): self
96+ {
97+ return new self (400 , $ body );
98+ }
99+
100+ public static function unauthorized (StreamInterface $ body = null ): self
101+ {
102+ return new self (401 , $ body );
94103 }
95104
96105 public static function notFound (StreamInterface $ body = null ): self
97106 {
98- return new self (404 , $ body ?: Stream:: fromResourceUri ( ' php://temp ' ) );
107+ return new self (404 , $ body );
99108 }
100109
101110 public function getStatusCode (): int
0 commit comments