mirror of
https://github.com/flextype/flextype.git
synced 2025-08-08 14:16:46 +02:00
feat(helpers): reorganize helpers #199
- remove helpers folder - add all core helpers in the `/src/flextype/helpers.php` - new helpers: app, container, entries, cache, session, emitter, logger, parsers, serializers, csrf. - logic for `flextype` helper is changed, this helper returns only current Flextype Instance
This commit is contained in:
@@ -1,20 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Flextype (https://flextype.org)
|
||||
* Founded by Sergey Romanenko and maintained by Flextype Community.
|
||||
*/
|
||||
|
||||
use Flextype\Foundation\Actions;
|
||||
|
||||
if (! function_exists('actions')) {
|
||||
/**
|
||||
* Get the available Flextype instance.
|
||||
*/
|
||||
function actions()
|
||||
{
|
||||
return Actions::getInstance();
|
||||
}
|
||||
}
|
@@ -1,20 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Flextype (https://flextype.org)
|
||||
* Founded by Sergey Romanenko and maintained by Flextype Community.
|
||||
*/
|
||||
|
||||
use Flextype\Foundation\Flextype;
|
||||
|
||||
if (! function_exists('flextype')) {
|
||||
/**
|
||||
* Get the available Flextype instance.
|
||||
*/
|
||||
function flextype($container = null)
|
||||
{
|
||||
return Flextype::getInstance($container);
|
||||
}
|
||||
}
|
@@ -1,83 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Flextype (https://flextype.org)
|
||||
* Founded by Sergey Romanenko and maintained by Flextype Community.
|
||||
*/
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
@@ -1,41 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Flextype (https://flextype.org)
|
||||
* Founded by Sergey Romanenko and maintained by Flextype Community.
|
||||
*/
|
||||
|
||||
use Symfony\Component\Finder\Finder as Finder;
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
218
src/flextype/helpers.php
Normal file
218
src/flextype/helpers.php
Normal file
@@ -0,0 +1,218 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Flextype (https://flextype.org)
|
||||
* Founded by Sergey Romanenko and maintained by Flextype Community.
|
||||
*/
|
||||
|
||||
use Flextype\Foundation\Flextype;
|
||||
use Symfony\Component\Finder\Finder as Finder;
|
||||
|
||||
|
||||
if (! function_exists('flextype')) {
|
||||
/**
|
||||
* Get the available Flextype instance.
|
||||
*/
|
||||
function flextype($container = null)
|
||||
{
|
||||
return Flextype::getInstance($container);
|
||||
}
|
||||
}
|
||||
|
||||
if (! function_exists('app')) {
|
||||
/**
|
||||
* Get Flextype app.
|
||||
*/
|
||||
function app() {
|
||||
return flextype()->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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user