mirror of
https://github.com/archtechx/helpers.git
synced 2025-12-14 04:34:02 +00:00
add code
This commit is contained in:
parent
2945c59e92
commit
0d63a5b79c
12 changed files with 97 additions and 109 deletions
32
src/helpers.php
Normal file
32
src/helpers.php
Normal 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;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue