diff --git a/CHANGELOG.md b/CHANGELOG.md index 04bf5f9..5ed6ee6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1419,8 +1419,9 @@ composer require planetbiru/magic-math 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. -Here’s the corrected version with improved grammar and clarity: +## New Method: `PicoPageData::getResultAsArray()` +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. ## Bug Fix: Handle Exception in method getDatabaseCredentialsFromPdo($pdo, $driver, $dbType) diff --git a/src/Database/PicoPageData.php b/src/Database/PicoPageData.php index 90341ab..f4ef083 100644 --- a/src/Database/PicoPageData.php +++ b/src/Database/PicoPageData.php @@ -280,6 +280,75 @@ public function getResult() return $this->result; } + /** + * Get the result as an array of stdClass objects or plain values. + * + * This method converts the internal result set, which may consist of MagicObject instances, + * into a plain PHP array. Each MagicObject is recursively converted to a stdClass object + * or a scalar value. + * + * @return array The result set as a plain PHP array. + */ + public function getResultAsArray() + { + return $this->magicObjectToArray($this->result); + } + + /** + * Recursively converts a MagicObject, an object, or an array of them into a plain PHP array or stdClass object. + * + * This method is useful for serialization or for passing the data to components + * that expect standard PHP types instead of MagicObject instances. + * + * @param mixed $data The data to convert. Can be a MagicObject, an array, an object, or a scalar value. + * @return mixed The converted data as a plain PHP array, stdClass object, or scalar value. + */ + protected function magicObjectToArray($data) + { + // Null or scalar -> return directly + if (is_null($data) || is_scalar($data)) { + return $data; + } + + // Array -> recursive + if (is_array($data)) { + return array_map(function ($item) { + return $this->magicObjectToArray($item); + }, $data); + } + + // Object + if (is_object($data)) { + // If there is a toArray method in MagicObject + if (method_exists($data, 'toArray')) { + return $this->magicObjectToArray($data->toArray()); + } + + // Get public properties + $vars = get_object_vars($data); + + // If empty, try using Reflection (for protected/private properties) + if (empty($vars)) { + $reflect = new \ReflectionClass($data); + foreach ($reflect->getProperties() as $prop) { + $prop->setAccessible(true); + $vars[$prop->getName()] = $prop->getValue($data); + } + } + + // Convert property values recursively + $obj = new \stdClass(); + foreach ($vars as $k => $v) { + $obj->$k = $this->magicObjectToArray($v); + } + return $obj; + } + + // Default fallback + return $data; + } + + /** * Get the current page number in the pagination context. *