From f652a8fc808b487051f35b69cd7259232307088a Mon Sep 17 00:00:00 2001 From: Awilum Date: Sat, 14 Aug 2021 17:25:48 +0300 Subject: [PATCH] feat(helpers): rename helper `filter` to `filterCollection` --- src/flextype/core/Entries.php | 10 ++--- src/flextype/helpers/helpers.php | 6 +-- tests/src/flextype/core/FilterHelperTest.php | 46 ++++++++++---------- 3 files changed, 31 insertions(+), 31 deletions(-) diff --git a/src/flextype/core/Entries.php b/src/flextype/core/Entries.php index 34bc6fd1..d5427cfd 100755 --- a/src/flextype/core/Entries.php +++ b/src/flextype/core/Entries.php @@ -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'); diff --git a/src/flextype/helpers/helpers.php b/src/flextype/helpers/helpers.php index c1d9e730..1717b194 100644 --- a/src/flextype/helpers/helpers.php +++ b/src/flextype/helpers/helpers.php @@ -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); diff --git a/tests/src/flextype/core/FilterHelperTest.php b/tests/src/flextype/core/FilterHelperTest.php index 91302390..8b770839 100644 --- a/tests/src/flextype/core/FilterHelperTest.php +++ b/tests/src/flextype/core/FilterHelperTest.php @@ -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']]])); });