Security update for Core, with self-updated composer
[yaffs-website] / vendor / zendframework / zend-diactoros / src / Response / XmlResponse.php
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  */
7
8 namespace Zend\Diactoros\Response;
9
10 use InvalidArgumentException;
11 use Psr\Http\Message\StreamInterface;
12 use Zend\Diactoros\Response;
13 use Zend\Diactoros\Stream;
14
15 /**
16  * XML response.
17  *
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.
20  */
21 class XmlResponse extends Response
22 {
23     use InjectContentTypeTrait;
24
25     /**
26      * Create an XML response.
27      *
28      * Produces an XML response with a Content-Type of application/xml and a default
29      * status of 200.
30      *
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.
35      */
36     public function __construct(
37         $xml,
38         $status = 200,
39         array $headers = []
40     ) {
41         parent::__construct(
42             $this->createBody($xml),
43             $status,
44             $this->injectContentType('application/xml; charset=utf-8', $headers)
45         );
46     }
47
48     /**
49      * Create the message body.
50      *
51      * @param string|StreamInterface $xml
52      * @return StreamInterface
53      * @throws InvalidArgumentException if $xml is neither a string or stream.
54      */
55     private function createBody($xml)
56     {
57         if ($xml instanceof StreamInterface) {
58             return $xml;
59         }
60
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)),
65                 __CLASS__
66             ));
67         }
68
69         $body = new Stream('php://temp', 'wb+');
70         $body->write($xml);
71         $body->rewind();
72         return $body;
73     }
74 }