diff --git a/src/Controller/ApiV2Id.php b/src/Controller/ApiV2Id.php index c2806ef..af8908d 100644 --- a/src/Controller/ApiV2Id.php +++ b/src/Controller/ApiV2Id.php @@ -21,6 +21,7 @@ use CrazyPHP\Core\Controller; use CrazyPHP\Model\Context; use CrazyPHP\Core\Model; +use CrazyPHP\Library\Form\Query; /** * Api V2 By Id @@ -49,8 +50,11 @@ public static function get():void { # Get id $id = self::getParametersUrl("id"); + # Filters parameters + $filtersParameters = Query::getForId(); + # Declare content - $content = $model->readById((string) $id); + $content = $model->readById((string) $id, ...$filtersParameters); # Get last modified date of model config $lastModified = FileConfig::getLastModified("Model"); diff --git a/src/Driver/Model/Mariadb.php b/src/Driver/Model/Mariadb.php index f5b1d79..075325a 100644 --- a/src/Driver/Model/Mariadb.php +++ b/src/Driver/Model/Mariadb.php @@ -513,6 +513,56 @@ public function run():array { "id" => " = ".$this->id ] ]); + + # Load reference + if($this->arguments["loadReference"]) + + # Process + $result = $this->_loadReferenceProcess($result); + + # check arguments + if($this->arguments["sqlPrefix"]) + + # Check result + if(!empty($result)) + + # Iteration result + foreach($result as &$row){ + + # Get id + $id = $row["id"] ?? null; + + # Process value + $row = $this->_sqlPrefixProcess($row); + + # Check id + if($id) + + # Push id in row + $row["id"] = $id; + + # Push table + $row["entity"] = $this->arguments["table"]; + + } + + # check arguments + if($this->arguments["unflatten"]) + + # Check result + if(!empty($result)) + + # Iteration result + foreach($result as &$row) + + # Set result + $row = Arrays::unflatten($row, "_"); + + # check arguments + if($this->arguments["pageStateProcess"]) + + # Process value + $result = $this->_pageStateProcess($result); }else # Insert to mariadb Check schema diff --git a/src/Front/Library/Utility/Arrays.ts b/src/Front/Library/Utility/Arrays.ts index bf9268f..c4581eb 100644 --- a/src/Front/Library/Utility/Arrays.ts +++ b/src/Front/Library/Utility/Arrays.ts @@ -156,6 +156,56 @@ export default class Arrays { } + /** + * Equals + * + * Check if two array are equals + * + * @param value1 + * @param value2 + * @returns {boolean} + */ + public static equal = (value1:any, value2:any):boolean => { + + // Direct match (including primitives) + if(value1 === value2) return true; + + // If either is not an object or is null, they must be strictly equal + if(typeof value1 !== "object" || typeof value2 !== "object" || value1 === null || value2 === null) return false; + + // If are arrays + if(Array.isArray(value1) && Array.isArray(value2)){ + + // Different lengths + if (value1.length !== value2.length) return false; + + // Recursively compare elements + return value1.every((el, i) => Arrays.equal(el, value2[i])); + + } + + // If object + if(Object.prototype.toString.call(value1) === "[object Object]" && Object.prototype.toString.call(value2) === "[object Object]"){ + + // Get key 1 + const keys1 = Object.keys(value1); + + // Get key 2 + const keys2 = Object.keys(value2); + + // Different number of keys + if (keys1.length !== keys2.length) return false; + + // Recursively compare object properties + return keys1.every((key) => Arrays.equal(value1[key], value2[key])); + + } + + // If one is an array and the other isn't, or different types + return false; + + } + /** Private static methods ****************************************************** */ diff --git a/src/Front/Library/Utility/Objects.ts b/src/Front/Library/Utility/Objects.ts index 7c2c1c0..6af8763 100644 --- a/src/Front/Library/Utility/Objects.ts +++ b/src/Front/Library/Utility/Objects.ts @@ -11,6 +11,7 @@ /** * Dependances */ +import Arrays from "./Arrays"; /** * Arrays @@ -349,6 +350,23 @@ export default class Objects { return current[key]; }, obj); + + } + + /** + * Equals + * + * Check if two array are equals + * + * @param value1 + * @param value2 + * @returns {boolean} + */ + public static equal = (value1:any, value2:any):boolean => { + + // Return eqal from array + return Arrays.equal(value1, value2); + } } diff --git a/src/Library/Array/Arrays.php b/src/Library/Array/Arrays.php index 61e2567..9452c71 100644 --- a/src/Library/Array/Arrays.php +++ b/src/Library/Array/Arrays.php @@ -151,6 +151,32 @@ function ($var) use ($keyValue, $key) { } + /** + * Filter array by nested key value multidimensional + * + * @param array $array Array to process + * @param string $key Nested key using separator (default ".") + * @param mixed $keyValue Value to filter + * @param string $separator Separator for nested keys (default ".") + * @return array Filtered array + */ + public static function filterByKeyMD(array $array = [], string $key = "", mixed $keyValue = "", string $separator = "."): array { + return array_filter($array, function ($item) use ($key, $keyValue, $separator) { + $keys = explode($separator, $key); + $value = self::getNestedValue($item, $keys); + return $value === $keyValue; + }); + } + private static function getNestedValue(array $array, array $keys) { + foreach ($keys as $key) { + if (!is_array($array) || !array_key_exists($key, $array)) { + return null; + } + $array = $array[$key]; + } + return $array; + } + /** * Remove By Key * diff --git a/src/Library/Form/Query.php b/src/Library/Form/Query.php index 08bb2f2..1441d46 100644 --- a/src/Library/Form/Query.php +++ b/src/Library/Form/Query.php @@ -71,6 +71,25 @@ public static function getForFilters():array { } + /** + * Get For Id + * + * Get query parameters for controller id + * + * @return array + */ + public static function getForId():array { + + # Set result + $result = [ + 0 => $_GET["option"] ?? $_GET["options"] ?? null, # Option + ]; + + # Return result + return $result; + + } + /** * Get For Filters From Array * diff --git a/tests/Library/Array/ArraysTest.php b/tests/Library/Array/ArraysTest.php index 6a2fd52..7bd28cb 100644 --- a/tests/Library/Array/ArraysTest.php +++ b/tests/Library/Array/ArraysTest.php @@ -632,4 +632,31 @@ public function testAddPrefixToKeysWithException():void { } + /** + * Test filter by key multidimensional + * + * @return void + */ + public function testFilterByKeyMD():void { + + # Set input + $input = [ + ["user" => ["id" => 1, "name" => "Alice"]], + ["user" => ["id" => 2, "name" => "Bob"]], + ["user" => ["id" => 3, "name" => "Charlie"]], + ]; + + # Set ouput + $output = [ + 1 => ["user" => ["id" => 2, "name" => "Bob"]], + ]; + + # Filter where "user.id" is 2 + $result = Arrays::filterByKeyMD($input, "user.id", 2); + + # Check + $this->assertEquals($output, $result); + + } + } \ No newline at end of file