mirror of
https://github.com/shlinkio/shlink.git
synced 2025-05-02 05:22:16 +03:00
Remove last references to functional-php
This commit is contained in:
parent
bff4bd12ae
commit
1854cc2f19
22 changed files with 147 additions and 114 deletions
module/Core/functions
74
module/Core/functions/array-utils.php
Normal file
74
module/Core/functions/array-utils.php
Normal file
|
@ -0,0 +1,74 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\ArrayUtils;
|
||||
|
||||
use function array_filter;
|
||||
use function array_reduce;
|
||||
use function in_array;
|
||||
|
||||
use const ARRAY_FILTER_USE_KEY;
|
||||
|
||||
function contains(mixed $value, array $array): bool
|
||||
{
|
||||
return in_array($value, $array, strict: true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array[] $multiArray
|
||||
* @return array
|
||||
*/
|
||||
function flatten(array $multiArray): array
|
||||
{
|
||||
return array_reduce(
|
||||
$multiArray,
|
||||
static fn (array $carry, array $value) => [...$carry, ...$value],
|
||||
initial: [],
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a callback returns true for at least one item in a collection.
|
||||
* @param callable(mixed $value, mixed $key): bool $callback
|
||||
*/
|
||||
function some(iterable $collection, callable $callback): bool
|
||||
{
|
||||
foreach ($collection as $key => $value) {
|
||||
if ($callback($value, $key)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a callback returns true for all item in a collection.
|
||||
* @param callable(mixed $value, string|number $key): bool $callback
|
||||
*/
|
||||
function every(iterable $collection, callable $callback): bool
|
||||
{
|
||||
foreach ($collection as $key => $value) {
|
||||
if (! $callback($value, $key)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array containing only those entries in the array whose key is in the supplied keys.
|
||||
*/
|
||||
function select_keys(array $array, array $keys): array
|
||||
{
|
||||
return array_filter(
|
||||
$array,
|
||||
static fn (string $key) => contains(
|
||||
$key,
|
||||
$keys,
|
||||
),
|
||||
ARRAY_FILTER_USE_KEY,
|
||||
);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue