diff --git a/src/flextype/Foundation/Helpers/ActionsHelper.php b/src/flextype/Foundation/Helpers/ActionsHelper.php deleted file mode 100644 index 94ba31a434..0000000000 --- a/src/flextype/Foundation/Helpers/ActionsHelper.php +++ /dev/null @@ -1,20 +0,0 @@ - $value) { - if ( - ! isset($value['key']) || - ! isset($value['operator']) || - ! isset($value['value']) - ) { - continue; - } - - $collection->where($value['key'], $value['operator'], $value['value']); - } - } - } - - if (isset($options['group_by'])) { - $collection->groupBy($options['group_by']); - } - - 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($options['offset'])) { - $collection->offset(isset($options['offset']) ? (int) $options['offset'] : 0); - } - - if (isset($options['limit'])) { - $collection->limit(isset($options['limit']) ? (int) $options['limit'] : 0); - } - - switch ($options['return']) { - case 'first': - $result = $collection->first(); - break; - case 'last': - $result = $collection->last(); - break; - case 'next': - $result = $collection->next(); - break; - case 'random': - $result = $collection->random(isset($options['random']) ? (int) $options['random'] : null); - break; - case 'shuffle': - $result = $collection->shuffle()->toArray(); - break; - case 'all': - default: - $result = $collection->all(); - break; - } - - return $result; - } -} diff --git a/src/flextype/Support/Helpers/FindHelper.php b/src/flextype/Support/Helpers/FindHelper.php deleted file mode 100644 index e401c75ceb..0000000000 --- a/src/flextype/Support/Helpers/FindHelper.php +++ /dev/null @@ -1,41 +0,0 @@ -find()->in($path); - - 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 $searchIn === 'directories' ? $find->directories() : $find->files(); - } -} diff --git a/src/flextype/helpers.php b/src/flextype/helpers.php new file mode 100644 index 0000000000..1ef368005b --- /dev/null +++ b/src/flextype/helpers.php @@ -0,0 +1,218 @@ +app(); + } +} + +if (! function_exists('container')) { + /** + * Get Flextype container. + */ + function container() { + return flextype()->container(); + } +} + +if (! function_exists('emitter')) { + /** + * Get Flextype emitter service. + */ + function emitter() { + return flextype()->container()->get('emitter'); + } +} + +if (! function_exists('cache')) { + /** + * Get Flextype cache service. + */ + function cache() { + return flextype()->container()->get('cache'); + } +} + +if (! function_exists('entries')) { + /** + * Get Flextype entries service. + */ + function entries() { + return flextype()->container()->get('entries'); + } +} + +if (! function_exists('parsers')) { + /** + * Get Flextype parsers service. + */ + function parsers() { + return flextype()->container()->get('parsers'); + } +} + +if (! function_exists('serializers')) { + /** + * Get Flextype serializers service. + */ + function serializers() { + return flextype()->container()->get('serializers'); + } +} + +if (! function_exists('logger')) { + /** + * Get Flextype logger service. + */ + function logger() { + return flextype()->container()->get('logger'); + } +} + +if (! function_exists('session')) { + /** + * Get Flextype session service. + */ + function session() { + return flextype()->container()->get('session'); + } +} + +if (! function_exists('csrf')) { + /** + * Get Flextype csrf service. + */ + function csrf() { + return flextype()->container()->get('csrf'); + } +} + +if (! function_exists('find')) { + /** + * Create a Finder instance with predefined filter params or without them. + * + * @param string $path Path. + * @param array $options Options array. + * @param string $searchIn Search in 'files' or 'directories'. Default is 'files'. + * + * @return Finder + */ + function find(string $path = '', array $options = [], string $searchIn = 'files'): Finder + { + $find = filesystem()->find()->in($path); + + 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 $searchIn === 'directories' ? $find->directories() : $find->files(); + } +} + +if (! function_exists('filter')) { + /** + * Create a collection from the given value and filter it. + * + * @param mixed $items Items. + * @param array $options Options array. + * + * @return array + */ + function filter($items = [], array $options = []): array + { + $collection = arrays($items); + + ! isset($options['return']) and $options['return'] = 'all'; + + if (isset($options['where'])) { + if (is_array($options['where'])) { + foreach ($options['where'] as $key => $value) { + if ( + ! isset($value['key']) || + ! isset($value['operator']) || + ! isset($value['value']) + ) { + continue; + } + + $collection->where($value['key'], $value['operator'], $value['value']); + } + } + } + + if (isset($options['group_by'])) { + $collection->groupBy($options['group_by']); + } + + 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($options['offset'])) { + $collection->offset(isset($options['offset']) ? (int) $options['offset'] : 0); + } + + if (isset($options['limit'])) { + $collection->limit(isset($options['limit']) ? (int) $options['limit'] : 0); + } + + switch ($options['return']) { + case 'first': + $result = $collection->first(); + break; + case 'last': + $result = $collection->last(); + break; + case 'next': + $result = $collection->next(); + break; + case 'random': + $result = $collection->random(isset($options['random']) ? (int) $options['random'] : null); + break; + case 'shuffle': + $result = $collection->shuffle()->toArray(); + break; + case 'all': + default: + $result = $collection->all(); + break; + } + + return $result; + } +}