super-powers/src/91004529-94a9-4590-b842-e7c6b624ecf5/code.power

81 lines
2.0 KiB
Plaintext
Raw Permalink Normal View History

2023-07-04 06:26:08 +00:00
/**
* Check if have an object with a length
*
* @input object The object to check
*
* @returns bool true on success
*
* @since 3.0.9
*/
public static function check($object)
{
if (is_object($object))
{
return count((array) $object) > 0;
}
return false;
}
/**
2024-08-21 21:09:13 +00:00
* Checks if two objects are equal by comparing their properties and values.
2023-07-04 06:26:08 +00:00
*
2024-08-21 21:09:13 +00:00
* This method converts both input objects to
* associative arrays, sorts the arrays by keys,
* and compares these sorted arrays.
2023-07-04 06:26:08 +00:00
*
2024-08-21 21:09:13 +00:00
* If the arrays are identical, the objects are considered equal.
2023-07-04 06:26:08 +00:00
*
2024-08-21 21:09:13 +00:00
* @param object|null $obj1 The first object to compare.
* @param object|null $obj2 The second object to compare.
*
* @return bool True if the objects are equal, false otherwise.
* @since 5.0.2
2023-07-04 06:26:08 +00:00
*/
public static function equal(?object $obj1, ?object $obj2): bool
{
// if any is null we return false as that means there is a none object
// we are not comparing null but objects
// but we allow null as some objects while
// not instantiate are still null
if (is_null($obj1) || is_null($obj2))
{
return false;
}
2024-08-21 21:09:13 +00:00
// Convert both objects to associative arrays
$array1 = json_decode(json_encode($obj1), true);
$array2 = json_decode(json_encode($obj2), true);
2023-07-04 06:26:08 +00:00
2024-08-21 21:09:13 +00:00
// Sort the arrays by keys
self::recursiveKsort($array1);
self::recursiveKsort($array2);
2023-07-04 06:26:08 +00:00
2024-08-21 21:09:13 +00:00
// Compare the sorted arrays
return $array1 === $array2;
2023-07-04 06:26:08 +00:00
}
2024-08-21 21:09:13 +00:00
/**
* Recursively sorts an associative array by keys.
*
* This method will sort an associative array by its keys at all levels.
*
* @param array &$array The array to sort.
*
* @return void
* @since 5.0.2
*/
protected static function recursiveKsort(array &$array): void
{
// Sort the array by its keys
ksort($array);
// Recursively sort nested arrays
foreach ($array as &$value)
{
if (is_array($value))
{
self::recursiveKsort($value);
}
}
}