annotate vendor/zendframework/zend-diactoros/src/Response/XmlResponse.php @ 12:7a779792577d

Update Drupal core to v8.4.5 (via Composer)
author Chris Cannam
date Fri, 23 Feb 2018 15:52:07 +0000
parents
children c2387f117808
rev   line source
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@12 15 /**
Chris@12 16 * XML response.
Chris@12 17 *
Chris@12 18 * Allows creating a response by passing an XML string to the constructor; by default,
Chris@12 19 * sets a status code of 200 and sets the Content-Type header to application/xml.
Chris@12 20 */
Chris@12 21 class XmlResponse extends Response
Chris@12 22 {
Chris@12 23 use InjectContentTypeTrait;
Chris@12 24
Chris@12 25 /**
Chris@12 26 * Create an XML response.
Chris@12 27 *
Chris@12 28 * Produces an XML response with a Content-Type of application/xml and a default
Chris@12 29 * status of 200.
Chris@12 30 *
Chris@12 31 * @param string|StreamInterface $xml String or stream for the message body.
Chris@12 32 * @param int $status Integer status code for the response; 200 by default.
Chris@12 33 * @param array $headers Array of headers to use at initialization.
Chris@12 34 * @throws InvalidArgumentException if $text is neither a string or stream.
Chris@12 35 */
Chris@12 36 public function __construct(
Chris@12 37 $xml,
Chris@12 38 $status = 200,
Chris@12 39 array $headers = []
Chris@12 40 ) {
Chris@12 41 parent::__construct(
Chris@12 42 $this->createBody($xml),
Chris@12 43 $status,
Chris@12 44 $this->injectContentType('application/xml; charset=utf-8', $headers)
Chris@12 45 );
Chris@12 46 }
Chris@12 47
Chris@12 48 /**
Chris@12 49 * Create the message body.
Chris@12 50 *
Chris@12 51 * @param string|StreamInterface $xml
Chris@12 52 * @return StreamInterface
Chris@12 53 * @throws InvalidArgumentException if $xml is neither a string or stream.
Chris@12 54 */
Chris@12 55 private function createBody($xml)
Chris@12 56 {
Chris@12 57 if ($xml instanceof StreamInterface) {
Chris@12 58 return $xml;
Chris@12 59 }
Chris@12 60
Chris@12 61 if (! is_string($xml)) {
Chris@12 62 throw new InvalidArgumentException(sprintf(
Chris@12 63 'Invalid content (%s) provided to %s',
Chris@12 64 (is_object($xml) ? get_class($xml) : gettype($xml)),
Chris@12 65 __CLASS__
Chris@12 66 ));
Chris@12 67 }
Chris@12 68
Chris@12 69 $body = new Stream('php://temp', 'wb+');
Chris@12 70 $body->write($xml);
Chris@12 71 $body->rewind();
Chris@12 72 return $body;
Chris@12 73 }
Chris@12 74 }