From d7b1cac719710e0030a1b1a10602d63e2fcb689d Mon Sep 17 00:00:00 2001 From: "Kamshory, MT" Date: Sat, 7 Dec 2024 23:47:59 +0700 Subject: [PATCH 1/3] Update Utility --- src/Util/Database/PicoDatabaseUtilPostgreSql.php | 13 ++++++++----- src/Util/Database/PicoDatabaseUtilSqlite.php | 12 ++++++++---- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/Util/Database/PicoDatabaseUtilPostgreSql.php b/src/Util/Database/PicoDatabaseUtilPostgreSql.php index 1ab0ae90..b071a106 100644 --- a/src/Util/Database/PicoDatabaseUtilPostgreSql.php +++ b/src/Util/Database/PicoDatabaseUtilPostgreSql.php @@ -333,12 +333,15 @@ public function fixDefaultValue($defaultValue, $type) else if (stripos($type, 'enum') !== false || stripos($type, 'varchar') !== false || stripos($type, 'char') !== false || stripos($type, 'text') !== false) { return "'" . addslashes($defaultValue) . "'"; } - else if(stripos($type, 'int') !== false - || stripos($type, 'real') !== false - || stripos($type, 'float') !== false - || stripos($type, 'double') !== false) + else if(stripos($type, 'int') !== false) { - return $defaultValue + 0; + $defaultValue = preg_replace('/[^\d]/', '', $defaultValue); + return (int)$defaultValue; + } + else if(stripos($type, 'decimal') !== false || stripos($type, 'float') !== false || stripos($type, 'double') !== false || stripos($type, 'real') !== false) + { + $defaultValue = preg_replace('/[^\d.]/', '', $defaultValue); + return (float)$defaultValue; } return $defaultValue; } diff --git a/src/Util/Database/PicoDatabaseUtilSqlite.php b/src/Util/Database/PicoDatabaseUtilSqlite.php index 91ca2d23..a7161fe1 100644 --- a/src/Util/Database/PicoDatabaseUtilSqlite.php +++ b/src/Util/Database/PicoDatabaseUtilSqlite.php @@ -495,11 +495,15 @@ public function fixDefaultValue($defaultValue, $type) { return "'".$defaultValue."'"; } - else if(stripos($type, 'int') !== false - || stripos($type, 'float') !== false - || stripos($type, 'double') !== false) + else if(stripos($type, 'int') !== false) { - return $defaultValue + 0; + $defaultValue = preg_replace('/[^\d]/', '', $defaultValue); + return (int)$defaultValue; + } + else if(stripos($type, 'decimal') !== false || stripos($type, 'float') !== false || stripos($type, 'double') !== false || stripos($type, 'real') !== false) + { + $defaultValue = preg_replace('/[^\d.]/', '', $defaultValue); + return (float)$defaultValue; } return $defaultValue; } From 4ff4e99da1a0dfd673d734f6f2571f75e24ffd7c Mon Sep 17 00:00:00 2001 From: "Kamshory, MT" Date: Sun, 8 Dec 2024 01:00:39 +0700 Subject: [PATCH 2/3] Update PicoDatabaseDump.php --- src/Generator/PicoDatabaseDump.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Generator/PicoDatabaseDump.php b/src/Generator/PicoDatabaseDump.php index 3d5f2fe6..613d0ec5 100644 --- a/src/Generator/PicoDatabaseDump.php +++ b/src/Generator/PicoDatabaseDump.php @@ -72,6 +72,12 @@ public function dumpStructure($entity, $databaseType, $createIfNotExists = false $tool = new PicoDatabaseUtilPostgreSql(); return $tool->dumpStructure($tableInfo, $picoTableName, $createIfNotExists, $dropIfExists, $engine, $charset); } + else if($databaseType == PicoDatabaseType::DATABASE_TYPE_SQLITE) + { + $tool = new PicoDatabaseUtilSqlite(); + return $tool->dumpStructure($tableInfo, $picoTableName, $createIfNotExists, $dropIfExists, $engine, $charset); + } + return ""; } /** @@ -103,6 +109,7 @@ public function dumpStructureTable($tableInfo, $databaseType, $createIfNotExists $tool = new PicoDatabaseUtilSqlite(); return $tool->dumpStructure($tableInfo, $picoTableName, $createIfNotExists, $dropIfExists, $engine, $charset); } + return ""; } /** From 6331366408a85b3d1fa617156b9f96fd83589279 Mon Sep 17 00:00:00 2001 From: "Kamshory, MT" Date: Sun, 8 Dec 2024 23:47:32 +0700 Subject: [PATCH 3/3] Update Docblock --- src/MagicDto.php | 25 +++-- src/MagicObject.php | 213 +++++++++++++++++++++++++++++-------------- src/SecretObject.php | 1 - 3 files changed, 165 insertions(+), 74 deletions(-) diff --git a/src/MagicDto.php b/src/MagicDto.php index 82b93797..8d69242a 100644 --- a/src/MagicDto.php +++ b/src/MagicDto.php @@ -828,26 +828,37 @@ public function arrayToXml($dataArray, $xml) { } } + /** + * Check if a method is overridden in a child class. + * + * This method uses reflection to determine if a given method is overridden in the specified child class. + * It compares the methods of the child class with those of its parent class, checking if the method + * is present in both classes but has been redefined in the child class. + * + * @param string $childClass The child class name or instance to check for method override. + * @param string $methodName The name of the method to check for overriding. + * @return bool Returns true if the method is overridden in the child class, false otherwise. + */ private function isMethodOverridden($childClass, $methodName) { - // Buat instance ReflectionClass untuk kelas anak + // Create a ReflectionClass instance for the child class $childReflection = new ReflectionClass($childClass); - // Dapatkan kelas induk + // Get the parent class $parentClass = $childReflection->getParentClass(); if ($parentClass) { - // Dapatkan metode dari kelas anak + // Get the public methods of the child class $childMethods = $childReflection->getMethods(ReflectionMethod::IS_PUBLIC); - // Dapatkan metode dari kelas induk + // Get the public methods of the parent class $parentMethods = $parentClass->getMethods(ReflectionMethod::IS_PUBLIC); - // Ambil nama metode dari metode induk + // Get the method names of the parent class $parentMethodNames = array_map(function($method) { return $method->getName(); }, $parentMethods); - // Cek apakah metode ada di kelas anak dan juga di kelas induk + // Check if the method is present in both the child and parent class foreach ($childMethods as $method) { if ($method->getName() === $methodName && in_array($methodName, $parentMethodNames)) { return true; // Metode telah di-override @@ -855,7 +866,7 @@ private function isMethodOverridden($childClass, $methodName) { } } - return false; // Jika tidak ada kelas induk atau metode tidak di-override + return false;// No parent class or method is not overridden } } diff --git a/src/MagicObject.php b/src/MagicObject.php index b6951d0a..08b6733d 100644 --- a/src/MagicObject.php +++ b/src/MagicObject.php @@ -535,7 +535,7 @@ protected function readOnly($readonly) /** * Check if database is connected or not * - * @return bool + * @return bool Returns `true` if the database is connected, `false` otherwise. */ private function _databaseConnected() { @@ -650,11 +650,18 @@ public function removePropertyExcept($sourceData, $propertyNames) } /** - * Save to database. + * Save the current object to the database. * - * @param bool $includeNull If TRUE, all properties will be saved to the database, including null. If FALSE, only columns with non-null values will be saved. - * @return PDOStatement - * @throws NoDatabaseConnectionException|NoRecordFoundException|PDOException + * This method persists the current object to the database. If `$includeNull` is TRUE, + * all properties of the object, including those with null values, will be saved. + * If FALSE, only the properties with non-null values will be saved. + * + * @param bool $includeNull If TRUE, all properties, including null, will be saved. + * If FALSE, only non-null values will be saved. + * @return PDOStatement Returns a PDOStatement object for further database interaction. + * @throws NoDatabaseConnectionException If there is no active database connection. + * @throws NoRecordFoundException If no corresponding record is found. + * @throws PDOException If a database error occurs. */ public function save($includeNull = false) { @@ -670,11 +677,17 @@ public function save($includeNull = false) } /** - * Query to save data. + * Generate a query to save data to the database. + * + * This method prepares a query to persist the current object to the database. + * If `$includeNull` is TRUE, properties with null values will be included in the query. + * If FALSE, only properties with non-null values will be included. * - * @param bool $includeNull If TRUE, all properties will be saved to the database, including null. If FALSE, only columns with non-null values will be saved. - * @return PicoDatabaseQueryBuilder - * @throws NoDatabaseConnectionException|NoRecordFoundException + * @param bool $includeNull If TRUE, all properties, including null, will be saved. + * If FALSE, only non-null values will be included in the query. + * @return PicoDatabaseQueryBuilder Returns a PicoDatabaseQueryBuilder object for query construction. + * @throws NoDatabaseConnectionException If there is no active database connection. + * @throws NoRecordFoundException If no corresponding record is found. */ public function saveQuery($includeNull = false) { @@ -692,8 +705,13 @@ public function saveQuery($includeNull = false) /** * Select data from the database. * + * This method retrieves data from the database. If no data is found, a `NoRecordFoundException` will be thrown. + * The retrieved data is then loaded into the current instance for further use. + * * @return self Returns the current instance for method chaining. - * @throws NoDatabaseConnectionException|NoRecordFoundException|PDOException + * @throws NoDatabaseConnectionException If there is no active database connection. + * @throws NoRecordFoundException If no records are found in the database. + * @throws PDOException If a database error occurs. */ public function select() { @@ -717,8 +735,13 @@ public function select() /** * Select all data from the database. * + * This method retrieves all data from the database. If no data is found, a `NoRecordFoundException` will be thrown. + * The retrieved data is then loaded into the current instance for further use. + * * @return self Returns the current instance for method chaining. - * @throws NoDatabaseConnectionException|NoRecordFoundException|PDOException + * @throws NoDatabaseConnectionException If there is no active database connection. + * @throws NoRecordFoundException If no records are found in the database. + * @throws PDOException If a database error occurs. */ public function selectAll() { @@ -740,10 +763,14 @@ public function selectAll() } /** - * Query to select data. + * Generate a query to select data. + * + * This method prepares a query to select data from the database. The query can then be used to execute the retrieval + * of data manually if needed. * - * @return PicoDatabaseQueryBuilder - * @throws NoDatabaseConnectionException|NoRecordFoundException|PDOException + * @return PicoDatabaseQueryBuilder Returns a PicoDatabaseQueryBuilder object for building the select query. + * @throws NoDatabaseConnectionException If there is no active database connection. + * @throws PDOException If a database error occurs. */ public function selectQuery() { @@ -861,11 +888,15 @@ protected function executeNativeQuery() } /** - * Insert into the database. + * Insert data into the database. + * + * This method inserts the current object’s data into the database. If `$includeNull` is TRUE, + * properties with null values will also be included in the insert query. If FALSE, only properties with non-null values will be inserted. * - * @param bool $includeNull If TRUE, all properties will be saved to the database, including null. If FALSE, only columns with non-null values will be saved. - * @return PDOStatement - * @throws NoDatabaseConnectionException|PDOException + * @param bool $includeNull If TRUE, all properties, including null, will be inserted. If FALSE, only non-null values will be inserted. + * @return PDOStatement Returns a PDOStatement object for further database interaction. + * @throws NoDatabaseConnectionException If there is no active database connection. + * @throws PDOException If there is an error executing the insert query. */ public function insert($includeNull = false) { @@ -883,9 +914,12 @@ public function insert($includeNull = false) /** * Get the query for inserting data. * - * @param bool $includeNull If TRUE, all properties will be saved to the database, including null. If FALSE, only columns with non-null values will be saved. - * @return PicoDatabaseQueryBuilder - * @throws NoDatabaseConnectionException + * This method prepares a query to insert data into the database. It can be used to manually execute the query. + * If `$includeNull` is TRUE, properties with null values will be included in the query. If FALSE, only non-null properties will be inserted. + * + * @param bool $includeNull If TRUE, all properties, including null, will be included in the insert query. If FALSE, only non-null properties will be included. + * @return PicoDatabaseQueryBuilder Returns a PicoDatabaseQueryBuilder object for building the insert query. + * @throws NoDatabaseConnectionException If there is no active database connection. */ public function insertQuery($includeNull = false) { @@ -903,9 +937,13 @@ public function insertQuery($includeNull = false) /** * Update data in the database. * - * @param bool $includeNull If TRUE, all properties will be saved to the database, including null. If FALSE, only columns with non-null values will be saved. - * @return PDOStatement - * @throws NoDatabaseConnectionException|PDOException + * This method updates the current object's data in the database. If `$includeNull` is TRUE, + * properties with null values will be included in the update query. If FALSE, only properties with non-null values will be updated. + * + * @param bool $includeNull If TRUE, all properties, including null, will be updated. If FALSE, only non-null values will be updated. + * @return PDOStatement Returns a PDOStatement object for further database interaction. + * @throws NoDatabaseConnectionException If there is no active database connection. + * @throws PDOException If there is an error executing the update query. */ public function update($includeNull = false) { @@ -923,9 +961,12 @@ public function update($includeNull = false) /** * Get the query for updating data. * - * @param bool $includeNull If TRUE, all properties will be saved to the database, including null. If FALSE, only columns with non-null values will be saved. - * @return PicoDatabaseQueryBuilder - * @throws NoDatabaseConnectionException + * This method prepares a query to update data in the database. It can be used to manually execute the query. + * If `$includeNull` is TRUE, properties with null values will be included in the update query. If FALSE, only non-null properties will be updated. + * + * @param bool $includeNull If TRUE, all properties, including null, will be included in the update query. If FALSE, only non-null properties will be included. + * @return PicoDatabaseQueryBuilder Returns a PicoDatabaseQueryBuilder object for building the update query. + * @throws NoDatabaseConnectionException If there is no active database connection. */ public function updateQuery($includeNull = false) { @@ -943,8 +984,11 @@ public function updateQuery($includeNull = false) /** * Delete data from the database. * - * @return PDOStatement - * @throws NoDatabaseConnectionException|PDOException + * This method deletes data associated with the current object from the database. + * + * @return PDOStatement Returns a PDOStatement object for further database interaction. + * @throws NoDatabaseConnectionException If there is no active database connection. + * @throws PDOException If there is an error executing the delete query. */ public function delete() { @@ -962,8 +1006,10 @@ public function delete() /** * Get the query for deleting data. * - * @return PicoDatabaseQueryBuilder - * @throws NoDatabaseConnectionException + * This method prepares a query to delete data from the database. It can be used to manually execute the query. + * + * @return PicoDatabaseQueryBuilder Returns a PicoDatabaseQueryBuilder object for building the delete query. + * @throws NoDatabaseConnectionException If there is no active database connection. */ public function deleteQuery() { @@ -1073,10 +1119,15 @@ private function transactionalCommand($command) } /** - * Get MagicObject with WHERE specification. + * Get a MagicObject with a WHERE specification. * - * @param PicoSpecification $specification Specification - * @return PicoDatabasePersistenceExtended + * This method applies a WHERE condition to the database query using the provided specification. + * The specification is an instance of `PicoSpecification` which defines the filtering criteria. + * + * @param PicoSpecification $specification The specification to define the WHERE condition. + * @return PicoDatabasePersistenceExtended Returns an instance of `PicoDatabasePersistenceExtended` + * that applies the WHERE condition based on the provided specification. + * @throws NoDatabaseConnectionException If there is no active database connection. */ public function where($specification) { @@ -1092,11 +1143,14 @@ public function where($specification) } /** - * Modify null properties. + * Modify properties with null values. * - * @param string $propertyName Property name - * @param mixed $propertyValue Property value - * @return self + * This method tracks properties that are assigned a null value, storing them in a `_nullProperties` array. + * If a property is set to null, it is added to the `_nullProperties` array; if it's set to a non-null value, it is removed from the array. + * + * @param string $propertyName The name of the property to check and modify. + * @param mixed $propertyValue The value to be assigned to the property. + * @return self Returns the current instance for method chaining. */ private function modifyNullProperties($propertyName, $propertyValue) { @@ -1191,10 +1245,13 @@ public function prepend($propertyName, $propertyValue) } /** - * Remove the last element of an array property and return it. + * Remove and return the last element of an array property. * - * @param string $propertyName Property name - * @return mixed + * This method removes the last element from an array property and returns it. + * If the specified property is not an array or does not exist, null is returned. + * + * @param string $propertyName The name of the property (array) from which the last element will be removed. + * @return mixed The last element of the array, or null if the property is not an array or doesn't exist. */ public function pop($propertyName) { @@ -1207,10 +1264,13 @@ public function pop($propertyName) } /** - * Remove the first element of an array property and return it. + * Remove and return the first element of an array property. * - * @param string $propertyName Property name - * @return mixed + * This method removes the first element from an array property and returns it. + * If the specified property is not an array or does not exist, null is returned. + * + * @param string $propertyName The name of the property (array) from which the first element will be removed. + * @return mixed The first element of the array, or null if the property is not an array or doesn't exist. */ public function shift($propertyName) { @@ -1223,10 +1283,12 @@ public function shift($propertyName) } /** - * Get property value. + * Get the value of a property. * - * @param string $propertyName Property name - * @return mixed|null + * This method retrieves the value of the specified property. If the property does not exist, it returns null. + * + * @param string $propertyName The name of the property to retrieve. + * @return mixed|null The value of the property, or null if the property is not set. */ public function get($propertyName) { @@ -1235,11 +1297,13 @@ public function get($propertyName) } /** - * Get property value or a default value if not set. + * Get the value of a property or a default value if the property is not set. * - * @param string $propertyName Property name - * @param mixed|null $defaultValue Default value - * @return mixed|null + * This method retrieves the value of the specified property. If the property is not set, the provided default value is returned. + * + * @param string $propertyName The name of the property to retrieve. + * @param mixed|null $defaultValue The default value to return if the property is not set. + * @return mixed|null The value of the property, or the default value if the property is not set. */ public function getOrDefault($propertyName, $defaultValue = null) { @@ -1259,10 +1323,14 @@ public function __set($propertyName, $propertyValue) } /** - * Get property value (magic getter). + * Magic method to get the value of a property. * - * @param string $propertyName Property name - * @return mixed|null + * This method is automatically called when an undefined or inaccessible property is accessed. + * It checks if the property has been set (including null values) using the `__isset` method and + * retrieves its value via the `get` method if it exists. + * + * @param string $propertyName The name of the property to retrieve. + * @return mixed|null The value of the property if it is set, or null if the property is not set or accessible. */ public function __get($propertyName) { @@ -1274,10 +1342,13 @@ public function __get($propertyName) } /** - * Check if a property has been set or not (including null). + * Magic method to check if a property is set (including null). * - * @param string $propertyName Property name - * @return bool + * This method is automatically called when checking if an undefined or inaccessible property is set + * using `isset()`. It checks if the property exists and is set (even if its value is null). + * + * @param string $propertyName The name of the property to check. + * @return bool True if the property is set (including null), false otherwise. */ public function __isset($propertyName) { @@ -1286,9 +1357,12 @@ public function __isset($propertyName) } /** - * Unset property value. + * Magic method to unset a property. * - * @param string $propertyName Property name + * This method is automatically called when a property is unset using `unset()`. + * It unsets the specified property from the object. + * + * @param string $propertyName The name of the property to unset. * @return void */ public function __unset($propertyName) @@ -1298,12 +1372,15 @@ public function __unset($propertyName) } /** - * Copy values from another object. + * Copy values from another object to the current instance. * - * @param self|mixed $source Source data - * @param array|null $filter Filter - * @param bool $includeNull Flag to include null values - * @return self + * This method copies property values from the provided source object to the current instance. + * Optionally, a filter can be applied to specify which properties to copy, and whether null values should be included. + * + * @param self|mixed $source The source object or data from which values will be copied. If a non-object is provided, this may result in unexpected behavior. + * @param array|null $filter An optional array of property names to filter which properties are copied. If null, all properties are copied. + * @param bool $includeNull Flag indicating whether to include properties with null values. If false, properties with null values will be excluded from the copy. + * @return self Returns the current instance for method chaining after copying the values. */ public function copyValueFrom($source, $filter = null, $includeNull = false) { @@ -1346,9 +1423,13 @@ private function removeValue($propertyName, $skipModifyNullProperties = false) } /** - * Get table information + * Retrieve table information for the current instance. + * + * This method retrieves the table information (e.g., schema, columns) associated with the current + * object. It lazily loads the table information, meaning it will be fetched only once and cached + * for future calls to improve performance. * - * @return PicoTableInfo + * @return PicoTableInfo Returns an instance of the `PicoTableInfo` class containing the table schema and other related metadata. */ public function tableInfo() { diff --git a/src/SecretObject.php b/src/SecretObject.php index c07baa9f..bcbf4246 100644 --- a/src/SecretObject.php +++ b/src/SecretObject.php @@ -133,7 +133,6 @@ class SecretObject extends stdClass // NOSONAR */ private $_secureFunction = null; // NOSONAR - /** * Constructor for initializing the object with data. *