Mercurial > hg > isophonics-drupal-site
comparison vendor/zendframework/zend-diactoros/src/Response/TextResponse.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | c2387f117808 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
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 /** | |
18 * Plain text response. | |
19 * | |
20 * Allows creating a response by passing a string to the constructor; | |
21 * by default, sets a status code of 200 and sets the Content-Type header to | |
22 * text/plain. | |
23 */ | |
24 class TextResponse extends Response | |
25 { | |
26 use InjectContentTypeTrait; | |
27 | |
28 /** | |
29 * Create a plain text response. | |
30 * | |
31 * Produces a text response with a Content-Type of text/plain and a default | |
32 * status of 200. | |
33 * | |
34 * @param string|StreamInterface $text String or stream for the message body. | |
35 * @param int $status Integer status code for the response; 200 by default. | |
36 * @param array $headers Array of headers to use at initialization. | |
37 * @throws InvalidArgumentException if $text is neither a string or stream. | |
38 */ | |
39 public function __construct($text, $status = 200, array $headers = []) | |
40 { | |
41 parent::__construct( | |
42 $this->createBody($text), | |
43 $status, | |
44 $this->injectContentType('text/plain; charset=utf-8', $headers) | |
45 ); | |
46 } | |
47 | |
48 /** | |
49 * Create the message body. | |
50 * | |
51 * @param string|StreamInterface $text | |
52 * @return StreamInterface | |
53 * @throws InvalidArgumentException if $html is neither a string or stream. | |
54 */ | |
55 private function createBody($text) | |
56 { | |
57 if ($text instanceof StreamInterface) { | |
58 return $text; | |
59 } | |
60 | |
61 if (! is_string($text)) { | |
62 throw new InvalidArgumentException(sprintf( | |
63 'Invalid content (%s) provided to %s', | |
64 (is_object($text) ? get_class($text) : gettype($text)), | |
65 __CLASS__ | |
66 )); | |
67 } | |
68 | |
69 $body = new Stream('php://temp', 'wb+'); | |
70 $body->write($text); | |
71 $body->rewind(); | |
72 return $body; | |
73 } | |
74 } |