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