1
0
Fork 0
mirror of https://github.com/archtechx/helpers.git synced 2025-12-14 04:34:02 +00:00
This commit is contained in:
Samuel Štancl 2021-10-28 19:37:04 +02:00
parent 2945c59e92
commit 0d63a5b79c
12 changed files with 97 additions and 109 deletions

32
src/helpers.php Normal file
View file

@ -0,0 +1,32 @@
<?php
if (! function_exists('uses_trait')) {
/** Check if a class or an object uses the specified trait. */
function uses_trait(object|string $class, string $trait): bool
{
return in_array($trait, class_uses_recursive($class), true);
}
}
if (! function_exists('is_simple_array')) {
/** Checks if a variadic array only has one element that's an array.
* @example [['foo', 'bar']] is a simple array
* @example [['foo'], ['bar']] is not a simple array
*/
function is_simple_array(array $variadicArray): bool
{
return count($variadicArray) === 1 && isset($variadicArray[0]) && is_array($variadicArray[0]);
}
}
if (! function_exists('variadic_array')) {
/** Converts [[a]] into [a] and [a, b] into [a, b] */
function variadic_array(array $items): array
{
if (is_simple_array($items)) {
$items = $items[0];
}
return $items;
}
}