[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @see https://github.com/laminas/laminas-diactoros for the canonical source repository 5 * @copyright https://github.com/laminas/laminas-diactoros/blob/master/COPYRIGHT.md 6 * @license https://github.com/laminas/laminas-diactoros/blob/master/LICENSE.md New BSD License 7 */ 8 9 declare(strict_types=1); 10 11 namespace Laminas\Diactoros\Response; 12 13 use Laminas\Diactoros\Exception; 14 use Laminas\Diactoros\Response; 15 use Laminas\Diactoros\Stream; 16 use Psr\Http\Message\StreamInterface; 17 18 use function get_class; 19 use function gettype; 20 use function is_object; 21 use function is_string; 22 use function sprintf; 23 24 /** 25 * Plain text response. 26 * 27 * Allows creating a response by passing a string to the constructor; 28 * by default, sets a status code of 200 and sets the Content-Type header to 29 * text/plain. 30 */ 31 class TextResponse extends Response 32 { 33 use InjectContentTypeTrait; 34 35 /** 36 * Create a plain text response. 37 * 38 * Produces a text response with a Content-Type of text/plain and a default 39 * status of 200. 40 * 41 * @param string|StreamInterface $text String or stream for the message body. 42 * @param int $status Integer status code for the response; 200 by default. 43 * @param array $headers Array of headers to use at initialization. 44 * @throws Exception\InvalidArgumentException if $text is neither a string or stream. 45 */ 46 public function __construct($text, int $status = 200, array $headers = []) 47 { 48 parent::__construct( 49 $this->createBody($text), 50 $status, 51 $this->injectContentType('text/plain; charset=utf-8', $headers) 52 ); 53 } 54 55 /** 56 * Create the message body. 57 * 58 * @param string|StreamInterface $text 59 * @throws Exception\InvalidArgumentException if $text is neither a string or stream. 60 */ 61 private function createBody($text) : StreamInterface 62 { 63 if ($text instanceof StreamInterface) { 64 return $text; 65 } 66 67 if (! is_string($text)) { 68 throw new Exception\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 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |