Overview

Namespaces

  • Kotchasan
    • Cache
    • Database
    • Http
    • Log
    • Orm
  • None
  • PHP
  • Psr
    • Cache
    • Http
      • Message
    • Log

Classes

  • ArrayIterator
  • DateInterval
  • DOMNode
  • Kotchasan
  • Kotchasan\Accordion
  • Kotchasan\ApiController
  • Kotchasan\ArrayTool
  • Kotchasan\Cache\ApcCache
  • Kotchasan\Cache\Cache
  • Kotchasan\Cache\CacheItem
  • Kotchasan\Cache\FileCache
  • Kotchasan\CKEditor
  • Kotchasan\Collection
  • Kotchasan\Config
  • Kotchasan\Controller
  • Kotchasan\Country
  • Kotchasan\Csv
  • Kotchasan\Curl
  • Kotchasan\Currency
  • Kotchasan\Database
  • Kotchasan\Database\Db
  • Kotchasan\Database\DbCache
  • Kotchasan\Database\Driver
  • Kotchasan\Database\PdoMysqlDriver
  • Kotchasan\Database\Query
  • Kotchasan\Database\QueryBuilder
  • Kotchasan\Database\Schema
  • Kotchasan\Database\Sql
  • Kotchasan\DataTable
  • Kotchasan\Date
  • Kotchasan\DOMNode
  • Kotchasan\DOMParser
  • Kotchasan\Email
  • Kotchasan\File
  • Kotchasan\Files
  • Kotchasan\Form
  • Kotchasan\Grid
  • Kotchasan\Html
  • Kotchasan\Htmldoc
  • Kotchasan\HtmlTable
  • Kotchasan\Http\AbstractMessage
  • Kotchasan\Http\AbstractRequest
  • Kotchasan\Http\Message
  • Kotchasan\Http\NotFound
  • Kotchasan\Http\Request
  • Kotchasan\Http\Response
  • Kotchasan\Http\Stream
  • Kotchasan\Http\UploadedFile
  • Kotchasan\Http\Uri
  • Kotchasan\Image
  • Kotchasan\InputItem
  • Kotchasan\Inputs
  • Kotchasan\KBase
  • Kotchasan\Language
  • Kotchasan\ListItem
  • Kotchasan\Log\AbstractLogger
  • Kotchasan\Log\Logger
  • Kotchasan\Login
  • Kotchasan\Menu
  • Kotchasan\Mime
  • Kotchasan\Model
  • Kotchasan\Number
  • Kotchasan\ObjectTool
  • Kotchasan\Orm\Field
  • Kotchasan\Orm\Recordset
  • Kotchasan\Password
  • Kotchasan\Pdf
  • Kotchasan\Province
  • Kotchasan\Router
  • Kotchasan\Session
  • Kotchasan\Singleton
  • Kotchasan\Tab
  • Kotchasan\TableRow
  • Kotchasan\Template
  • Kotchasan\Text
  • Kotchasan\Validator
  • Kotchasan\View
  • PHPMailer
  • Psr\Log\AbstractLogger
  • Psr\Log\LogLevel
  • Psr\Log\NullLogger
  • SMTP

Interfaces

  • ArrayAccess
  • Countable
  • DateTimeInterface
  • Iterator
  • IteratorAggregate
  • Psr\Cache\CacheItemInterface
  • Psr\Cache\CacheItemPoolInterface
  • Psr\Http\Message\MessageInterface
  • Psr\Http\Message\RequestInterface
  • Psr\Http\Message\ResponseInterface
  • Psr\Http\Message\ServerRequestInterface
  • Psr\Http\Message\StreamInterface
  • Psr\Http\Message\UploadedFileInterface
  • Psr\Http\Message\UriInterface
  • Psr\Log\LoggerAwareInterface
  • Psr\Log\LoggerInterface
  • SeekableIterator
  • Serializable
  • Traversable

Traits

  • Psr\Log\LoggerTrait

