Overview

Namespaces

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

Classes

  • ArrayIterator
  • DateInterval
  • DOMNode
  • Kotchasan
  • Kotchasan\Accordion
  • 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\Object
  • Kotchasan\Orm\Field
  • Kotchasan\Orm\Recordset
  • Kotchasan\Password
  • Kotchasan\Pdf
  • Kotchasan\Province
  • Kotchasan\Router
  • Kotchasan\Singleton
  • Kotchasan\Tab
  • Kotchasan\TableRow
  • Kotchasan\Template
  • Kotchasan\Text
  • Kotchasan\Validator
  • Kotchasan\View
  • PDF\FPDF
  • 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
  • Throwable
  • Traversable

Traits

  • Psr\Log\LoggerTrait

Exceptions

  • ErrorException
  • Exception
  • InvalidArgumentException
  • Kotchasan\Cache\Exception
  • Kotchasan\Database\Exception
  • LogicException
  • phpmailerException
  • RuntimeException

Functions

  • createClass
  • debug
  • getClassPath
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * @filesource Kotchasan/ListItem.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;
 12: 
 13: /**
 14:  * คลาสสำหรับจัดการแอเรย์.
 15:  *
 16:  * @author Goragod Wiriya <admin@goragod.com>
 17:  *
 18:  * @since 1.0
 19:  */
 20: class ListItem
 21: {
 22:     /**
 23:      * ข้อมูล.
 24:      *
 25:      * @var array
 26:      */
 27:     public $datas;
 28:     /**
 29:      * ที่อยู่ไฟล์ที่โหลดมา.
 30:      *
 31:      * @var string
 32:      */
 33:     private $source;
 34: 
 35:     /**
 36:      * นำเข้าข้อมูลครั้งละหลายรายการ.
 37:      *
 38:      * @param array $array ข้อมูลที่ต้องการนำเข้า
 39:      */
 40:     public function assign($array)
 41:     {
 42:         if (isset($this->datas)) {
 43:             $this->datas = array_merge($this->datas, $array);
 44:         } else {
 45:             $this->datas = $array;
 46:         }
 47:     }
 48: 
 49:     /**
 50:      * ลบข้อมูลทั้งหมด.
 51:      */
 52:     public function clear()
 53:     {
 54:         unset($this->datas);
 55:     }
 56: 
 57:     /**
 58:      * อ่านจำนวนสมาชิกทั้งหมด.
 59:      *
 60:      * @return int จำนวนสมาชิกทั้งหมด
 61:      */
 62:     public function count()
 63:     {
 64:         return count($this->datas);
 65:     }
 66: 
 67:     /**
 68:      * ลบรายการที่กำหนด
 69:      * คืนค่า true ถ้าสำเร็จ, false ถ้าไม่พบ.
 70:      *
 71:      * @param string $key ของรายการที่ต้องการจะลบ
 72:      *
 73:      * @return bool
 74:      */
 75:     public function delete($key)
 76:     {
 77:         if (array_key_exists($key, $this->datas)) {
 78:             unset($this->datas[$key]);
 79: 
 80:             return true;
 81:         }
 82: 
 83:         return false;
 84:     }
 85: 
 86:     /**
 87:      * คืนค่าแอเรย์รายการแรก
 88:      *
 89:      * @return mixed
 90:      */
 91:     public function firstItem()
 92:     {
 93:         return reset($this->datas);
 94:     }
 95: 
 96:     /**
 97:      * อ่านข้อมูลที่ $key
 98:      * คืนค่ารายการที่ $key ถ้าไม่พบคืนค่า null.
 99:      *
100:      * @param string $key
101:      *
102:      * @return mixed
103:      */
104:     public function get($key)
105:     {
106:         return array_key_exists($key, $this->datas) ? $this->datas[$key] : null;
107:     }
108: 
109:     /**
110:      * ค้นหาข้อมูลในแอเรย์
111:      * คืนค่า key ของรายการที่พบ ถ้าไม่พบคืนค่า false.
112:      *
113:      * @param mixed $value รายการค้นหา
114:      *
115:      * @return mixed
116:      */
117:     public function indexOf($value)
118:     {
119:         return array_search($value, $this->datas);
120:     }
121: 
122:     /**
123:      * กำหนดค่าเริ่มต้นของ Class.
124:      *
125:      * @param array $config
126:      */
127:     public function init($config)
128:     {
129:         $this->datas = $config;
130:     }
131: 
132:     /**
133:      * เพิ่มรายการใหม่ต่อจากรายการที่ $key.
134:      *
135:      * @param mixed $key
136:      * @param mixed $item รายการใหม่
137:      */
138:     public function insert($key, $item)
139:     {
140:         if (is_int($key) && $key == count($this->datas)) {
141:             $this->datas[] = $item;
142:         } else {
143:             $temp = $this->datas;
144:             $this->datas = array();
145:             foreach ($temp as $k => $value) {
146:                 $this->datas[$k] = $value;
147:                 if ($k == $key) {
148:                     $this->datas[$key] = $item;
149:                 }
150:             }
151:         }
152:     }
153: 
154:     /**
155:      * เพิ่มรายการใหม่ก่อนรายการที่ $key.
156:      *
157:      * @param mixed $key
158:      * @param mixed $item รายการใหม่
159:      */
160:     public function insertBefore($key, $item)
161:     {
162:         $temp = $this->datas;
163:         $this->datas = array();
164:         foreach ($temp as $k => $value) {
165:             if ($k == $key) {
166:                 $this->datas[$key] = $item;
167:             }
168:             $this->datas[$k] = $value;
169:         }
170:     }
171: 
172:     /**
173:      * อ่านจำนวนรายการทั้งหมด.
174:      *
175:      * @return array
176:      */
177:     public function items()
178:     {
179:         return $this->datas;
180:     }
181: 
182:     /**
183:      * อ่านรายชื่อ keys
184:      * คืนค่า แอเรย์ของรายการ key ทั้งหมด.
185:      *
186:      * @return array
187:      */
188:     public function keys()
189:     {
190:         return array_keys($this->datas);
191:     }
192: 
193:     /**
194:      * คืนค่าแอเรย์รายการสุดท้าย.
195:      *
196:      * @return mixed
197:      */
198:     public function lastItem()
199:     {
200:         return end($this->datas);
201:     }
202: 
203:     /**
204:      * โหลดแอเรย์จากไฟล์.
205:      *
206:      * @param string $file ชื่อไฟล์ที่ต้องการโหลดรวม path
207:      *
208:      * @return \static
209:      */
210:     public function loadFromFile($file)
211:     {
212:         if (is_file($file)) {
213:             $config = include $file;
214:             $this->source = $file;
215:             $this->assign($config);
216:         }
217: 
218:         return $this;
219:     }
220: 
221:     /**
222:      * บันทึกเป็นไฟล์
223:      * คืนค่า true ถ้าสำเร็จ.
224:      *
225:      * @return bool
226:      */
227:     public function saveToFile()
228:     {
229:         if (!isset($this->source) || empty($this->datas)) {
230:             return false;
231:         } else {
232:             $datas = array();
233:             foreach ($this->datas as $key => $value) {
234:                 if (is_array($value)) {
235:                     $datas[] = (is_int($key) ? $key : "'".strtolower($key)."'")." => array(\n".$this->arrayToString(1, $value)."\n\t)";
236:                 } else {
237:                     $datas[] = (is_int($key) ? $key : "'".strtolower($key)."'").' => '.(is_int($value) ? $value : "'".addslashes($value)."'");
238:                 }
239:             }
240:             $file = str_replace(ROOT_PATH, '', $this->source);
241:             $f = @fopen(ROOT_PATH.$file, 'w');
242:             if ($f === false) {
243:                 return false;
244:             } else {
245:                 fwrite($f, "<?php\n/* $file */\nreturn array (\n\t".implode(",\n\t", $datas)."\n);");
246:                 fclose($f);
247: 
248:                 return true;
249:             }
250:         }
251:     }
252: 
253:     /**
254:      * เพิ่มรายการใหม่ที่ลำดับสุดท้าย ถ้ามี $key อยู่แล้วจะแทนที่รายการเดิม
255:      *
256:      * @param string $key
257:      * @param mixed  $value
258:      */
259:     public function set($key, $value)
260:     {
261:         $this->datas[$key] = $value;
262:     }
263: 
264:     /**
265:      * คืนค่า แอเรย์ของข้อมูลทั้งหมด.
266:      *
267:      * @return array
268:      */
269:     public function values()
270:     {
271:         return array_values($this->datas);
272:     }
273: 
274:     /**
275:      * array to string.
276:      *
277:      * @param int   $indent
278:      * @param array $array
279:      *
280:      * @return string
281:      */
282:     private function arrayToString($indent, $array)
283:     {
284:         $t = str_repeat("\t", $indent + 1);
285:         foreach ($array as $key => $value) {
286:             if (is_array($value)) {
287:                 $datas[] = (is_int($key) ? $key : "'$key'")." => array(\n".$this->arrayToString($indent + 1, $value)."\n$t)";
288:             } else {
289:                 $datas[] = (is_int($key) ? $key : "'$key'").' => '.(is_int($value) ? $value : "'".addslashes($value)."'");
290:             }
291:         }
292: 
293:         return $t.implode(",\n$t", $datas);
294:     }
295: }
296: 
kotchasan API documentation generated by ApiGen