1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-10 15:14:20 +02:00

refactor(core): code formating and refactoring

This commit is contained in:
Awilum
2020-12-02 13:53:09 +03:00
parent a535de88d3
commit b965a50486
7 changed files with 54 additions and 42 deletions

View File

@@ -12,52 +12,58 @@ if (! function_exists('filter')) {
* Create a collection from the given value and filter it.
*
* @param mixed $items Items.
* @param array $filter Filters params array.
* @param array $params Params array.
*
* @return array|bool|int
*/
function filter($items = [], array $filter = [])
function filter($items = [], array $params = [])
{
$collection = arrays($items);
! isset($filter['return']) AND $filter['return'] = 'all';
! isset($params['return']) and $params['return'] = 'all';
if (isset($filter['where'])) {
if (is_array($filter['where'])) {
foreach ($filter['where'] as $key => $value) {
if (isset($value['key']) &&
isset($value['operator']) &&
isset($value['value'])) {
$collection->where($value['key'], $value['operator'], $value['value']);
if (isset($params['where'])) {
if (is_array($params['where'])) {
foreach ($params['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($filter['group_by'])) {
$collection->groupBy($filter['group_by']);
if (isset($params['group_by'])) {
$collection->groupBy($params['group_by']);
}
if (isset($filter['slice_offset']) && isset($filter['slice_offset'])) {
$collection->slice(isset($filter['slice_offset']) ? (int) $filter['slice_offset'] : 0,
isset($filter['slice_limit']) ? (int) $filter['slice_limit'] : 0);
if (isset($params['slice_offset']) && isset($params['slice_offset'])) {
$collection->slice(
isset($params['slice_offset']) ? (int) $params['slice_offset'] : 0,
isset($params['slice_limit']) ? (int) $params['slice_limit'] : 0
);
}
if (isset($filter['sort_by'])) {
if (isset($filter['sort_by']['key']) && isset($filter['sort_by']['direction'])) {
$collection->sortBySubKey($filter['sort_by']['key'], $filter['sort_by']['direction']);
if (isset($params['sort_by'])) {
if (isset($params['sort_by']['key']) && isset($params['sort_by']['direction'])) {
$collection->sortBySubKey($params['sort_by']['key'], $params['sort_by']['direction']);
}
}
if (isset($filter['offset'])) {
$collection->offset(isset($filter['offset']) ? (int) $filter['offset'] : 0);
if (isset($params['offset'])) {
$collection->offset(isset($params['offset']) ? (int) $params['offset'] : 0);
}
if (isset($filter['limit'])) {
$collection->limit(isset($filter['limit']) ? (int) $filter['limit'] : 0);
if (isset($params['limit'])) {
$collection->limit(isset($params['limit']) ? (int) $params['limit'] : 0);
}
switch ($filter['return']) {
switch ($params['return']) {
case 'first':
$result = $collection->first();
break;
@@ -68,7 +74,7 @@ if (! function_exists('filter')) {
$result = $collection->next();
break;
case 'random':
$result = $collection->random(isset($filter['random']) ? (int) $filter['random'] : null);
$result = $collection->random(isset($params['random']) ? (int) $params['random'] : null);
break;
case 'exists':
$result = $collection->count() > 0;
@@ -86,6 +92,5 @@ if (! function_exists('filter')) {
}
return $result;
}
}

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 $filter Filters params array.
* @param array $params Parameters array.
* @param string $search_in Search in 'files' or 'directories'. Default is 'files'.
*
* @return Symfony\Component\Finder<Finder>
*/
function find(string $path = '', array $filter = [], string $search_in = 'files'): Finder
function find(string $path = '', array $params = [], string $search_in = 'files'): Finder
{
$find = filesystem()->find()->in($path);
isset($filter['depth']) and $find->depth($filter['depth']) or $find->depth(1);
isset($filter['date']) and $find->date($filter['date']);
isset($filter['size']) and $find->size($filter['size']);
isset($filter['exclude']) and $find->exclude($filter['exclude']);
isset($filter['contains']) and $find->contains($filter['contains']);
isset($filter['not_contains']) and $find->notContains($filter['not_contains']);
isset($filter['filter']) and $find->filter($filter['filter']);
isset($filter['sort']) and $find->sort($filter['sort']);
isset($filter['path']) and $find->path($filter['path']);
isset($filter['sort_by']) && $filter['sort_by'] === 'atime' and $find->sortByAccessedTime();
isset($filter['sort_by']) && $filter['sort_by'] === 'mtime' and $find->sortByModifiedTime();
isset($filter['sort_by']) && $filter['sort_by'] === 'ctime' and $find->sortByChangedTime();
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();
return $search_in === 'directories' ? $find->directories() : $find->files();
}

View File

@@ -11,6 +11,8 @@ namespace Flextype\Support\Parsers;
use Atomastic\Strings\Strings;
use function flextype;
class Markdown
{
/**

View File

@@ -11,6 +11,8 @@ namespace Flextype\Support\Parsers;
use Atomastic\Strings\Strings;
use function flextype;
class Shortcode
{
/**

View File

@@ -9,11 +9,12 @@ declare(strict_types=1);
namespace Flextype\Support\Serializers;
use Atomastic\Arrays\Arrays;
use Atomastic\Strings\Strings;
use function array_slice;
use function arrays;
use function count;
use function flextype;
use function implode;
use function ltrim;
use function preg_replace;
@@ -68,8 +69,8 @@ class Frontmatter
{
if (isset($input['content'])) {
$content = $input['content'];
$input = arrays($input)->delete('content')->toArray();
$matter = flextype('yaml')->encode($input);
$input = arrays($input)->delete('content')->toArray();
$matter = flextype('yaml')->encode($input);
} else {
$content = '';
$matter = flextype('yaml')->encode($input);

View File

@@ -13,6 +13,7 @@ use Atomastic\Strings\Strings;
use RuntimeException;
use function defined;
use function flextype;
use function json_decode;
use function json_encode;
use function json_last_error;

View File

@@ -16,6 +16,7 @@ use Symfony\Component\Yaml\Exception\ParseException as SymfonyYamlParseException
use Symfony\Component\Yaml\Yaml as SymfonyYaml;
use function error_reporting;
use function flextype;
use function function_exists;
use function ini_get;
use function ini_set;