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
;
15 use function get_class
;
17 use function is_object
;
18 use function is_string
;
24 * Allows creating a response by passing an XML string to the constructor; by default,
25 * sets a status code of 200 and sets the Content-Type header to application/xml.
27 class XmlResponse
extends Response
29 use InjectContentTypeTrait
;
32 * Create an XML response.
34 * Produces an XML response with a Content-Type of application/xml and a default
37 * @param string|StreamInterface $xml String or stream for the message body.
38 * @param int $status Integer status code for the response; 200 by default.
39 * @param array $headers Array of headers to use at initialization.
40 * @throws InvalidArgumentException if $text is neither a string or stream.
42 public function __construct(
48 $this->createBody($xml),
50 $this->injectContentType('application/xml; charset=utf-8', $headers)
55 * Create the message body.
57 * @param string|StreamInterface $xml
58 * @return StreamInterface
59 * @throws InvalidArgumentException if $xml is neither a string or stream.
61 private function createBody($xml)
63 if ($xml instanceof StreamInterface
) {
67 if (! is_string($xml)) {
68 throw new InvalidArgumentException(sprintf(
69 'Invalid content (%s) provided to %s',
70 (is_object($xml) ?
get_class($xml) : gettype($xml)),
75 $body = new Stream('php://temp', 'wb+');