composer package updates
[openemr.git] / vendor / zendframework / zend-diactoros / src / Response / XmlResponse.php
blob5f81e9eeb8377cbc0c35ac5e98866b286518ee27
1 <?php
2 /**
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
6 */
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;
16 use function gettype;
17 use function is_object;
18 use function is_string;
19 use function sprintf;
21 /**
22 * XML response.
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;
31 /**
32 * Create an XML response.
34 * Produces an XML response with a Content-Type of application/xml and a default
35 * status of 200.
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(
43 $xml,
44 $status = 200,
45 array $headers = []
46 ) {
47 parent::__construct(
48 $this->createBody($xml),
49 $status,
50 $this->injectContentType('application/xml; charset=utf-8', $headers)
54 /**
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) {
64 return $xml;
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)),
71 __CLASS__
72 ));
75 $body = new Stream('php://temp', 'wb+');
76 $body->write($xml);
77 $body->rewind();
78 return $body;