Skip to content

Commit

Permalink
Merge pull request #20 from Planetbiru/feature/2.0
Browse files Browse the repository at this point in the history
Feature/2.0
  • Loading branch information
kamshory authored Oct 18, 2024
2 parents ee9c896 + 975219f commit e595e4a
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 37 deletions.
47 changes: 47 additions & 0 deletions src/Exceptions/InvalidReturnTypeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
namespace MagicObject\Exceptions;

use Exception;
use Throwable;

/**
* Class InvalidReturnTypeException
*
* Exception thrown when an invalid return type is encountered.
*
* @package MagicObject\Exceptions
*/
class InvalidReturnTypeException extends Exception
{
/**
* Previous exception
*
* @var Throwable|null
*/
private $previous;

/**
* Constructor for InvalidReturnTypeException.
*
* @param string $message Exception message
* @param int $code Exception code (default: 0)
* @param Throwable|null $previous Previous exception (optional)
*/
public function __construct($message, $code = 0, $previous = null)
{
parent::__construct($message, $code, $previous);
$this->previous = $previous;
}

/**
* Get the previous exception.
*
* Returns the previous exception if it exists, or null if there is no previous exception.
*
* @return Throwable|null The previous exception
*/
public function getPreviousException()
{
return $this->previous;
}
}
75 changes: 44 additions & 31 deletions src/MagicObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
use MagicObject\Database\PicoSortable;
use MagicObject\Database\PicoSpecification;
use MagicObject\Database\PicoTableInfo;
use MagicObject\DataLabel\PicoDataLabel;
use MagicObject\Exceptions\FindOptionException;
use MagicObject\Exceptions\InvalidAnnotationException;
use MagicObject\Exceptions\InvalidQueryInputException;
use MagicObject\Exceptions\InvalidReturnTypeException;
use MagicObject\Exceptions\NoDatabaseConnectionException;
use MagicObject\Exceptions\NoRecordFoundException;
use MagicObject\Util\ClassUtil\PicoAnnotationParser;
Expand Down Expand Up @@ -671,7 +671,8 @@ public function selectQuery()
* - instances of a specified class if the return type matches a class name.
*
* @throws PDOException If there is an error executing the database query.
* @throws InvalidQueryInputException If there is no query to be executed on the database query.
* @throws InvalidQueryInputException If there is no query to be executed.
* @throws InvalidReturnTypeException If the return type specified is invalid.
*/
protected function executeNativeQuery() //NOSONAR
{
Expand All @@ -696,26 +697,21 @@ protected function executeNativeQuery() //NOSONAR
$queryString = trim($queryString, " \r\n\t ");
if(empty($queryString))
{
throw new InvalidQueryInputException("No query found.\r\n".$docComment);
// Try reading the query in another way
preg_match('/@query\s*\(\s*"(.*?)"\s*\)/s', $docComment, $matches);
$queryString = $matches ? $matches[1] : '';
if(empty($queryString))
{
throw new InvalidQueryInputException("No query found.\r\n".$docComment);
}
}

// Get parameter information from the caller function
$callerParams = $reflection->getParameters();

// Get return type from the caller function
$returnTypeObj = $reflection->getReturnType();

if($returnTypeObj == null)
{
// PHP 5
preg_match('/@return\s+([^\s]+)/', $docComment, $matches);
$returnType = $matches ? $matches[1] : '';
}
else
{
// PHP 7 or above
$returnType = $returnTypeObj."";
}
preg_match('/@return\s+([^\s]+)/', $docComment, $matches);
$returnType = $matches ? $matches[1] : 'void';

// Trim return type
$returnType = trim($returnType);
Expand Down Expand Up @@ -797,35 +793,52 @@ protected function executeNativeQuery() //NOSONAR
return json_encode($stmt->fetchAll(PDO::FETCH_OBJ));
} else {
try {
// Check for array-type hinting in the return type
// Check for array-type hinting in the return type
if (stripos($returnType, "[") !== false) {
$className = trim(explode("[", $returnType)[0]);
if (class_exists($className)) {
$className = trim(explode("[", $returnType)[0]);
if ($className == "stdClass") {
// Return all rows as stdClass objects
return $stmt->fetchAll(PDO::FETCH_OBJ);
}
else if($className == 'MagicObject') {
$result = $stmt->fetchAll(PDO::FETCH_OBJ);
$ret = [];
if ($className == "stdClass") {
// Return all rows as stdClass objects
return $stmt->fetchAll(PDO::FETCH_OBJ);
} else {
// Map result rows to the specified class
foreach ($result as $row) {
$ret[] = new MagicObject($row);
}
return $ret;
}
else if (class_exists($className)) {
// Map result rows to the specified class
$obj = new $className();
if($obj instanceof MagicObject) {
$result = $stmt->fetchAll(PDO::FETCH_OBJ);
foreach ($result as $row) {
$ret[] = new $className($row);
}
return $ret;
}
}
}
}
throw new InvalidReturnTypeException("Invalid return type for $className");
} else {
// Return a single object of the specified class
$className = trim($returnType);
if (class_exists($className)) {
$row = $stmt->fetch(PDO::FETCH_OBJ);
return new $className($row);
if($className == 'MagicObject') {
$row = $stmt->fetch(PDO::FETCH_OBJ);
return new MagicObject($row);
}
else if (class_exists($className)) {
$obj = new $className();
if($obj instanceof MagicObject) {
$row = $stmt->fetch(PDO::FETCH_OBJ);
return $obj->loadData($row);
}
}
throw new InvalidReturnTypeException("Invalid return type for $className");
}
} catch (Exception $e) {
// Log the exception if the class is not found
error_log('Class not found: ' . $e->getMessage());
return null;
throw new InvalidReturnTypeException("Invalid return type for $className");
}
}
}
Expand Down
20 changes: 14 additions & 6 deletions tests/caller.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
require_once dirname(__DIR__) . "/vendor/autoload.php";

