Chris@12
|
1 <?php
|
Chris@12
|
2 /**
|
Chris@12
|
3 * @see https://github.com/zendframework/zend-diactoros for the canonical source repository
|
Chris@12
|
4 * @copyright Copyright (c) 2015-2017 Zend Technologies USA Inc. (http://www.zend.com)
|
Chris@12
|
5 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
|
Chris@12
|
6 */
|
Chris@12
|
7
|
Chris@12
|
8 namespace Zend\Diactoros\Response;
|
Chris@12
|
9
|
Chris@12
|
10 use InvalidArgumentException;
|
Chris@12
|
11 use Psr\Http\Message\StreamInterface;
|
Chris@12
|
12 use Zend\Diactoros\Response;
|
Chris@12
|
13 use Zend\Diactoros\Stream;
|
Chris@12
|
14
|
Chris@16
|
15 use function get_class;
|
Chris@16
|
16 use function gettype;
|
Chris@16
|
17 use function is_object;
|
Chris@16
|
18 use function is_string;
|
Chris@16
|
19 use function sprintf;
|
Chris@16
|
20
|
Chris@12
|
21 /**
|
Chris@12
|
22 * XML response.
|
Chris@12
|
23 *
|
Chris@12
|
24 * Allows creating a response by passing an XML string to the constructor; by default,
|
Chris@12
|
25 * sets a status code of 200 and sets the Content-Type header to application/xml.
|
Chris@12
|
26 */
|
Chris@12
|
27 class XmlResponse extends Response
|
Chris@12
|
28 {
|
Chris@12
|
29 use InjectContentTypeTrait;
|
Chris@12
|
30
|
Chris@12
|
31 /**
|
Chris@12
|
32 * Create an XML response.
|
Chris@12
|
33 *
|
Chris@12
|
34 * Produces an XML response with a Content-Type of application/xml and a default
|
Chris@12
|
35 * status of 200.
|
Chris@12
|
36 *
|
Chris@12
|
37 * @param string|StreamInterface $xml String or stream for the message body.
|
Chris@12
|
38 * @param int $status Integer status code for the response; 200 by default.
|
Chris@12
|
39 * @param array $headers Array of headers to use at initialization.
|
Chris@12
|
40 * @throws InvalidArgumentException if $text is neither a string or stream.
|
Chris@12
|
41 */
|
Chris@12
|
42 public function __construct(
|
Chris@12
|
43 $xml,
|
Chris@12
|
44 $status = 200,
|
Chris@12
|
45 array $headers = []
|
Chris@12
|
46 ) {
|
Chris@12
|
47 parent::__construct(
|
Chris@12
|
48 $this->createBody($xml),
|
Chris@12
|
49 $status,
|
Chris@12
|
50 $this->injectContentType('application/xml; charset=utf-8', $headers)
|
Chris@12
|
51 );
|
Chris@12
|
52 }
|
Chris@12
|
53
|
Chris@12
|
54 /**
|
Chris@12
|
55 * Create the message body.
|
Chris@12
|
56 *
|
Chris@12
|
57 * @param string|StreamInterface $xml
|
Chris@12
|
58 * @return StreamInterface
|
Chris@12
|
59 * @throws InvalidArgumentException if $xml is neither a string or stream.
|
Chris@12
|
60 */
|
Chris@12
|
61 private function createBody($xml)
|
Chris@12
|
62 {
|
Chris@12
|
63 if ($xml instanceof StreamInterface) {
|
Chris@12
|
64 return $xml;
|
Chris@12
|
65 }
|
Chris@12
|
66
|
Chris@12
|
67 if (! is_string($xml)) {
|
Chris@12
|
68 throw new InvalidArgumentException(sprintf(
|
Chris@12
|
69 'Invalid content (%s) provided to %s',
|
Chris@12
|
70 (is_object($xml) ? get_class($xml) : gettype($xml)),
|
Chris@12
|
71 __CLASS__
|
Chris@12
|
72 ));
|
Chris@12
|
73 }
|
Chris@12
|
74
|
Chris@12
|
75 $body = new Stream('php://temp', 'wb+');
|
Chris@12
|
76 $body->write($xml);
|
Chris@12
|
77 $body->rewind();
|
Chris@12
|
78 return $body;
|
Chris@12
|
79 }
|
Chris@12
|
80 }
|