public __construct ( string | resource | Psr\Http\Message\StreamInterface $body = 'php://memory', integer $status = 200, array $headers = [] ) | ||
$body | string | resource | Psr\Http\Message\StreamInterface | Stream identifier and/or actual stream resource |
$status | integer | Status code for the response, if any. |
$headers | array | Headers for the response, if any. |
/** * 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, $status = 200, array $headers = [], $encodingOptions = self::DEFAULT_JSON_FLAGS) { $body = new Stream('php://temp', 'wb+'); $body->write($this->jsonEncode($data, $encodingOptions)); $headers = $this->injectContentType('application/json', $headers); parent::__construct($body, $status, $headers); }
/** * 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, $status = 302, array $headers = array()) { 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'] = array((string) $uri); parent::__construct('php://temp', $status, $headers); }
/** * PsrApiResponse constructor. * * @param null $data * @param array $apiMessages * @param int $status * @param array $headers * @param int $encodingOptions */ public function __construct($data = null, $apiMessages = [], $status = 200, array $headers = [], $encodingOptions = 0) { $this->messages = new ApiMessages(); $this->setData($data); $this->addApiMessages($apiMessages); $this->encodingOptions = $encodingOptions; $headers = $this->injectContentType('application/json', $headers); parent::__construct($this->getBody(), $status, $headers); }
/** * Create a JSONP response with the given data. * * @param mixed $data Data to convert to JSON. * @param string $callback The JSONP callback function. * @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 $data pr $callback invalid. */ public function __construct($data, $callback, $status = 200, array $headers = [], $encodingOptions = JsonResponse::DEFAULT_JSON_FLAGS) { $this->validateCallback($callback); $body = new Stream('php://temp', 'wb+'); $body->write($callback); $body->write('('); $body->write($this->jsonEncode($data, $encodingOptions)); $body->write(');'); $body->rewind(); $headers = $this->injectContentType('application/javascript', $headers); parent::__construct($body, $status, $headers); }
/** * @param string|resource|Psr\Http\Message\StreamInterface $body * @param integer $status * @param array $headers */ public function __construct($body = 'php://memory', $status = 200, array $headers = []) { $headers = array_merge(['Access-Control-Allow-Origin' => '*', 'Connection' => 'close', 'Content-Type' => 'text/plain'], $headers); parent::__construct($body, $status, $headers); }
/** * @param ResponseBody $responseBody * @param string|resource|StreamInterface $body Stream identifier and/or actual stream resource */ public function __construct(ResponseBody $responseBody = null, $body = 'php://memory') { parent::__construct($body, 200, ['content-type' => 'application/json']); $this->responseBody = $responseBody ?: new ResponseBody(); }
public function __construct(QrCode $qrCode, $status = 200, array $headers = []) { parent::__construct($this->createBody($qrCode), $status, $this->injectContentType($qrCode->getContentType(), $headers)); }
/** * @param string $content * @param int $status * @param array $headers */ public function __construct($content, $status = 200, array $headers = []) { $headers = $this->injectContentType(MediaTypeInterface::JSON_API_MEDIA_TYPE, $headers); parent::__construct($this->createBody($content), $status, $headers); }
/** * @param string|resource|\Psr\Http\Message\StreamInterface $body The response body. * @param integer $status The response status code. * @param array $headers The response headers. * @param array $parameters The response parameters. */ public function __construct($body = 'php://memory', $status = 200, array $headers = array(), array $parameters = array()) { parent::__construct($body, $status, $headers); $this->parameters = $parameters; }
/** * Create a console response with the given data. * * @param mixed $data Data to convert to string * @param int $status Integer status code for the response; 200 by default. * @throws InvalidArgumentException if unable to encode the $data to JSON. */ public function __construct($data, $status = 200) { $body = new Stream('php://temp', 'wb+'); $body->write($this->encode($data)); parent::__construct($body, $status); }
/** * Build response from HAL resource * * @param Resource $resource */ public function __construct(Resource $resource) { parent::__construct('php://memory', 200, ['Content-Type' => 'application/hal+json']); $this->resource = $resource; }
/** * 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, $status = 200, array $headers = array()) { parent::__construct($this->createBody($html), $status, $this->injectContentType('text/html', $headers)); }
/** * @param RendererInterface $renderer */ public function __construct(RendererInterface $renderer) { parent::__construct(); $this->renderer = $renderer; }
/** * Create an empty response with the given status code. * * @param int $status Status code for the response, if any. * @param array $headers Headers for the response, if any. */ public function __construct($status = 204, array $headers = []) { $body = new Stream('php://temp', 'r'); parent::__construct($body, $status, $headers); }
/** * Create a plain text response. * * Produces a text response with a Content-Type of text/plain and a default * status of 200. */ public function __construct(string $text, int $status = 200, array $headers = []) { $body = new Stream('php://temp', 'wb+'); $body->write($text); parent::__construct($body, $status, $this->injectContentType('text/plain; charset=utf-8', $headers)); }
/** * Create an empty response with the given status code and attached resource. * * @param ResourceDOInterface $resource * @param int $status Status code for the response, if any. * @param array $headers Headers for the response, if any. */ public function __construct(ResourceDOInterface $resource, $status = 204, array $headers = []) { $this->setContent($resource); $body = $this->createBody(); parent::__construct($body, $status, $headers); }
/** * Create an empty response with the given status code. * * @param string|resource|Stream $content * @param int $status Status code for the response, if any. * @param array $headers Headers for the response, if any. */ public function __construct($content = null, $status = 204, array $headers = []) { $body = $this->createBody($content); parent::__construct($body, $status, $headers); }
/** * Create a plain text response. * * Produces a text response with a Content-Type of text/plain and a default * status of 200. * * @param string|StreamInterface $text String 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 $text is neither a string or stream. */ public function __construct($text, $status = 200, array $headers = []) { parent::__construct($this->createBody($text), $status, $this->injectContentType('text/plain; charset=utf-8', $headers)); }
/** * Create an empty response with the given status code and attached uploaded file information. * * @param UploadedFileInterface $uploadedFile * @param int $status Status code for the response, if any. * @param array $headers Headers for the response, if any. */ public function __construct(UploadedFileInterface $uploadedFile, $status = 204, array $headers = []) { $this->setContent($uploadedFile); $body = $this->createBody(); parent::__construct($body, $status, $headers); }