1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-11 15:44:49 +02:00

feat(helpers): use $options instead of $params

This commit is contained in:
Awilum
2020-12-03 13:58:36 +03:00
parent cca8606bb4
commit a9f1c97f32
2 changed files with 35 additions and 35 deletions

View File

@@ -11,20 +11,20 @@ if (! function_exists('filter')) {
/**
* Create a collection from the given value and filter it.
*
* @param mixed $items Items.
* @param array $params Params array.
* @param mixed $items Items.
* @param array $options Options array.
*
* @return array|bool|int
*/
function filter($items = [], array $params = [])
function filter($items = [], array $options = [])
{
$collection = arrays($items);
! isset($params['return']) and $params['return'] = 'all';
! isset($options['return']) and $options['return'] = 'all';
if (isset($params['where'])) {
if (is_array($params['where'])) {
foreach ($params['where'] as $key => $value) {
if (isset($options['where'])) {
if (is_array($options['where'])) {
foreach ($options['where'] as $key => $value) {
if (
! isset($value['key']) ||
! isset($value['operator']) ||
@@ -38,32 +38,32 @@ if (! function_exists('filter')) {
}
}
if (isset($params['group_by'])) {
$collection->groupBy($params['group_by']);
if (isset($options['group_by'])) {
$collection->groupBy($options['group_by']);
}
if (isset($params['slice_offset']) && isset($params['slice_offset'])) {
if (isset($options['slice_offset']) && isset($options['slice_offset'])) {
$collection->slice(
isset($params['slice_offset']) ? (int) $params['slice_offset'] : 0,
isset($params['slice_limit']) ? (int) $params['slice_limit'] : 0
isset($options['slice_offset']) ? (int) $options['slice_offset'] : 0,
isset($options['slice_limit']) ? (int) $options['slice_limit'] : 0
);
}
if (isset($params['sort_by'])) {
if (isset($params['sort_by']['key']) && isset($params['sort_by']['direction'])) {
$collection->sortBy($params['sort_by']['key'], $params['sort_by']['direction']);
if (isset($options['sort_by'])) {
if (isset($options['sort_by']['key']) && isset($options['sort_by']['direction'])) {
$collection->sortBy($options['sort_by']['key'], $options['sort_by']['direction']);
}
}
if (isset($params['offset'])) {
$collection->offset(isset($params['offset']) ? (int) $params['offset'] : 0);
if (isset($options['offset'])) {
$collection->offset(isset($options['offset']) ? (int) $options['offset'] : 0);
}
if (isset($params['limit'])) {
$collection->limit(isset($params['limit']) ? (int) $params['limit'] : 0);
if (isset($options['limit'])) {
$collection->limit(isset($options['limit']) ? (int) $options['limit'] : 0);
}
switch ($params['return']) {
switch ($options['return']) {
case 'first':
$result = $collection->first();
break;
@@ -74,7 +74,7 @@ if (! function_exists('filter')) {
$result = $collection->next();
break;
case 'random':
$result = $collection->random(isset($params['random']) ? (int) $params['random'] : null);
$result = $collection->random(isset($options['random']) ? (int) $options['random'] : null);
break;
case 'exists':
$result = $collection->count() > 0;

View File

@@ -14,27 +14,27 @@ if (! function_exists('find')) {
* Create a Finder instance with predefined filter params or without them.
*
* @param string $path Path.
* @param array $params Parameters array.
* @param array $options Options array.
* @param string $search_in Search in 'files' or 'directories'. Default is 'files'.
*
* @return Symfony\Component\Finder<Finder>
*/
function find(string $path = '', array $params = [], string $search_in = 'files'): Finder
function find(string $path = '', array $options = [], string $search_in = 'files'): Finder
{
$find = filesystem()->find()->in($path);
isset($params['depth']) and $find->depth($params['depth']) or $find->depth(1);
isset($params['date']) and $find->date($params['date']);
isset($params['size']) and $find->size($params['size']);
isset($params['exclude']) and $find->exclude($params['exclude']);
isset($params['contains']) and $find->contains($params['contains']);
isset($params['not_contains']) and $find->notContains($params['not_contains']);
isset($params['filter']) and $find->filter($params['filter']);
isset($params['sort']) and $find->sort($params['sort']);
isset($params['path']) and $find->path($params['path']);
isset($params['sort_by']) && $params['sort_by'] === 'atime' and $find->sortByAccessedTime();
isset($params['sort_by']) && $params['sort_by'] === 'mtime' and $find->sortByModifiedTime();
isset($params['sort_by']) && $params['sort_by'] === 'ctime' and $find->sortByChangedTime();
isset($options['depth']) and $find->depth($options['depth']) or $find->depth(1);
isset($options['date']) and $find->date($options['date']);
isset($options['size']) and $find->size($options['size']);
isset($options['exclude']) and $find->exclude($options['exclude']);
isset($options['contains']) and $find->contains($options['contains']);
isset($options['not_contains']) and $find->notContains($options['not_contains']);
isset($options['filter']) and $find->filter($options['filter']);
isset($options['sort']) and $find->sort($options['sort']);
isset($options['path']) and $find->path($options['path']);
isset($options['sort_by']) && $options['sort_by'] === 'atime' and $find->sortByAccessedTime();
isset($options['sort_by']) && $options['sort_by'] === 'mtime' and $find->sortByModifiedTime();
isset($options['sort_by']) && $options['sort_by'] === 'ctime' and $find->sortByChangedTime();
return $search_in === 'directories' ? $find->directories() : $find->files();
}