3 * @see https://github.com/zendframework/zend-diactoros for the canonical source repository
4 * @copyright Copyright (c) 2015-2017 Zend Technologies USA Inc. (http://www.zend.com)
5 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
8 namespace Zend\Diactoros\Response;
10 use InvalidArgumentException;
11 use Psr\Http\Message\StreamInterface;
12 use Zend\Diactoros\Response;
13 use Zend\Diactoros\Stream;
18 * Allows creating a response by passing an XML string to the constructor; by default,
19 * sets a status code of 200 and sets the Content-Type header to application/xml.
21 class XmlResponse extends Response
23 use InjectContentTypeTrait;
26 * Create an XML response.
28 * Produces an XML response with a Content-Type of application/xml and a default
31 * @param string|StreamInterface $xml String or stream for the message body.
32 * @param int $status Integer status code for the response; 200 by default.
33 * @param array $headers Array of headers to use at initialization.
34 * @throws InvalidArgumentException if $text is neither a string or stream.
36 public function __construct(
42 $this->createBody($xml),
44 $this->injectContentType('application/xml; charset=utf-8', $headers)
49 * Create the message body.
51 * @param string|StreamInterface $xml
52 * @return StreamInterface
53 * @throws InvalidArgumentException if $xml is neither a string or stream.
55 private function createBody($xml)
57 if ($xml instanceof StreamInterface) {
61 if (! is_string($xml)) {
62 throw new InvalidArgumentException(sprintf(
63 'Invalid content (%s) provided to %s',
64 (is_object($xml) ? get_class($xml) : gettype($xml)),
69 $body = new Stream('php://temp', 'wb+');