Kotchasan Framework Documentation

Kotchasan Framework Documentation

ArrayResult

TH 05 Feb 2026 06:23

ArrayResult

\Kotchasan\Result\ArrayResult เป็น implementation ของ ResultInterface สำหรับ array-based results ใช้กับ cached queries

การสร้าง

use Kotchasan\Result\ArrayResult;

$data = [
    ['id' => 1, 'name' => 'John'],
    ['id' => 2, 'name' => 'Jane']
];

// Array format (default)
$result = new ArrayResult($data);

// Object format
$result = new ArrayResult($data, 'object');

Constructor

public function __construct(array $data, string $resultFormat = 'array')
Parameter Type Description
$data array Array of rows
$resultFormat string Format: 'array' (default) หรือ 'object'

Result Formats

Array Format (Default)

$result = new ArrayResult($data);

$row = $result->fetch();
echo $row['id'];
echo $row['name'];

Object Format

$result = new ArrayResult($data, 'object');

$row = $result->fetch();
echo $row->id;
echo $row->name;

Methods

Standard Methods (from ResultInterface)

Method Return Description
fetch() mixed ดึงแถวถัดไป
fetchAll() array ดึงแถวที่เหลือทั้งหมด
fetchColumn($col) mixed ดึง column เดียว
rowCount() int จำนวนแถว
columnCount() int จำนวน columns
getColumnMeta($col) array metadata ของ column
getResult() array raw data array
isEmpty() bool ว่างหรือไม่
hasMore() bool มีแถวเหลือหรือไม่
reset() bool รีเซ็ต pointer
close() bool ล้าง data
count() int จำนวนแถว

Additional Methods

Method Return Description
first() mixed ดึงแถวแรก (ไม่เลื่อน pointer)
all() array ดึงทั้งหมด (ไม่เลื่อน pointer)
getData() array ดึง raw array
getResultFormat() string ดึง result format

Iterator Methods

Method Description
current() แถวปัจจุบัน
key() position ปัจจุบัน
next() เลื่อนไปแถวถัดไป
rewind() กลับไปแถวแรก
valid() มี data ที่ position ปัจจุบันหรือไม่

ตัวอย่างการใช้งาน

Basic Usage

$data = [
    ['id' => 1, 'name' => 'John', 'email' => 'john@example.com'],
    ['id' => 2, 'name' => 'Jane', 'email' => 'jane@example.com']
];

$result = new ArrayResult($data);

// ดึงทีละแถว
while ($row = $result->fetch()) {
    echo $row['name'] . "\n";
}

Using first() and all()

$result = new ArrayResult($data, 'object');

// ดึงแถวแรก (ไม่เลื่อน pointer)
$first = $result->first();
echo $first->name;  // John

// ดึงทั้งหมด (ไม่เลื่อน pointer)
$all = $result->all();
echo count($all);  // 2

Using foreach (Iterator)

$result = new ArrayResult($data);

foreach ($result as $index => $row) {
    echo "{$index}: {$row['name']}\n";
}

Column Metadata

$result = new ArrayResult($data);

$meta = $result->getColumnMeta(0);
// ['name' => 'id', 'native_type' => 'STRING']

echo $result->columnCount();  // 3 (id, name, email)

Reset and Reread

$result = new ArrayResult($data);

$all1 = $result->fetchAll();  // ดึงหมด, pointer อยู่ท้าย
$result->reset();              // รีเซ็ต
$all2 = $result->fetchAll();  // ดึงได้อีกครั้ง

Close (Free Memory)

$result = new ArrayResult($data);
// ใช้งาน...

$result->close();  // ล้าง data array, free memory

Use Cases

Cached Query Results

// Query cache ส่งคืน ArrayResult
$cached = $cache->get('users_query');

if ($cached) {
    $result = new ArrayResult($cached);
} else {
    $result = $db->select('*')->from('user')->execute();
    $cache->set('users_query', $result->fetchAll());
}

Mock Results for Testing

// สร้าง mock result สำหรับ unit test
$mockData = [
    ['id' => 1, 'name' => 'Test User']
];

$result = new ArrayResult($mockData, 'object');
$user = $result->first();

$this->assertEquals('Test User', $user->name);

Converting PDOResult to ArrayResult

// ดึงจาก database
$pdoResult = $db->select('*')->from('user')->execute();
$data = $pdoResult->fetchAll();
$pdoResult->close();

// แปลงเป็น ArrayResult สำหรับ multiple iterations
$arrayResult = new ArrayResult($data);

// วนซ้ำได้หลายรอบ
foreach ($arrayResult as $user) { ... }
$arrayResult->reset();
foreach ($arrayResult as $user) { ... }

Internal Properties

Property Type Description
$data array Raw data array
$position int Current position (0-indexed)
$resultFormat string 'array' or 'object'

คลาสที่เกี่ยวข้อง