mirror of
https://github.com/flextype/flextype.git
synced 2025-08-09 14:46:53 +02:00
feat(helpers): add new arrays_filter() instead of collect_filter()
This commit is contained in:
@@ -73,7 +73,8 @@
|
||||
],
|
||||
"files": [
|
||||
"src/flextype/Support/Helpers/FinderHelper.php",
|
||||
"src/flextype/Support/Helpers/CollectionHelper.php",
|
||||
"src/flextype/Support/Helpers/FinderFilterHelper.php",
|
||||
"src/flextype/Support/Helpers/ArraysFilterHelper.php",
|
||||
"src/flextype/Foundation/Helpers/FlextypeHelper.php"
|
||||
]
|
||||
},
|
||||
|
@@ -12,7 +12,7 @@ namespace Flextype;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Slim\Http\Response;
|
||||
use function array_replace_recursive;
|
||||
use function collect_filter;
|
||||
use function arrays_filter;
|
||||
use function count;
|
||||
use function is_array;
|
||||
|
||||
|
@@ -12,7 +12,7 @@ namespace Flextype\Foundation\Entries;
|
||||
use Atomastic\Arrays\Arrays;
|
||||
|
||||
use function array_merge;
|
||||
use function collect_filter;
|
||||
use function arrays_filter;
|
||||
use function count;
|
||||
use function find_filter;
|
||||
use function ltrim;
|
||||
@@ -177,7 +177,7 @@ class Entries
|
||||
}
|
||||
|
||||
// Apply collection filter
|
||||
$this->storage['fetch_collection']['data'] = collect_filter($this->storage['fetch_collection']['data'], $filter);
|
||||
$this->storage['fetch_collection']['data'] = arrays_filter($this->storage['fetch_collection']['data'], $filter);
|
||||
|
||||
// Run event: onEntriesAfterInitialized
|
||||
flextype('emitter')->emit('onEntriesAfterInitialized');
|
||||
|
@@ -9,19 +9,7 @@ declare(strict_types=1);
|
||||
|
||||
use Atomastic\Arrays\Arrays;
|
||||
|
||||
if (! function_exists('collect')) {
|
||||
/**
|
||||
* Create a collection from the given value.
|
||||
*
|
||||
* @param mixed $items Items
|
||||
*/
|
||||
function collect($items = []): Arrays
|
||||
{
|
||||
return new Arrays($items);
|
||||
}
|
||||
}
|
||||
|
||||
if (! function_exists('collect_filter')) {
|
||||
if (! function_exists('arrays_filter')) {
|
||||
/**
|
||||
* Create a collection from the given value and filter it.
|
||||
*
|
||||
@@ -30,9 +18,9 @@ if (! function_exists('collect_filter')) {
|
||||
*
|
||||
* @return array|bool|int
|
||||
*/
|
||||
function collect_filter($items = [], array $filter = [])
|
||||
function arrays_filter($items = [], array $filter = [])
|
||||
{
|
||||
$collection = new Arrays($items);
|
||||
$collection = arrays($items);
|
||||
|
||||
if (isset($filter['where'])) {
|
||||
if (is_array($filter['where'])) {
|
35
src/flextype/Support/Helpers/FinderFilterHelper.php
Normal file
35
src/flextype/Support/Helpers/FinderFilterHelper.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Flextype (http://flextype.org)
|
||||
* Founded by Sergey Romanenko and maintained by Flextype Community.
|
||||
*/
|
||||
|
||||
use Symfony\Component\Finder\Finder;
|
||||
|
||||
if (! function_exists('find_filter')) {
|
||||
/**
|
||||
* Create a Finder filter instance.
|
||||
*/
|
||||
function find_filter(string $path, array $filter = [], $search_in = 'files')
|
||||
{
|
||||
$find = 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();
|
||||
|
||||
return $search_in === 'files' ? $find->files() : $find->directories();
|
||||
}
|
||||
}
|
@@ -18,28 +18,3 @@ if (! function_exists('find')) {
|
||||
return new Finder();
|
||||
}
|
||||
}
|
||||
|
||||
if (! function_exists('find_filter')) {
|
||||
/**
|
||||
* Create a Finder filter instance.
|
||||
*/
|
||||
function find_filter(string $path, array $filter = [], $search_in = 'files')
|
||||
{
|
||||
$find = 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();
|
||||
|
||||
return $search_in === 'files' ? $find->files() : $find->directories();
|
||||
}
|
||||
}
|
||||
|
91
tests/Support/Helpers/ArraysFilterHelperTest.php
Normal file
91
tests/Support/Helpers/ArraysFilterHelperTest.php
Normal file
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Atomastic\Arrays\Arrays;
|
||||
|
||||
test('test arrays_filter() method', function () {
|
||||
$this->assertEquals([], arrays_filter());
|
||||
$this->assertEquals([], arrays_filter([]));
|
||||
$this->assertEquals([], arrays_filter([], []));
|
||||
$this->assertEquals(['foo', 'bar'], arrays_filter(['foo', 'bar'], []));
|
||||
|
||||
$data = ['home' => ['title' => 'Home'],
|
||||
'about' => ['title' => 'About'],
|
||||
'blog' => ['title' => 'Blog']];
|
||||
|
||||
// return: first
|
||||
$this->assertEquals(['title' => 'Home'], arrays_filter($data, ['return' => 'first']));
|
||||
|
||||
// return: last
|
||||
$this->assertEquals(['title' => 'Blog'], arrays_filter($data, ['return' => 'last']));
|
||||
|
||||
// return: next
|
||||
$this->assertEquals(['title' => 'About'], arrays_filter($data, ['return' => 'next']));
|
||||
|
||||
// return: random
|
||||
$random = arrays_filter($data, ['return' => 'random']);
|
||||
$this->assertContains($random, $data);
|
||||
|
||||
$random = arrays_filter($data, ['return' => 'random', 'random' => 0]);
|
||||
$this->assertIsArray($random);
|
||||
$this->assertCount(0, $random);
|
||||
|
||||
$random = arrays_filter($data, ['return' => 'random', 'random' => 1]);
|
||||
$this->assertIsArray($random);
|
||||
$this->assertCount(1, $random);
|
||||
$this->assertContains(arrays_filter($data, ['return' => 'first']), $data);
|
||||
|
||||
$random = arrays_filter($data, ['return' => 'random', 'random' => 2]);
|
||||
$this->assertIsArray($random);
|
||||
$this->assertCount(2, $random);
|
||||
$this->assertContains(arrays_filter($random, ['return' => 'first']), $data);
|
||||
$this->assertContains(arrays_filter($random, ['return' => 'last']), $data);
|
||||
|
||||
// return: exists
|
||||
$this->assertTrue(arrays_filter($data, ['return' => 'exists']));
|
||||
|
||||
// return: shuffle
|
||||
$this->assertNotEquals(
|
||||
arrays_filter($data, ['return' => 'shuffle']),
|
||||
arrays_filter($data, ['return' => 'shuffle'])
|
||||
);
|
||||
|
||||
// return: count
|
||||
$this->assertEquals(3, arrays_filter($data, ['return' => 'count']));
|
||||
|
||||
// param: limit and return: all
|
||||
$this->assertEquals(['home' => ['title' => 'Home']], arrays_filter($data, ['return' => 'all', 'limit' => 1]));
|
||||
|
||||
// param: offset and return: all
|
||||
$this->assertEquals(['about' => ['title' => 'About'],
|
||||
'blog' => ['title' => 'Blog']], arrays_filter($data, ['return' => 'all', 'offset' => 1]));
|
||||
|
||||
// param: slice_offset slice_limit and return: all
|
||||
$this->assertEquals(['about' => ['title' => 'About']], arrays_filter($data, ['return' => 'all', 'slice_offset' => 1, 'slice_limit' => 1]));
|
||||
|
||||
// param: sort_by and return: all
|
||||
$this->assertEquals(['about' => ['title' => 'About'],
|
||||
'blog' => ['title' => 'Blog'],
|
||||
'home' => ['title' => 'Home']],
|
||||
arrays_filter($data, ['return' => 'all',
|
||||
'sort_by' => ['key' => 'title',
|
||||
'direction' => 'ASC']]));
|
||||
|
||||
$this->assertEquals(['home' => ['title' => 'Home'],
|
||||
'blog' => ['title' => 'Blog'],
|
||||
'about' => ['title' => 'About']],
|
||||
arrays_filter($data, ['return' => 'all',
|
||||
'sort_by' => ['key' => 'title',
|
||||
'direction' => 'DESC']]));
|
||||
|
||||
$this->assertEquals(['Home' => [0 => ['title' => 'Home']],
|
||||
'About' => [0 => ['title' => 'About']],
|
||||
'Blog' => [0 => ['title' => 'Blog']]],
|
||||
arrays_filter($data, ['return' => 'all',
|
||||
'group_by' => 'title']));
|
||||
// param: where and return: all
|
||||
$this->assertEquals(['about' => ['title' => 'About']],
|
||||
arrays_filter($data, ['return' => 'all',
|
||||
'where' => [['key' => 'title', 'operator' => '=', 'value' => 'About']]]));
|
||||
});
|
@@ -1,95 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Atomastic\Arrays\Arrays;
|
||||
|
||||
test('test collect() method', function () {
|
||||
$this->assertInstanceOf(Arrays::class, collect());
|
||||
});
|
||||
|
||||
test('test collect_filter() method', function () {
|
||||
$this->assertEquals([], collect_filter());
|
||||
$this->assertEquals([], collect_filter([]));
|
||||
$this->assertEquals([], collect_filter([], []));
|
||||
$this->assertEquals(['foo', 'bar'], collect_filter(['foo', 'bar'], []));
|
||||
|
||||
$data = ['home' => ['title' => 'Home'],
|
||||
'about' => ['title' => 'About'],
|
||||
'blog' => ['title' => 'Blog']];
|
||||
|
||||
// return: first
|
||||
$this->assertEquals(['title' => 'Home'], collect_filter($data, ['return' => 'first']));
|
||||
|
||||
// return: last
|
||||
$this->assertEquals(['title' => 'Blog'], collect_filter($data, ['return' => 'last']));
|
||||
|
||||
// return: next
|
||||
$this->assertEquals(['title' => 'About'], collect_filter($data, ['return' => 'next']));
|
||||
|
||||
// return: random
|
||||
$random = collect_filter($data, ['return' => 'random']);
|
||||
$this->assertContains($random, $data);
|
||||
|
||||
$random = collect_filter($data, ['return' => 'random', 'random' => 0]);
|
||||
$this->assertIsArray($random);
|
||||
$this->assertCount(0, $random);
|
||||
|
||||
$random = collect_filter($data, ['return' => 'random', 'random' => 1]);
|
||||
$this->assertIsArray($random);
|
||||
$this->assertCount(1, $random);
|
||||
$this->assertContains(collect_filter($data, ['return' => 'first']), $data);
|
||||
|
||||
$random = collect_filter($data, ['return' => 'random', 'random' => 2]);
|
||||
$this->assertIsArray($random);
|
||||
$this->assertCount(2, $random);
|
||||
$this->assertContains(collect_filter($random, ['return' => 'first']), $data);
|
||||
$this->assertContains(collect_filter($random, ['return' => 'last']), $data);
|
||||
|
||||
// return: exists
|
||||
$this->assertTrue(collect_filter($data, ['return' => 'exists']));
|
||||
|
||||
// return: shuffle
|
||||
$this->assertNotEquals(
|
||||
collect_filter($data, ['return' => 'shuffle']),
|
||||
collect_filter($data, ['return' => 'shuffle'])
|
||||
);
|
||||
|
||||
// return: count
|
||||
$this->assertEquals(3, collect_filter($data, ['return' => 'count']));
|
||||
|
||||
// param: limit and return: all
|
||||
$this->assertEquals(['home' => ['title' => 'Home']], collect_filter($data, ['return' => 'all', 'limit' => 1]));
|
||||
|
||||
// param: offset and return: all
|
||||
$this->assertEquals(['about' => ['title' => 'About'],
|
||||
'blog' => ['title' => 'Blog']], collect_filter($data, ['return' => 'all', 'offset' => 1]));
|
||||
|
||||
// param: slice_offset slice_limit and return: all
|
||||
$this->assertEquals(['about' => ['title' => 'About']], collect_filter($data, ['return' => 'all', 'slice_offset' => 1, 'slice_limit' => 1]));
|
||||
|
||||
// param: sort_by and return: all
|
||||
$this->assertEquals(['about' => ['title' => 'About'],
|
||||
'blog' => ['title' => 'Blog'],
|
||||
'home' => ['title' => 'Home']],
|
||||
collect_filter($data, ['return' => 'all',
|
||||
'sort_by' => ['key' => 'title',
|
||||
'direction' => 'ASC']]));
|
||||
|
||||
$this->assertEquals(['home' => ['title' => 'Home'],
|
||||
'blog' => ['title' => 'Blog'],
|
||||
'about' => ['title' => 'About']],
|
||||
collect_filter($data, ['return' => 'all',
|
||||
'sort_by' => ['key' => 'title',
|
||||
'direction' => 'DESC']]));
|
||||
|
||||
$this->assertEquals(['Home' => [0 => ['title' => 'Home']],
|
||||
'About' => [0 => ['title' => 'About']],
|
||||
'Blog' => [0 => ['title' => 'Blog']]],
|
||||
collect_filter($data, ['return' => 'all',
|
||||
'group_by' => 'title']));
|
||||
// param: where and return: all
|
||||
$this->assertEquals(['about' => ['title' => 'About']],
|
||||
collect_filter($data, ['return' => 'all',
|
||||
'where' => [['key' => 'title', 'operator' => '=', 'value' => 'About']]]));
|
||||
});
|
18
tests/Support/Helpers/FinderHelperTest.php
Normal file
18
tests/Support/Helpers/FinderHelperTest.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Symfony\Component\Finder\Finder;
|
||||
|
||||
beforeEach(function() {
|
||||
filesystem()->directory(PATH['project'] . '/entries')->create(0755, true);
|
||||
flextype('entries')->create('foo', []);
|
||||
});
|
||||
|
||||
afterEach(function (): void {
|
||||
filesystem()->directory(PATH['project'] . '/entries')->delete();
|
||||
});
|
||||
|
||||
test('test find() method', function () {
|
||||
$this->assertInstanceOf(Finder::class, find());
|
||||
});
|
Reference in New Issue
Block a user