Skip to content

Commit

Permalink
Update api id and add minor feature
Browse files Browse the repository at this point in the history
  • Loading branch information
kekefreedog committed Jan 29, 2025
1 parent 4c3c8f6 commit 57a5eb2
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Controller/ApiV2Id.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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");
Expand Down
50 changes: 50 additions & 0 deletions src/Driver/Model/Mariadb.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 50 additions & 0 deletions src/Front/Library/Utility/Arrays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
******************************************************
*/
Expand Down
18 changes: 18 additions & 0 deletions src/Front/Library/Utility/Objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
/**
* Dependances
*/
import Arrays from "./Arrays";

/**
* Arrays
Expand Down Expand Up @@ -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);

}

}
Expand Down
26 changes: 26 additions & 0 deletions src/Library/Array/Arrays.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
19 changes: 19 additions & 0 deletions src/Library/Form/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
27 changes: 27 additions & 0 deletions tests/Library/Array/ArraysTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}

}

0 comments on commit 57a5eb2

Please sign in to comment.