/** * Create a JSON response with the given data. * * Default JSON encoding is performed with the following options, which * produces RFC4627-compliant JSON, capable of embedding into HTML. * * - JSON_HEX_TAG * - JSON_HEX_APOS * - JSON_HEX_AMP * - JSON_HEX_QUOT * - JSON_UNESCAPED_SLASHES * * @param mixed $data Data to convert to JSON. * @param int $status Integer status code for the response; 200 by default. * @param array $headers Array of headers to use at initialization. * @param int $encodingOptions JSON encoding options to use. * * @throws InvalidArgumentException if unable to encode the $data to JSON. */ public function __construct($data, int $status = 200, array $headers = [], $encodingOptions = self::DEFAULT_JSON_FLAGS) { $body = new Stream(fopen('php://temp', 'wb+')); $body->write($this->jsonEncode($data, $encodingOptions)); $body->rewind(); $headers = $this->injectContentType('application/json', $headers); parent::__construct($status, $headers, $body); }
/** * Create a redirect response. * * Produces a redirect response with a Location header and the given status * (302 by default). * * Note: this method overwrites the `location` $headers value. * * @param string|UriInterface $uri URI for the Location header. * @param int $status Integer status code for the redirect; 302 by default. * @param array $headers Array of headers to use at initialization. */ public function __construct($uri, int $status = 302, array $headers = []) { if (!is_string($uri) && !$uri instanceof UriInterface) { throw new InvalidArgumentException(sprintf('Uri provided to %s MUST be a string or Psr\\Http\\Message\\UriInterface instance; received "%s"', __CLASS__, is_object($uri) ? get_class($uri) : gettype($uri))); } $headers['location'] = [(string) $uri]; parent::__construct($status, $headers, new Stream(fopen('php://temp', 'r+'))); }
public function testWithoutHeaderThatDoesNotExist() { $response = new Response(200, ['Baz' => 'Bam']); $response2 = $response->withoutHeader('foO'); $this->assertSame($response, $response2); $this->assertFalse($response2->hasHeader('foo')); $this->assertSame(['Baz' => ['Bam']], $response2->getHeaders()); }
/** * Create an HTML response. * * Produces an HTML response with a Content-Type of text/html and a default * status of 200. * * @param string|StreamInterface $html HTML or stream for the message body. * @param int $status Integer status code for the response; 200 by default. * @param array $headers Array of headers to use at initialization. * * @throws InvalidArgumentException if $html is neither a string or stream. */ public function __construct($html, int $status = 200, array $headers = []) { parent::__construct($status, $this->injectContentType('text/html; charset=utf-8', $headers), $this->createBody($html)); }
/** * Create an empty response with the given status code. * * @param array $headers Headers for the response, if any. * @param int $status Status code for the response, if any. */ public function __construct(array $headers = [], int $status = 204) { parent::__construct($status, $headers, new Stream(fopen('php://temp', 'r'))); }