Skip to content

Commit 01679fc

Browse files
authored
Merge pull request #171 from Planetbiru/feature/version-3.20.0
Add method PicoPageData::getResultAsArray()
2 parents 213b6c6 + 566f07b commit 01679fc

2 files changed

Lines changed: 71 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1419,8 +1419,9 @@ composer require planetbiru/magic-math
14191419

14201420
A new method `isPdoConnected()` has been introduced to allow developers to verify not only the TCP-level connection, but also the ability of PHP to execute SQL statements on the database.
14211421

1422-
Here’s the corrected version with improved grammar and clarity:
1422+
## New Method: `PicoPageData::getResultAsArray()`
14231423

1424+
The new **`getResultAsArray()`** method allows users to return a list of data directly as an array, which can be immediately sent as JSON. This eliminates the need to first collect the data as an array of objects and then manually construct an array.
14241425

14251426
## Bug Fix: Handle Exception in method getDatabaseCredentialsFromPdo($pdo, $driver, $dbType)
14261427

src/Database/PicoPageData.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,75 @@ public function getResult()
280280
return $this->result;
281281
}
282282

283+
/**
284+
* Get the result as an array of stdClass objects or plain values.
285+
*
286+
* This method converts the internal result set, which may consist of MagicObject instances,
287+
* into a plain PHP array. Each MagicObject is recursively converted to a stdClass object
288+
* or a scalar value.
289+
*
290+
* @return array The result set as a plain PHP array.
291+
*/
292+
public function getResultAsArray()
293+
{
294+
return $this->magicObjectToArray($this->result);
295+
}
296+
297+
/**
298+
* Recursively converts a MagicObject, an object, or an array of them into a plain PHP array or stdClass object.
299+
*
300+
* This method is useful for serialization or for passing the data to components
301+
* that expect standard PHP types instead of MagicObject instances.
302+
*
303+
* @param mixed $data The data to convert. Can be a MagicObject, an array, an object, or a scalar value.
304+
* @return mixed The converted data as a plain PHP array, stdClass object, or scalar value.
305+
*/
306+
protected function magicObjectToArray($data)
307+
{
308+
// Null or scalar -> return directly
309+
if (is_null($data) || is_scalar($data)) {
310+
return $data;
311+
}
312+
313+
// Array -> recursive
314+
if (is_array($data)) {
315+
return array_map(function ($item) {
316+
return $this->magicObjectToArray($item);
317+
}, $data);
318+
}
319+
320+
// Object
321+
if (is_object($data)) {
322+
// If there is a toArray method in MagicObject
323+
if (method_exists($data, 'toArray')) {
324+
return $this->magicObjectToArray($data->toArray());
325+
}
326+
327+
// Get public properties
328+
$vars = get_object_vars($data);
329+
330+
// If empty, try using Reflection (for protected/private properties)
331+
if (empty($vars)) {
332+
$reflect = new \ReflectionClass($data);
333+
foreach ($reflect->getProperties() as $prop) {
334+
$prop->setAccessible(true);
335+
$vars[$prop->getName()] = $prop->getValue($data);
336+
}
337+
}
338+
339+
// Convert property values recursively
340+
$obj = new \stdClass();
341+
foreach ($vars as $k => $v) {
342+
$obj->$k = $this->magicObjectToArray($v);
343+
}
344+
return $obj;
345+
}
346+
347+
// Default fallback
348+
return $data;
349+
}
350+
351+
283352
/**
284353
* Get the current page number in the pagination context.
285354
*

0 commit comments

Comments
 (0)