1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-08 06:06:45 +02:00

feat(helpers): rename helper filter to filterCollection

This commit is contained in:
Awilum
2021-08-14 17:25:48 +03:00
parent 806d8e159c
commit f652a8fc80
3 changed files with 31 additions and 31 deletions

View File

@@ -149,7 +149,7 @@ class Entries
if (cache()->has($entryCacheID)) {
// Fetch entry from cache and Apply filter for fetch data
$this->registry()->set('fetch.data', filter(cache()->get($entryCacheID),
$this->registry()->set('fetch.data', filterCollection(cache()->get($entryCacheID),
$this->registry()->get('fetch.options.filter', [])));
// Run event
@@ -180,7 +180,7 @@ class Entries
emitter()->emit('on' . strings($this->options['directory'])->capitalize()->toString() . 'FetchSingleHasResult');
// Apply filter for fetch data
$this->registry()->set('fetch.data', filter($this->registry()->get('fetch.data'), $this->registry()->get('fetch.options.filter', [])));
$this->registry()->set('fetch.data', filterCollection($this->registry()->get('fetch.data'), $this->registry()->get('fetch.options.filter', [])));
// Set cache state
$cache = $this->registry()->get('fetch.data.cache.enabled',
@@ -248,7 +248,7 @@ class Entries
// Process filter `only` for collection
// after process we need to unset $options['filter']['only']
// to avoid it's running inside filter() helper.
// to avoid it's running inside filterCollection() helper.
if (isset($options['filter']['only'])) {
$data = [];
foreach ($this->registry()->get('fetch.data') as $key => $value) {
@@ -260,7 +260,7 @@ class Entries
// Process filter `except` for collection
// after process we need to unset $options['filter']['except']
// to avoid it's running inside filter() helper.
// to avoid it's running inside filterCollection() helper.
if (isset($options['filter']['except'])) {
$data = [];
foreach ($this->registry()->get('fetch.data') as $key => $value) {
@@ -271,7 +271,7 @@ class Entries
}
// Apply filter for fetch data
$this->registry()->set('fetch.data', filter($this->registry()->get('fetch.data'), isset($options['filter']) ? $options['filter'] : []));
$this->registry()->set('fetch.data', filterCollection($this->registry()->get('fetch.data'), isset($options['filter']) ? $options['filter'] : []));
} else {
// Run event:
emitter()->emit('on' . strings($this->options['directory'])->capitalize()->toString() . 'FetchCollectionNoResult');

View File

@@ -192,16 +192,16 @@ if (! function_exists('find')) {
}
}
if (! function_exists('filter')) {
if (! function_exists('filterCollection')) {
/**
* Create a collection from the given value and filter it.
* Filter collection.
*
* @param mixed $items Items.
* @param array $options Options array.
*
* @return array
*/
function filter($items = [], array $options = []): array
function filterCollection($items = [], array $options = []): array
{
$collection = arrays($items);

View File

@@ -4,79 +4,79 @@ declare(strict_types=1);
use Atomastic\Arrays\Arrays;
test('test filter() method', function () {
$this->assertEquals([], filter());
$this->assertEquals([], filter([]));
$this->assertEquals([], filter([], []));
$this->assertEquals(['foo', 'bar'], filter(['foo', 'bar'], []));
test('test filterCollection() method', function () {
$this->assertEquals([], filterCollection());
$this->assertEquals([], filterCollection([]));
$this->assertEquals([], filterCollection([], []));
$this->assertEquals(['foo', 'bar'], filterCollection(['foo', 'bar'], []));
$data = ['home' => ['title' => 'Home'],
'about' => ['title' => 'About'],
'blog' => ['title' => 'Blog']];
// return: first
$this->assertEquals(['title' => 'Home'], filter($data, ['return' => 'first']));
$this->assertEquals(['title' => 'Home'], filterCollection($data, ['return' => 'first']));
// return: last
$this->assertEquals(['title' => 'Blog'], filter($data, ['return' => 'last']));
$this->assertEquals(['title' => 'Blog'], filterCollection($data, ['return' => 'last']));
// return: next
$this->assertEquals(['title' => 'About'], filter($data, ['return' => 'next']));
$this->assertEquals(['title' => 'About'], filterCollection($data, ['return' => 'next']));
// return: random
$random = filter($data, ['return' => 'random']);
$random = filterCollection($data, ['return' => 'random']);
$this->assertContains($random, $data);
$random = filter($data, ['return' => 'random', 'random' => 0]);
$random = filterCollection($data, ['return' => 'random', 'random' => 0]);
$this->assertIsArray($random);
$this->assertCount(0, $random);
$random = filter($data, ['return' => 'random', 'random' => 1]);
$random = filterCollection($data, ['return' => 'random', 'random' => 1]);
$this->assertIsArray($random);
$this->assertCount(1, $random);
$this->assertContains(filter($data, ['return' => 'first']), $data);
$this->assertContains(filterCollection($data, ['return' => 'first']), $data);
$random = filter($data, ['return' => 'random', 'random' => 2]);
$random = filterCollection($data, ['return' => 'random', 'random' => 2]);
$this->assertIsArray($random);
$this->assertCount(2, $random);
$this->assertContains(filter($random, ['return' => 'first']), $data);
$this->assertContains(filter($random, ['return' => 'last']), $data);
$this->assertContains(filterCollection($random, ['return' => 'first']), $data);
$this->assertContains(filterCollection($random, ['return' => 'last']), $data);
// return: shuffle
$this->assertTrue(
is_array(filter($data, ['return' => 'shuffle'])) &&
is_array(filter($data, ['return' => 'shuffle']))
is_array(filterCollection($data, ['return' => 'shuffle'])) &&
is_array(filterCollection($data, ['return' => 'shuffle']))
);
// param: offset and return: all
$this->assertEquals(['about' => ['title' => 'About'],
'blog' => ['title' => 'Blog']], filter($data, ['return' => 'all', 'offset' => 1]));
'blog' => ['title' => 'Blog']], filterCollection($data, ['return' => 'all', 'offset' => 1]));
// param: limit and return: all
$this->assertEquals(['home' => ['title' => 'Home']], filter($data, ['return' => 'all', 'limit' => 1]));
$this->assertEquals(['home' => ['title' => 'Home']], filterCollection($data, ['return' => 'all', 'limit' => 1]));
// param: sort_by and return: all
$this->assertEquals(['about' => ['title' => 'About'],
'blog' => ['title' => 'Blog'],
'home' => ['title' => 'Home']],
filter($data, ['return' => 'all',
filterCollection($data, ['return' => 'all',
'sort_by' => ['key' => 'title',
'direction' => 'ASC']]));
$this->assertEquals(['home' => ['title' => 'Home'],
'blog' => ['title' => 'Blog'],
'about' => ['title' => 'About']],
filter($data, ['return' => 'all',
filterCollection($data, ['return' => 'all',
'sort_by' => ['key' => 'title',
'direction' => 'DESC']]));
$this->assertEquals(['Home' => [0 => ['title' => 'Home']],
'About' => [0 => ['title' => 'About']],
'Blog' => [0 => ['title' => 'Blog']]],
filter($data, ['return' => 'all',
filterCollection($data, ['return' => 'all',
'group_by' => 'title']));
// param: where and return: all
$this->assertEquals(['about' => ['title' => 'About']],
filter($data, ['return' => 'all',
filterCollection($data, ['return' => 'all',
'where' => [['key' => 'title', 'operator' => '=', 'value' => 'About']]]));
});