Add and autoload the array helper

This commit is contained in:
Alex Tselegidis 2022-10-16 18:03:55 +03:00
parent d02c71a0e2
commit 98a0e07484
2 changed files with 79 additions and 0 deletions

View file

@ -66,6 +66,7 @@ $autoload['libraries'] = ['database', 'session'];
*/
$autoload['helper'] = [
'array',
'asset',
'config',
'debug',

View file

@ -0,0 +1,78 @@
<?php defined('BASEPATH') or exit('No direct script access allowed');
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
* @link http://easyappointments.org
* @since v1.4.0
* ---------------------------------------------------------------------------- */
if ( ! function_exists('is_assoc'))
{
/**
* Check if an array is an associative array.
*
* @param array $array
*
* @return bool
*/
function is_assoc(array $array): bool
{
if (empty($array))
{
return FALSE;
}
return array_keys($array) !== range(0, count($array) - 1);
}
}
if ( ! function_exists('array_find'))
{
/**
* Find the first array element based on the provided function.
*
* @param array $array
* @param callable $callback
*
* @return mixed
*/
function array_find(array $array, callable $callback): mixed
{
if (empty($array))
{
return NULL;
}
if ( ! is_callable($callback))
{
throw new InvalidArgumentException('No filter function provided.');
}
return array_filter($array, $callback)[0] ?? NULL;
}
}
if ( ! function_exists('array_fields'))
{
/**
* Keep only the provided fields of an array.
*
* @param array $array
* @param array $fields
*
* @return array
*/
function array_fields(array $array, array $fields): array
{
return array_filter($array, function ($field) use ($fields) {
return in_array($field, $fields);
}, ARRAY_FILTER_USE_KEY);
}
}