Exceptions

  • Exception
  • InvalidArgumentException
  • Kotchasan\ApiException
  • Kotchasan\Cache\Exception
  • Kotchasan\Database\Exception
  • Kotchasan\InputItemException
  • LogicException
  • phpmailerException
  • RuntimeException

Functions

  • createClass
  • debug
  • getClassPath
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * @filesource Kotchasan/Http/Response.php
  4:  *
  5:  * @copyright 2016 Goragod.com
  6:  * @license http://www.kotchasan.com/license/
  7:  *
  8:  * @see http://www.kotchasan.com/
  9:  */
 10: 
 11: namespace Kotchasan\Http;
 12: 
 13: use Psr\Http\Message\ResponseInterface;
 14: 
 15: /**
 16:  * Response Class.
 17:  *
 18:  * @author Goragod Wiriya <admin@goragod.com>
 19:  *
 20:  * @since 1.0
 21:  */
 22: class Response extends Message implements ResponseInterface
 23: {
 24:     /**
 25:      * Status codes.
 26:      *
 27:      * @var array
 28:      */
 29:     public static $statusTexts = array(
 30:         100 => 'Continue',
 31:         101 => 'Switching Protocols',
 32:         102 => 'Processing', // RFC2518
 33:         200 => 'OK',
 34:         201 => 'Created',
 35:         202 => 'Accepted',
 36:         203 => 'Non-Authoritative Information',
 37:         204 => 'No Content',
 38:         205 => 'Reset Content',
 39:         206 => 'Partial Content',
 40:         207 => 'Multi-Status', // RFC4918
 41:         208 => 'Already Reported', // RFC5842
 42:         226 => 'IM Used', // RFC3229
 43:         300 => 'Multiple Choices',
 44:         301 => 'Moved Permanently',
 45:         302 => 'Found',
 46:         303 => 'See Other',
 47:         304 => 'Not Modified',
 48:         305 => 'Use Proxy',
 49:         306 => 'Reserved',
 50:         307 => 'Temporary Redirect',
 51:         308 => 'Permanent Redirect', // RFC7238
 52:         400 => 'Bad Request',
 53:         401 => 'Unauthorized',
 54:         402 => 'Payment Required',
 55:         403 => 'Forbidden',
 56:         404 => 'Not Found',
 57:         405 => 'Method Not Allowed',
 58:         406 => 'Not Acceptable',
 59:         407 => 'Proxy Authentication Required',
 60:         408 => 'Request Timeout',
 61:         409 => 'Conflict',
 62:         410 => 'Gone',
 63:         411 => 'Length Required',
 64:         412 => 'Precondition Failed',
 65:         413 => 'Request Entity Too Large',
 66:         414 => 'Request-URI Too Long',
 67:         415 => 'Unsupported Media Type',
 68:         416 => 'Requested Range Not Satisfiable',
 69:         417 => 'Expectation Failed',
 70:         418 => 'I\'m a teapot', // RFC2324
 71:         422 => 'Unprocessable Entity', // RFC4918
 72:         423 => 'Locked', // RFC4918
 73:         424 => 'Failed Dependency', // RFC4918
 74:         425 => 'Reserved for WebDAV advanced collections expired proposal', // RFC2817
 75:         426 => 'Upgrade Required', // RFC2817
 76:         428 => 'Precondition Required', // RFC6585
 77:         429 => 'Too Many Requests', // RFC6585
 78:         431 => 'Request Header Fields Too Large', // RFC6585
 79:         500 => 'Internal Server Error',
 80:         501 => 'Not Implemented',
 81:         502 => 'Bad Gateway',
 82:         503 => 'Service Unavailable',
 83:         504 => 'Gateway Timeout',
 84:         505 => 'HTTP Version Not Supported',
 85:         506 => 'Variant Also Negotiates (Experimental)', // RFC2295
 86:         507 => 'Insufficient Storage', // RFC4918
 87:         508 => 'Loop Detected', // RFC5842
 88:         510 => 'Not Extended', // RFC2774
 89:         511 => 'Network Authentication Required', // RFC6585
 90:     );
 91:     /**
 92:      * เนื้อหา.
 93:      *
 94:      * @var string
 95:      */
 96:     protected $content;
 97:     /**
 98:      * @var string
 99:      */
100:     protected $reasonPhrase;
101:     /**
102:      * @var int
103:      */
104:     protected $statusCode;
105: 
106:     /**
107:      * create Response.
108:      *
109:      * @param int          $code         status Code
110:      * @param string||null $reasonPhrase ถ้าไม่กำหนดจะใช้ข้อความจากระบบ
111:      */
112:     public function __construct($code = 200, $reasonPhrase = null)
113:     {
114:         $this->statusCode = $code;
115:         $this->reasonPhrase = $reasonPhrase;
116:     }
117: 
118:     /**
119:      * คืนค่าเนื้อหาของ Response.
120:      *
121:      * @return string
122:      */
123:     public function getContent()
124:     {
125:         return $this->content;
126:     }
127: 
128:     /**
129:      * Gets the response reason phrase associated with the status code.
130:      *
131:      * @see http://tools.ietf.org/html/rfc7231#section-6
132:      * @see http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
133:      *
134:      * @return string
135:      */
136:     public function getReasonPhrase()
137:     {
138:         if ($this->reasonPhrase) {
139:             return $this->reasonPhrase;
140:         }
141:         if (isset(static::$statusTexts[$this->statusCode])) {
142:             return static::$statusTexts[$this->statusCode];
143:         }
144: 
145:         return '';
146:     }
147: 
148:     /**
149:      * คืนค่า Response Status.
150:      *
151:      * @return int
152:      */
153:     public function getStatusCode()
154:     {
155:         return $this->statusCode;
156:     }
157: 
158:     /**
159:      * Sends HTTP headers and content.
160:      *
161:      * @return \static
162:      */
163:     public function send()
164:     {
165:         $this->sendHeaders();
166:         $this->sendContent();
167: 
168:         return $this;
169:     }
170: 
171:     /**
172:      * กำหนดเนื้อหาให้กับ  Response.
173:      *
174:      * @param mixed $content
175:      *
176:      * @throws \InvalidArgumentException ถ้า $content ไม่ใช่ string
177:      *
178:      * @return \static
179:      */
180:     public function withContent($content)
181:     {
182:         if (null !== $content && !is_string($content) && !is_numeric($content) && !is_callable(array($content, '__toString'))) {
183:             throw new \InvalidArgumentException(sprintf('The Response content must be a string or object implementing __toString(), "%s" given.', gettype($content)));
184:         }
185:         $this->content = (string) $content;
186: 
187:         return $this;
188:     }
189: 
190:     /**
191:      * กำหนดค่า status code.
192:      *
193:      * @param int    $code
194:      * @param string $reasonPhrase
195:      *
196:      * @return \static
197:      */
198:     public function withStatus($code, $reasonPhrase = '')
199:     {
200:         $clone = clone $this;
201:         $clone->statusCode = $code;
202:         $clone->reasonPhrase = $reasonPhrase;
203: 
204:         return $clone;
205:     }
206: 
207:     /**
208:      * ส่งออกเนื้อหา.
209:      *
210:      * @return \static
211:      */
212:     protected function sendContent()
213:     {
214:         if ($this->content) {
215:             echo $this->content;
216:         }
217: 
218:         return $this;
219:     }
220: 
221:     /**
222:      * ส่งออก HTTP headers.
223:      *
224:      * @return \static
225:      */
226:     protected function sendHeaders()
227:     {
228:         if (headers_sent()) {
229:             return $this;
230:         }
231:         header(sprintf('HTTP/%s %s %s', $this->protocol, $this->statusCode, $this->getReasonPhrase()), true, $this->statusCode);
232:         foreach ($this->headers as $name => $values) {
233:             foreach ($values as $value) {
234:                 header($name.': '.$value);
235:             }
236:         }
237: 
238:         return $this;
239:     }
240: }
241: 
Kotchasan API documentation generated by ApiGen