$databaseCredential = new SecretObject();
$databaseCredential->loadYamlFile(dirname(dirname(__DIR__)) . "/test.yml", false, true, true);
$databaseCredential->loadYamlFile(dirname(dirname(__DIR__)) . "/test.yml.txt", false, true, true);
$databaseCredential->getDatabase()->setDatabaseName("sipro");
$database = new PicoDatabase($databaseCredential->getDatabase(), null, function($sql){
echo $sql.";\r\n\r\n";
Expand Down Expand Up @@ -254,9 +254,9 @@ public function native11($supervisorId, $aktif)
*
* @param int $supervisorId The ID of the table to search for.
* @param DateTime[] $timeCreate The date and time when data is created
* @return self[]
* @return MagicObject
* @query("
SELECT supervisor.*
SELECT supervisor.*
FROM supervisor
WHERE supervisor.waktu_buat in :timeCreate
AND supervisor.aktif = :aktif
Expand All @@ -271,7 +271,7 @@ public function native13($timeCreate, $aktif)

$obj = new Supervisor(null, $database);


/*
$native1 = $obj->native1(1, true);
$native2 = $obj->native2(1, true);
Expand Down Expand Up @@ -321,7 +321,15 @@ public function native13($timeCreate, $aktif)
echo "Alamat: " . $native9[0]->getTelepon() . "\r\n";
echo "Alamat: " . $native10->getTelepon() . "\r\n";
echo "Alamat: " . $native11[0]->getTelepon() . "\r\n";
*/

$native13 = $obj->native13([new DateTime('2023-03-27 15:23:47', new DateTimeZone($databaseCredential->getDatabase()->getTimeZone()))], true);
try
{
$native13 = $obj->native13([new DateTime('2025-03-27 15:23:47', new DateTimeZone($databaseCredential->getDatabase()->getTimeZone()))], true);
echo "\r\nnative13:\r\n";
print_r($native13);
echo ($native13);
}
catch(Exception $e)
{
echo $e->getMessage();
}

0 comments on commit e595e4a

Please sign in to comment.