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/AbstractMessage.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\MessageInterface;
 14: use Psr\Http\Message\StreamInterface;
 15: 
 16: /**
 17:  * HTTP messages base class (PSR-7)
 18:  *
 19:  * @author Goragod Wiriya <admin@goragod.com>
 20:  *
 21:  * @since 1.0
 22:  */
 23: abstract class AbstractMessage implements MessageInterface
 24: {
 25:     /**
 26:      * @var array
 27:      */
 28:     protected $headers = array();
 29:     /**
 30:      * @var string
 31:      */
 32:     protected $protocol = '1.1';
 33:     /**
 34:      * @var StreamInterface
 35:      */
 36:     protected $stream;
 37: 
 38:     /**
 39:      * init Class
 40:      *
 41:      * @param bool $with_header true คืนค่า HTTP Header ด้วย, false (default) ไม่รวม HTTP Header
 42:      */
 43:     public function __construct($with_header = false)
 44:     {
 45:         if ($with_header) {
 46:             $this->headers = $this->getRequestHeaders();
 47:         }
 48:     }
 49: 
 50:     /**
 51:      * อ่าน stream.
 52:      *
 53:      * @return StreamInterface
 54:      */
 55:     public function getBody()
 56:     {
 57:         return $this->stream;
 58:     }
 59: 
 60:     /**
 61:      * อ่าน header ที่ต้องการ ผลลัพท์เป็น array
 62:      * คืนค่าแอเรย์ของ header ถ้าไม่พบคืนค่าแอเรย์ว่าง
 63:      *
 64:      * @param string $name
 65:      *
 66:      * @return string[]
 67:      */
 68:     public function getHeader($name)
 69:     {
 70:         return isset($this->headers[$name]) ? $this->headers[$name] : array();
 71:     }
 72: 
 73:     /**
 74:      * อ่าน header ที่ต้องการ ผลลัพท์เป็น string
 75:      * คืนค่ารายการ header ทั้งหมดที่พบเชื่อมต่อด้วย ลูกน้ำ (,) หรือคืนค่าข้อความว่าง หากไม่พบ
 76:      *
 77:      * @param string $name
 78:      *
 79:      * @return string
 80:      */
 81:     public function getHeaderLine($name)
 82:     {
 83:         $values = $this->getHeader($name);
 84:         return empty($values) ? '' : implode(',', $values);
 85:     }
 86: 
 87:     /**
 88:      * คืนค่า header ทั้งหมด ผลลัพท์เป็น array.
 89:      *
 90:      * @return array
 91:      */
 92:     public function getHeaders()
 93:     {
 94:         return $this->headers;
 95:     }
 96: 
 97:     /**
 98:      * คืนค่าเวอร์ชั่นของโปรโตคอล
 99:      * เช่น 1.1, 1.0
100:      *
101:      * @return string
102:      */
103:     public function getProtocolVersion()
104:     {
105:         return $this->protocol;
106:     }
107: 
108:     /**
109:      * ตรวจสอบว่ามี header หรือไม่
110:      * คืนค่า true ถ้ามี.
111:      *
112:      * @param string $name
113:      *
114:      * @return bool
115:      */
116:     public function hasHeader($name)
117:     {
118:         return isset($this->headers[$name]);
119:     }
120: 
121:     /**
122:      * เพิ่ม header ใหม่
123:      *
124:      * @param string          $name  ชื่อของ Header
125:      * @param string|string[] $value ค่าของ Header เป็น string หรือ แอเรย์ของ string
126:      *
127:      * @throws \InvalidArgumentException ถ้าชื่อ header ไม่ถูกต้อง
128:      *
129:      * @return \static
130:      */
131:     public function withAddedHeader($name, $value)
132:     {
133:         $this->filterHeader($name);
134:         $clone = clone $this;
135:         if (is_array($value)) {
136:             foreach ($value as $item) {
137:                 $clone->headers[$name][] = $item;
138:             }
139:         } else {
140:             $clone->headers[$name][] = $value;
141:         }
142:         return $clone;
143:     }
144: 
145:     /**
146:      * กำหนด stream
147:      *
148:      * @param streamInterface $body
149:      *
150:      * @return \static
151:      */
152:     public function withBody(StreamInterface $body)
153:     {
154:         $clone = clone $this;
155:         $clone->stream = $body;
156:         return $clone;
157:     }
158: 
159:     /**
160:      * กำหนด header แทนที่รายการเดิม
161:      *
162:      * @param string          $name  ชื่อของ Header
163:      * @param string|string[] $value ค่าของ Header เป็น string หรือ แอเรย์ของ string
164:      *
165:      * @throws \InvalidArgumentException for invalid header names or values
166:      *
167:      * @return \static
168:      */
169:     public function withHeader($name, $value)
170:     {
171:         $this->filterHeader($name);
172:         $clone = clone $this;
173:         $clone->headers[$name] = is_array($value) ? $value : (array) $value;
174:         return $clone;
175:     }
176: 
177:     /**
178:      * กำหนด header พร้อมกันหลายรายการ แทนที่รายการเดิม
179:      *
180:      * @param array $headers array($key => $value, $key => $value...)
181:      *
182:      * @throws \InvalidArgumentException for invalid header names or values
183:      *
184:      * @return \static
185:      */
186:     public function withHeaders($headers)
187:     {
188:         $clone = clone $this;
189:         foreach ($headers as $name => $value) {
190:             $this->filterHeader($name);
191:             $clone->headers[$name] = is_array($value) ? $value : (array) $value;
192:         }
193:         return $clone;
194:     }
195: 
196:     /**
197:      * กำหนดเวอร์ชั่นของโปรโตคอล.
198:      *
199:      * @param string $version เช่น 1.1, 1.0
200:      *
201:      * @return \static
202:      */
203:     public function withProtocolVersion($version)
204:     {
205:         $clone = clone $this;
206:         $clone->protocol = $version;
207:         return $clone;
208:     }
209: 
210:     /**
211:      * ลบ header.
212:      *
213:      * @param string $name ชื่อ header ที่ต้องการลบ
214:      *
215:      * @return \static
216:      */
217:     public function withoutHeader($name)
218:     {
219:         $clone = clone $this;
220:         unset($clone->headers[$name]);
221:         return $clone;
222:     }
223: 
224:     /**
225:      * ตรวจสอบความถูกต้องของ header
226:      *
227:      * @param string $name
228:      *
229:      * @throws \InvalidArgumentException ถ้า header ไม่ถูกต้อง
230:      */
231:     protected function filterHeader($name)
232:     {
233:         if (!preg_match('/^[a-zA-Z0-9\-]+$/', $name)) {
234:             throw new \InvalidArgumentException('Invalid header name');
235:         }
236:     }
237: 
238:     /**
239:      * ฟังก์ชั่นคืนค่า HTTP Header
240:      *
241:      * @return array
242:      */
243:     protected function getRequestHeaders()
244:     {
245:         $headers = array();
246:         if (function_exists("apache_request_headers")) {
247:             foreach (apache_request_headers() as $key => $value) {
248:                 if (preg_match('/^[a-zA-Z0-9\-]+$/', $key)) {
249:                     $headers[$key] = array($value);
250:                 }
251:             }
252:         } else {
253:             foreach ($_SERVER as $key => $value) {
254:                 if (preg_match('/^HTTP_([a-zA-Z0-9_]+)$/', $key, $match)) {
255:                     $headers[str_replace(' ', '-', ucwords(strtolower(str_replace(array('_', '-'), ' ', $match[1]))))] = array($value);
256:                 }
257:             }
258:         }
259:         return $headers;
260:     }
261: }
262: 
Kotchasan API documentation generated by ApiGen