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

feat(entries): add ability to work with entries as with Arrays Collection

This commit is contained in:
Awilum
2020-11-27 16:22:49 +03:00
parent bebd27099a
commit b4b0fde384
4 changed files with 26 additions and 22 deletions

View File

@@ -97,6 +97,10 @@
* **entries** add new events: `onEntryHas`, `onEntryInitialized`, `onEntriesInitialized` ([#467](https://github.com/flextype/flextype/issues/467))
* **helpers** add new helper `find()` for files and directories searching instead of `find_filter()`
* **helpers** add new helper `filter()` for data collection filtering instead of `arrays_filter()`
### Bug Fixes
* **entries** fix issue with delete() method ([#465](https://github.com/flextype/flextype/issues/465))

View File

@@ -12,7 +12,7 @@ namespace Flextype;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Http\Response;
use function array_replace_recursive;
use function arrays_filter;
use function filter;
use function count;
use function is_array;

View File

@@ -12,7 +12,7 @@ namespace Flextype\Foundation\Entries;
use Atomastic\Arrays\Arrays;
use function array_merge;
use function arrays_filter;
use function filter;
use function count;
use function find_filter;
use function ltrim;
@@ -62,11 +62,11 @@ class Entries
* @param bool $collection Set `true` if collection of entries need to be fetched.
* @param array $filter Select items in collection by given conditions.
*
* @return array|bool|int
* @return Arrays
*
* @access public
*/
public function fetch(string $id, bool $collection = false, array $filter = [])
public function fetch(string $id, bool $collection = false, array $filter = []): Arrays
{
if ($collection) {
return $this->fetchCollection($id, $filter);
@@ -80,11 +80,11 @@ class Entries
*
* @param string $id Unique identifier of the entry(entries).
*
* @return array The entry array data.
* @return Arrays
*
* @access public
*/
public function fetchSingle(string $id): array
public function fetchSingle(string $id): Arrays
{
// Store data
$this->storage['fetch_single']['id'] = $id;
@@ -104,7 +104,7 @@ class Entries
flextype('emitter')->emit('onEntryAfterCacheInitialized');
// Return entry from cache
return $this->storage['fetch_single']['data'];
return arrays($this->storage['fetch_single']['data']);
}
// Try to get current requested entry from filesystem
@@ -115,7 +115,7 @@ class Entries
// Try to get requested entry from the filesystem
$entry_file_content = filesystem()->file($entry_file)->get();
if ($entry_file_content === false) {
return [];
return arrays([]);
}
// Decode entry file content
@@ -134,11 +134,11 @@ class Entries
}
// Return entry data
return $this->storage['fetch_single']['data'];
return arrays($this->storage['fetch_single']['data']);
}
// Return empty array if entry is not founded
return [];
return arrays([]);
}
/**
@@ -147,11 +147,11 @@ class Entries
* @param string $id Unique identifier of the entry(entries).
* @param array $filter Select items in collection by given conditions.
*
* @return array|bool|int
* @return Arrays
*
* @access public
*/
public function fetchCollection(string $id, array $filter = [])
public function fetchCollection(string $id, array $filter = []): Arrays
{
// Store data
$this->storage['fetch_collection']['id'] = $this->getDirectoryLocation($id);
@@ -161,7 +161,7 @@ class Entries
flextype('emitter')->emit('onEntriesInitialized');
// Apply find_filter
$entries_list = find_filter($this->storage['fetch_collection']['id'], $filter);
$entries_list = find($this->storage['fetch_collection']['id'], $filter);
// If entries founded in the entries folder
// We are checking... Whether the requested entry is an a true entry.
@@ -177,15 +177,15 @@ class Entries
$this->storage['fetch_collection']['data'][$_id] = $this->fetchSingle($_id);
}
// Apply arrays_filter
$this->storage['fetch_collection']['data'] = arrays_filter($this->storage['fetch_collection']['data'], $filter);
// Apply filter
$this->storage['fetch_collection']['data'] = filter($this->storage['fetch_collection']['data'], $filter);
// Run event: onEntriesAfterInitialized
flextype('emitter')->emit('onEntriesAfterInitialized');
}
// Return entries array
return $this->storage['fetch_collection']['data'];
return arrays($this->storage['fetch_collection']['data']);
}
/**

View File

@@ -34,7 +34,7 @@ test('test fetch() method', function () {
$this->assertTrue(count($fetch) > 0);
// 2
$this->assertEquals([], flextype('entries')->fetch('bar'));
$this->assertEquals([], flextype('entries')->fetch('bar')->toArray());
// 3
flextype('entries')->create('zed', ['title' => 'Zed']);
@@ -45,7 +45,7 @@ test('test fetch() method', function () {
flextype('entries')->create('foo', []);
flextype('entries')->create('foo/bar', []);
flextype('entries')->create('foo/baz', ['foo' => ['bar' => 'zed']]);
$fetch = flextype('entries')->fetch('foo', true);
$fetch = flextype('entries')->fetch('foo', true)->toArray();
$this->assertTrue(count($fetch) > 0);
});
@@ -57,23 +57,23 @@ test('test fetchSingle() method', function () {
$this->assertTrue(count($fetch) > 0);
// 2
$this->assertEquals([], flextype('entries')->fetchSingle('bar'));
$this->assertEquals([], flextype('entries')->fetchSingle('bar')->toArray());
// 3
flextype('entries')->create('zed', ['title' => 'Zed']);
$fetch = flextype('entries')->fetchSingle('zed');
$fetch = flextype('entries')->fetchSingle('zed')->toArray();
$this->assertEquals('Zed', $fetch['title']);
// 4
flextype('entries')->setStorage('fetch_single.id', 'wrong-entry');
$this->assertEquals([], flextype('entries')->fetchSingle('wrong-entry'));
$this->assertEquals([], flextype('entries')->fetchSingle('wrong-entry')->toArray());
});
test('test fetchCollection() method', function () {
flextype('entries')->create('foo', []);
flextype('entries')->create('foo/bar', []);
flextype('entries')->create('foo/baz', []);
$fetch = flextype('entries')->fetchCollection('foo');
$fetch = flextype('entries')->fetchCollection('foo')->toArray();
$this->assertTrue(count($fetch) > 0);
});