mirror of
https://github.com/flextype/flextype.git
synced 2025-08-09 22:56:46 +02:00
feat(helpers): new helpers find() and filter() instead of find_filter() and arrays_filter()
This commit is contained in:
@@ -70,9 +70,8 @@
|
||||
"src/flextype"
|
||||
],
|
||||
"files": [
|
||||
"src/flextype/Support/Helpers/FinderHelper.php",
|
||||
"src/flextype/Support/Helpers/FinderFilterHelper.php",
|
||||
"src/flextype/Support/Helpers/ArraysFilterHelper.php",
|
||||
"src/flextype/Support/Helpers/FindHelper.php",
|
||||
"src/flextype/Support/Helpers/FilterHelper.php",
|
||||
"src/flextype/Foundation/Helpers/FlextypeHelper.php"
|
||||
]
|
||||
},
|
||||
|
@@ -7,18 +7,16 @@ declare(strict_types=1);
|
||||
* Founded by Sergey Romanenko and maintained by Flextype Community.
|
||||
*/
|
||||
|
||||
use Atomastic\Arrays\Arrays;
|
||||
|
||||
if (! function_exists('arrays_filter')) {
|
||||
if (! function_exists('filter')) {
|
||||
/**
|
||||
* Create a collection from the given value and filter it.
|
||||
*
|
||||
* @param mixed $items Items.
|
||||
* @param array $filter Filters array.
|
||||
* @param array $filter Filters params array.
|
||||
*
|
||||
* @return array|bool|int
|
||||
*/
|
||||
function arrays_filter($items = [], array $filter = [])
|
||||
function filter($items = [], array $filter = [])
|
||||
{
|
||||
$collection = arrays($items);
|
||||
|
@@ -9,13 +9,19 @@ declare(strict_types=1);
|
||||
|
||||
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')
|
||||
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 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
|
||||
{
|
||||
$find = find()->in($path);
|
||||
$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']);
|
||||
@@ -30,6 +36,6 @@ if (! function_exists('find_filter')) {
|
||||
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();
|
||||
return $search_in === 'directories' ? $find->directories() : $find->files();
|
||||
}
|
||||
}
|
@@ -1,20 +0,0 @@
|
||||
<?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')) {
|
||||
/**
|
||||
* Create a Finder instance.
|
||||
*/
|
||||
function find(): Finder
|
||||
{
|
||||
return new Finder();
|
||||
}
|
||||
}
|
@@ -4,88 +4,88 @@ 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'], []));
|
||||
test('test filter() method', function () {
|
||||
$this->assertEquals([], filter());
|
||||
$this->assertEquals([], filter([]));
|
||||
$this->assertEquals([], filter([], []));
|
||||
$this->assertEquals(['foo', 'bar'], filter(['foo', 'bar'], []));
|
||||
|
||||
$data = ['home' => ['title' => 'Home'],
|
||||
'about' => ['title' => 'About'],
|
||||
'blog' => ['title' => 'Blog']];
|
||||
|
||||
// return: first
|
||||
$this->assertEquals(['title' => 'Home'], arrays_filter($data, ['return' => 'first']));
|
||||
$this->assertEquals(['title' => 'Home'], filter($data, ['return' => 'first']));
|
||||
|
||||
// return: last
|
||||
$this->assertEquals(['title' => 'Blog'], arrays_filter($data, ['return' => 'last']));
|
||||
$this->assertEquals(['title' => 'Blog'], filter($data, ['return' => 'last']));
|
||||
|
||||
// return: next
|
||||
$this->assertEquals(['title' => 'About'], arrays_filter($data, ['return' => 'next']));
|
||||
$this->assertEquals(['title' => 'About'], filter($data, ['return' => 'next']));
|
||||
|
||||
// return: random
|
||||
$random = arrays_filter($data, ['return' => 'random']);
|
||||
$random = filter($data, ['return' => 'random']);
|
||||
$this->assertContains($random, $data);
|
||||
|
||||
$random = arrays_filter($data, ['return' => 'random', 'random' => 0]);
|
||||
$random = filter($data, ['return' => 'random', 'random' => 0]);
|
||||
$this->assertIsArray($random);
|
||||
$this->assertCount(0, $random);
|
||||
|
||||
$random = arrays_filter($data, ['return' => 'random', 'random' => 1]);
|
||||
$random = filter($data, ['return' => 'random', 'random' => 1]);
|
||||
$this->assertIsArray($random);
|
||||
$this->assertCount(1, $random);
|
||||
$this->assertContains(arrays_filter($data, ['return' => 'first']), $data);
|
||||
$this->assertContains(filter($data, ['return' => 'first']), $data);
|
||||
|
||||
$random = arrays_filter($data, ['return' => 'random', 'random' => 2]);
|
||||
$random = 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);
|
||||
$this->assertContains(filter($random, ['return' => 'first']), $data);
|
||||
$this->assertContains(filter($random, ['return' => 'last']), $data);
|
||||
|
||||
// return: exists
|
||||
$this->assertTrue(arrays_filter($data, ['return' => 'exists']));
|
||||
$this->assertTrue(filter($data, ['return' => 'exists']));
|
||||
|
||||
// return: shuffle
|
||||
$this->assertTrue(
|
||||
is_array(arrays_filter($data, ['return' => 'shuffle'])) &&
|
||||
is_array(arrays_filter($data, ['return' => 'shuffle']))
|
||||
is_array(filter($data, ['return' => 'shuffle'])) &&
|
||||
is_array(filter($data, ['return' => 'shuffle']))
|
||||
);
|
||||
|
||||
// return: count
|
||||
$this->assertEquals(3, arrays_filter($data, ['return' => 'count']));
|
||||
$this->assertEquals(3, filter($data, ['return' => 'count']));
|
||||
|
||||
// param: limit and return: all
|
||||
$this->assertEquals(['home' => ['title' => 'Home']], arrays_filter($data, ['return' => 'all', 'limit' => 1]));
|
||||
$this->assertEquals(['home' => ['title' => 'Home']], 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]));
|
||||
'blog' => ['title' => 'Blog']], 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]));
|
||||
$this->assertEquals(['about' => ['title' => 'About']], 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',
|
||||
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',
|
||||
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',
|
||||
filter($data, ['return' => 'all',
|
||||
'group_by' => 'title']));
|
||||
// param: where and return: all
|
||||
$this->assertEquals(['about' => ['title' => 'About']],
|
||||
arrays_filter($data, ['return' => 'all',
|
||||
filter($data, ['return' => 'all',
|
||||
'where' => [['key' => 'title', 'operator' => '=', 'value' => 'About']]]));
|
||||
});
|
@@ -14,5 +14,8 @@ afterEach(function (): void {
|
||||
});
|
||||
|
||||
test('test find() method', function () {
|
||||
$this->assertInstanceOf(Finder::class, find());
|
||||
$this->assertTrue(find(PATH['project'] . '/entries')->hasResults());
|
||||
$this->assertTrue(find(PATH['project'] . '/entries', [])->hasResults());
|
||||
$this->assertTrue(find(PATH['project'] . '/entries', [], 'files')->hasResults());
|
||||
$this->assertTrue(find(PATH['project'], [], 'directories')->hasResults());
|
||||
});
|
@@ -1,21 +0,0 @@
|
||||
<?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_filter() method', function () {
|
||||
$this->assertTrue(find_filter(PATH['project'] . '/entries')->hasResults());
|
||||
$this->assertTrue(find_filter(PATH['project'] . '/entries', [])->hasResults());
|
||||
$this->assertTrue(find_filter(PATH['project'] . '/entries', [], 'files')->hasResults());
|
||||
$this->assertTrue(find_filter(PATH['project'], [], 'directories')->hasResults());
|
||||
});
|
Reference in New Issue
Block a user