Kotchasan Framework Documentation
คลาส ListItem - จัดการข้อมูลแบบ List
คลาส ListItem - จัดการข้อมูลแบบ List
คลาส ListItem เป็นคลาสสำหรับจัดการข้อมูลแบบ list (array) พร้อมความสามารถในการบันทึกและโหลดข้อมูลจากไฟล์
Namespace
Kotchasan\ListItemภาพรวม
ListItem class ให้บริการ:
- จัดการข้อมูลแบบ associative array
- เพิ่ม/ลบ/แก้ไข items
- แทรก items ก่อน/หลังตำแหน่งที่ต้องการ
- ค้นหา item จากค่า (indexOf)
- โหลด/บันทึกข้อมูลจากไฟล์ PHP
Public Properties
public $datas; // array - ข้อมูลทั้งหมดPublic Methods (18 methods)
init()
เริ่มต้นคลาสด้วยข้อมูล
public function init(array $config): voidตัวอย่าง:
$list = new ListItem();
$list->init(['a' => 1, 'b' => 2, 'c' => 3]);assign()
เพิ่มข้อมูลหลายรายการ (merge)
public function assign(array $array): voidตัวอย่าง:
$list->assign(['d' => 4, 'e' => 5]);set()
กำหนดค่าที่ key
public function set(string $key, mixed $value): voidตัวอย่าง:
$list->set('name', 'John');
$list->set('age', 30);get()
ดึงค่าจาก key
public function get(string $key): mixedตัวอย่าง:
$name = $list->get('name'); // 'John'
$unknown = $list->get('notfound'); // nulldelete()
ลบ item
public function delete(string $key): boolคืนค่า: true ถ้าลบสำเร็จ, false ถ้าไม่พบ
ตัวอย่าง:
$list->delete('age'); // true
$list->delete('notfound'); // falseclear()
ล้างข้อมูลทั้งหมด
public function clear(): voidตัวอย่าง:
$list->clear();count()
นับจำนวน items
public function count(): intตัวอย่าง:
echo $list->count(); // 3items()
ดึงทั้งหมดเป็น array
public function items(): arrayตัวอย่าง:
$all = $list->items();
// ['name' => 'John', 'age' => 30]keys()
ดึง array ของ keys
public function keys(): arrayตัวอย่าง:
$keys = $list->keys();
// ['name', 'age']values()
ดึง array ของ values
public function values(): arrayตัวอย่าง:
$values = $list->values();
//['John', 30]firstItem()
ดึง item แรก
public function firstItem(): mixedตัวอย่าง:
$first = $list->firstItem();lastItem()
ดึง item สุดท้าย
public function lastItem(): mixedตัวอย่าง:
$last = $list->lastItem();indexOf()
ค้นหา key จากค่า
public function indexOf(mixed $value): mixedคืนค่า: key ที่พบ หรือ false
ตัวอย่าง:
$list->init(['a' => 'apple', 'b' => 'banana']);
$key = $list->indexOf('banana'); // 'b'
$notfound = $list->indexOf('orange'); // falseinsert()
แทรก item หลัง key ที่ระบุ
public function insert(mixed $key, mixed $item): voidตัวอย่าง:
$list->init(['a' => 1, 'c' => 3]);
$list->insert('a', ['b' => 2]);
// Result: ['a' => 1, 'b' => 2, 'c' => 3]insertBefore()
แทรก item ก่อน key ที่ระบุ
public function insertBefore(mixed $key, mixed $item): voidตัวอย่าง:
$list->init(['a' => 1, 'c' => 3]);
$list->insertBefore('c', ['b' => 2]);
// Result: ['a' => 1, 'b' => 2, 'c' => 3]loadFromFile()
โหลดข้อมูลจากไฟล์ PHP
public function loadFromFile(string $file): staticตัวอย่าง:
$list = new ListItem();
$list->loadFromFile('config/settings.php');saveToFile()
บันทึกข้อมูลลงไฟล์
public function saveToFile(): boolคืนค่า: true ถ้าบันทึกสำเร็จ
ตัวอย่าง:
$list->loadFromFile('config/settings.php');
$list->set('version', '2.0');
$list->saveToFile();ตัวอย่างการใช้งานครบวงจร
1. Configuration Management
use Kotchasan\ListItem;
// โหลด config
$config = new ListItem();
$config->loadFromFile('config/app.php');
// อ่านค่า
$appName = $config->get('app_name');
$version = $config->get('version');
// แก้ไขค่า
$config->set('debug_mode', true);
$config->set('version', '2.0.1');
// บันทึก
$config->saveToFile();2. Dynamic Menu Management
use Kotchasan\ListItem;
// สร้าง menu
$menu = new ListItem();
$menu->init([
'home' => ['text' => 'หน้าแรก', 'url' => '/'],
'about' => ['text' => 'เกี่ยวกับ', 'url' => '/about'],
'contact' => ['text' => 'ติดต่อ', 'url' => '/contact']
]);
// แทรก menu ใหม่หลัง 'about'
$menu->insert('about', [
'products' => ['text' => 'สินค้า', 'url' => '/products']
]);
// แทรก menu ก่อน 'contact'
$menu->insertBefore('contact', [
'blog' => ['text' => 'บล็อก', 'url' => '/blog']
]);
// ลบ menu
if ($menu->get('old_page')) {
$menu->delete('old_page');
}
// แสดง menu
foreach ($menu->items() as $key => $item) {
echo "<a href='{$item['url']}'>{$item['text']}</a>";
}3. Settings Manager
use Kotchasan\ListItem;
class SettingsManager
{
private $settings;
public function __construct($file)
{
$this->settings = new ListItem();
$this->settings->loadFromFile($file);
}
public function get($key, $default = null)
{
$value = $this->settings->get($key);
return $value !== null ? $value : $default;
}
public function set($key, $value)
{
$this->settings->set($key, $value);
return $this;
}
public function has($key)
{
return $this->settings->get($key) !== null;
}
public function remove($key)
{
return $this->settings->delete($key);
}
public function all()
{
return $this->settings->items();
}
public function save()
{
return $this->settings->saveToFile();
}
public function reset()
{
$this->settings->clear();
return $this;
}
}
// ใช้งาน
$config = new SettingsManager('config/app.php');
// อ่านค่า
$timezone = $config->get('timezone', 'Asia/Bangkok');
$debug = $config->get('debug', false);
// ตั้งค่า
$config->set('app_name', 'My App')
->set('version', '1.0.0')
->save();
// ลบ
if ($config->has('old_setting')) {
$config->remove('old_setting')->save();
}4. Data Cache with File
use Kotchasan\ListItem;
class CacheManager
{
private $cache;
public function __construct()
{
$this->cache = new ListItem();
$this->cache->loadFromFile('cache/data.php');
}
public function get($key)
{
$data = $this->cache->get($key);
if ($data && isset($data['expires'])) {
if (time() > $data['expires']) {
// หมดอายุแล้ว
$this->cache->delete($key);
return null;
}
return $data['value'];
}
return $data;
}
public function set($key, $value, $ttl = 3600)
{
$this->cache->set($key, [
'value' => $value,
'expires' => time() + $ttl
]);
return $this->cache->saveToFile();
}
public function delete($key)
{
$this->cache->delete($key);
return $this->cache->saveToFile();
}
public function clear()
{
$this->cache->clear();
return $this->cache->saveToFile();
}
public function cleanup()
{
$now = time();
$items = $this->cache->items();
foreach ($items as $key => $data) {
if (isset($data['expires']) && $now > $data['expires']) {
$this->cache->delete($key);
}
}
return $this->cache->saveToFile();
}
}
// ใช้งาน
$cache = new CacheManager();
// บันทึก
$cache->set('user_123', ['name' => 'John', 'email' => 'john@example.com'], 3600);
// อ่าน
$user = $cache->get('user_123');
// ล้าง cache หมดอายุ
$cache->cleanup();5. Translation Management
use Kotchasan\ListItem;
class Translator
{
private $translations;
private $locale;
public function __construct($locale = 'th')
{
$this->locale = $locale;
$this->translations = new ListItem();
$this->translations->loadFromFile("lang/{$locale}.php");
}
public function trans($key, $params = [])
{
$text = $this->translations->get($key);
if ($text === null) {
return $key;
}
foreach ($params as $placeholder => $value) {
$text = str_replace($placeholder, $value, $text);
}
return $text;
}
public function add($key, $value)
{
$this->translations->set($key, $value);
return $this;
}
public function save()
{
return $this->translations->saveToFile();
}
public function switchLocale($locale)
{
$this->locale = $locale;
$this->translations->clear();
$this->translations->loadFromFile("lang/{$locale}.php");
}
}
// ใช้งาน
$t = new Translator('th');
echo $t->trans('welcome'); // "ยินดีต้อนรับ"
echo $t->trans('hello_user', [':name' => 'John']); // "สวัสดี John"
// เพิ่มคำแปลใหม่
$t->add('goodbye', 'ลาก่อน')->save();
// เปลี่ยนภาษา
$t->switchLocale('en');
echo $t->trans('welcome'); // "Welcome"Best Practices
1. การจัดการไฟล์
// ✅ ดี: ใช้ loadFromFile ก่อนแก้ไข
$config = new ListItem();
$config->loadFromFile('config/app.php');
$config->set('version', '2.0');
$config->saveToFile();
// ❌ ไม่ดี: saveToFile โดยไม่ได้ loadFromFile ก่อน
$config = new ListItem();
$config->set('version', '2.0');
$config->saveToFile(); // จะ return false เพราะไม่มี source file2. การจัดการ Keys
// ✅ ดี: ตรวจสอบก่อนใช้
$value = $list->get('key');
if ($value !== null) {
// ใช้ $value
}
// ✅ ดี: ใช้ default value
$value = $list->get('key') ?? 'default';
// ✅ ดี: ตรวจสอบก่อนลบ
if ($list->get('old_key') !== null) {
$list->delete('old_key');
}3. Performance
// ✅ ดี: บันทึกครั้งเดียวหลังแก้ไขหลายครั้ง
$config->set('key1', 'value1');
$config->set('key2', 'value2');
$config->set('key3', 'value3');
$config->saveToFile(); // บันทึกครั้งเดียว
// ❌ ไม่ดี: บันทึกทุกครั้ง
$config->set('key1', 'value1');
$config->saveToFile();
$config->set('key2', 'value2');
$config->saveToFile(); // ช้า!สรุป
คลาส ListItem เป็นคลาสจัดการข้อมูลแบบ list ที่:
- ✅ จัดการข้อมูลแบบ key-value
- ✅ แทรก/ลบ items ได้ยืดหยุ่น
- ✅ โหลด/บันทึกไฟล์ PHP
- ✅ ค้นหาข้อมูลได้
- 📝 เหมาะสำหรับ config files
- 🔄 Chain methods ได้
เหมาะสำหรับ:
- Configuration management
- Dynamic menus
- Settings storage
- Data caching
- Translation files
ขนาดไฟล์: 279 บรรทัด
Public Methods: 18 methods
Storage: PHP array files