diff --git a/src/flextype/Foundation/Entries/Entries.php b/src/flextype/Foundation/Entries/Entries.php index 875a404f..c47e7862 100755 --- a/src/flextype/Foundation/Entries/Entries.php +++ b/src/flextype/Foundation/Entries/Entries.php @@ -35,106 +35,6 @@ class Entries */ private $storage = []; - /** - * Callback method for fetch. - * - * @var string - * @access private - */ - private $fetchCallbackMethod = 'single'; - - /** - * Get an item from an storage using "dot" notation. - * - * @param string|int|null $key Key. - * @param mixed $default Default value. - * - * @access public - * - * @return array Updated storage. - */ - public function getStorage($key, $default = null) - { - return arrays($this->storage)->get($key, $default); - } - - /** - * Checks if the given dot-notated key exists in the storage. - * - * @param string|array $keys Keys - * - * @return bool Return TRUE key exists in the array, FALSE otherwise. - */ - public function hasStorage($keys): bool - { - return arrays($this->storage)->has($keys); - } - - /** - * Set an storage item to a given value using "dot" notation. - * If no key is given to the method, the entire storage will be replaced. - * - * @param string|null $key Key. - * @param mixed $value Value. - * - * @access public - * - * @return array Updated storage. - */ - public function setStorage(?string $key, $value): void - { - $this->storage = arrays($this->storage)->set($key, $value)->toArray(); - } - - /** - * Deletes an storage value using "dot notation". - * - * @param array|string $keys Keys - * - * @return array Updated storage. - */ - public function deleteStorage($keys): self - { - return $this->storage = arrays($this->storage)->delete($keys)->toArray(); - } - - /** - * Fetch. - * - * @param string $id Unique identifier of the entry. - * @param array $options Options array. - * - * @access public - * - * @return mixed - */ - public function fetch(string $id, array $options = []) - { - // Store data in EMS. - $this->setStorage('fetch.id', $id); - $this->setStorage('fetch.options', $options); - $this->setStorage('fetch.data', []); - - // Run event: onEntriesFetch - flextype('emitter')->emit('onEntriesFetch'); - - // Get valid callable method for fetch. - $fetchFromCallbackMethodName = strings($this->hasStorage('fetch.options.from') ? - $this->getStorage('fetch.options.from') : - $this->fetchCallbackMethod) - ->studly() - ->prepend('fetch') - ->toString(); - - $fetchFromCallbackMethod = is_callable([$this, $fetchFromCallbackMethodName]) ? - $fetchFromCallbackMethodName : - $this->fetchCallbackMethod; - - // Get fetch result - return $this->{$fetchFromCallbackMethod}($this->getStorage('fetch.id'), - $this->getStorage('fetch.options')); - } - /** * Fetch single entry. * @@ -434,6 +334,61 @@ class Entries return filesystem()->file($this->getFileLocation($this->getStorage('has.id')))->exists(); } + /** + * Get an item from an storage using "dot" notation. + * + * @param string|int|null $key Key. + * @param mixed $default Default value. + * + * @access public + * + * @return array Updated storage. + */ + public function getStorage($key, $default = null) + { + return arrays($this->storage)->get($key, $default); + } + + /** + * Checks if the given dot-notated key exists in the storage. + * + * @param string|array $keys Keys + * + * @return bool Return TRUE key exists in the array, FALSE otherwise. + */ + public function hasStorage($keys): bool + { + return arrays($this->storage)->has($keys); + } + + /** + * Set an storage item to a given value using "dot" notation. + * If no key is given to the method, the entire storage will be replaced. + * + * @param string|null $key Key. + * @param mixed $value Value. + * + * @access public + * + * @return array Updated storage. + */ + public function setStorage(?string $key, $value): void + { + $this->storage = arrays($this->storage)->set($key, $value)->toArray(); + } + + /** + * Deletes an storage value using "dot notation". + * + * @param array|string $keys Keys + * + * @return array Updated storage. + */ + public function deleteStorage($keys): self + { + return $this->storage = arrays($this->storage)->delete($keys)->toArray(); + } + /** * Get entry file location * diff --git a/src/flextype/Foundation/Entries/Fields/EntriesField.php b/src/flextype/Foundation/Entries/Fields/EntriesField.php new file mode 100644 index 00000000..45ac6546 --- /dev/null +++ b/src/flextype/Foundation/Entries/Fields/EntriesField.php @@ -0,0 +1,53 @@ +get('flextype.settings.entries.fields.entries.fetchCollection.enabled')) { + flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function (): void { + if (flextype('entries')->getStorage('fetch.data.entries.fetchCollection') !== null) { + // Get fetch. + $original = flextype('entries')->getStorage('fetch'); + + // Modify fetch. + foreach (flextype('entries')->getStorage('fetch.data.entries.fetchCollection') as $field => $body) { + $data[$field] = flextype('entries')->fetchCollection($body['id'], + isset($body['options']) ? + $body['options'] : + []); + } + + // Save fetch. + flextype('entries')->setStorage('fetch.id', $original['id']); + flextype('entries')->setStorage('fetch.options', $original['options']); + flextype('entries')->setStorage('fetch.data', arrays($original['data'])->merge($data)->toArray()); + } + }); +} + +if (flextype('registry')->get('flextype.settings.entries.fields.entries.fetchSingle.enabled')) { + flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function (): void { + if (flextype('entries')->getStorage('fetch.data.entries.fetchSingle') !== null) { + // Get fetch. + $original = flextype('entries')->getStorage('fetch'); + + // Modify fetch. + foreach (flextype('entries')->getStorage('fetch.data.entries.fetchSingle') as $field => $body) { + $data[$field] = flextype('entries')->fetchSingle($body['id'], + isset($body['options']) ? + $body['options'] : + []); + } + + // Save fetch. + flextype('entries')->setStorage('fetch.id', $original['id']); + flextype('entries')->setStorage('fetch.options', $original['options']); + flextype('entries')->setStorage('fetch.data', arrays($original['data'])->merge($data)->toArray()); + } + }); +} diff --git a/src/flextype/Foundation/Entries/Fields/FetchField.php b/src/flextype/Foundation/Entries/Fields/FetchField.php deleted file mode 100644 index ecff19c9..00000000 --- a/src/flextype/Foundation/Entries/Fields/FetchField.php +++ /dev/null @@ -1,32 +0,0 @@ -get('flextype.settings.entries.fields.fetch.enabled')) { - flextype('emitter')->addListener('onEntriesFetchSingleHasResult', static function (): void { - - if (flextype('entries')->getStorage('fetch.data.fetch') !== null) { - - // Get fetch. - $original = flextype('entries')->getStorage('fetch'); - - // Modify fetch. - foreach (flextype('entries')->getStorage('fetch.data.fetch') as $field => $body) { - $data[$field] = flextype('entries')->fetch($body['id'], - isset($body['options']) ? - $body['options'] : - []); - } - - // Save fetch. - flextype('entries')->setStorage('fetch.id', $original['id']); - flextype('entries')->setStorage('fetch.options', $original['options']); - flextype('entries')->setStorage('fetch.data', arrays($original['data'])->merge($data)->toArray()); - } - }); -} diff --git a/src/flextype/Support/Parsers/Shortcodes/EntriesShortcode.php b/src/flextype/Support/Parsers/Shortcodes/EntriesShortcode.php index 8fd886b0..c8ad6c4f 100644 --- a/src/flextype/Support/Parsers/Shortcodes/EntriesShortcode.php +++ b/src/flextype/Support/Parsers/Shortcodes/EntriesShortcode.php @@ -12,6 +12,6 @@ use Thunder\Shortcode\Shortcode\ShortcodeInterface; // Shortcode: [entries_fetch id="entry-id" field="field-name" default="default-value"] if (flextype('registry')->get('flextype.settings.shortcode.shortcodes.entries.enabled')) { flextype('shortcode')->addHandler('entries_fetch', static function (ShortcodeInterface $s) { - return arrays(flextype('entries')->fetch($s->getParameter('id')))->get($s->getParameter('field'), $s->getParameter('default')); + return arrays(flextype('entries')->fetchSingle($s->getParameter('id')))->get($s->getParameter('field'), $s->getParameter('default')); }); } diff --git a/src/flextype/settings.yaml b/src/flextype/settings.yaml index c2bfad61..96a3f2ab 100644 --- a/src/flextype/settings.yaml +++ b/src/flextype/settings.yaml @@ -59,8 +59,11 @@ errors: entries: extension: md fields: - fetch: - enabled: true + entries: + fetchCollection: + enabled: true + fetchSingle: + enabled: true slug: enabled: true published_at: diff --git a/tests/Foundation/Entries/EntriesTest.php b/tests/Foundation/Entries/EntriesTest.php index dd3ef6ba..1418827f 100644 --- a/tests/Foundation/Entries/EntriesTest.php +++ b/tests/Foundation/Entries/EntriesTest.php @@ -27,28 +27,28 @@ test('test update() method', function () { $this->assertFalse(flextype('entries')->update('bar', ['title' => 'Test'])); }); -test('test fetch() entry', function () { +test('test fetchSingle() and fetchCollection() entry', function () { flextype('entries')->create('foo', ['title' => 'Foo']); flextype('entries')->create('foo/bar', ['title' => 'Bar']); flextype('entries')->create('foo/baz', ['title' => 'Baz']); flextype('entries')->create('foo/zed', ['title' => 'Zed']); - $this->assertEquals(12, flextype('entries')->fetch('foo')->count()); - $this->assertEquals('foo', flextype('entries')->fetch('foo')['id']); - $this->assertEquals(12, flextype('entries')->fetch('foo', ['from' => 'single'])->count()); - $this->assertEquals('foo', flextype('entries')->fetch('foo')['id']); - $this->assertEquals(3, flextype('entries')->fetch('foo', ['from' => 'collection'])->count()); + $this->assertEquals(12, flextype('entries')->fetchSingle('foo')->count()); + $this->assertEquals('foo', flextype('entries')->fetchSingle('foo')['id']); + $this->assertEquals(12, flextype('entries')->fetchSingle('foo', [])->count()); + $this->assertEquals('foo', flextype('entries')->fetchSingle('foo')['id']); + $this->assertEquals(3, flextype('entries')->fetchCollection('foo')->count()); - $this->assertEquals('Bar', flextype('entries')->fetch('foo/bar')['title']); - $this->assertEquals('Baz', flextype('entries')->fetch('foo/baz')['title']); - $this->assertEquals('Zed', flextype('entries')->fetch('foo/zed')['title']); + $this->assertEquals('Bar', flextype('entries')->fetchSingle('foo/bar')['title']); + $this->assertEquals('Baz', flextype('entries')->fetchSingle('foo/baz')['title']); + $this->assertEquals('Zed', flextype('entries')->fetchSingle('foo/zed')['title']); flextype('entries')->setStorage('fetch.id', 'wrong-entry'); - $this->assertEquals(0, flextype('entries')->fetch('wrong-entry')->count()); + $this->assertEquals(0, flextype('entries')->fetchSingle('wrong-entry')->count()); flextype('entries')->setStorage('fetch.id', 'wrong-entry'); - $this->assertEquals(0, flextype('entries')->fetch('wrong-entry')->count()); + $this->assertEquals(0, flextype('entries')->fetchSingle('wrong-entry')->count()); - $this->assertTrue(count(flextype('entries')->fetch('foo', ['from' => 'collection'])) > 0); + $this->assertTrue(count(flextype('entries')->fetchCollection('foo')) > 0); /* flextype('emitter')->addListener('onEntriesFetchCollectionHasResult', static function (): void { diff --git a/tests/Foundation/Entries/Fields/CreatedAtFieldTest.php b/tests/Foundation/Entries/Fields/CreatedAtFieldTest.php index b9fc101e..4d95f539 100644 --- a/tests/Foundation/Entries/Fields/CreatedAtFieldTest.php +++ b/tests/Foundation/Entries/Fields/CreatedAtFieldTest.php @@ -13,7 +13,7 @@ afterEach(function (): void { test('test CreatedAtField', function () { // 1 flextype('entries')->create('foo', []); - $created_at = flextype('entries')->fetch('foo')['created_at']; + $created_at = flextype('entries')->fetchSingle('foo')['created_at']; $this->assertTrue(strlen($created_at) > 0); $this->assertTrue((ctype_digit($created_at) && strtotime(date('Y-m-d H:i:s', $created_at)) === (int)$created_at)); }); diff --git a/tests/Foundation/Entries/Fields/CreatedByFieldTest.php b/tests/Foundation/Entries/Fields/CreatedByFieldTest.php index c97aa8c9..22864492 100644 --- a/tests/Foundation/Entries/Fields/CreatedByFieldTest.php +++ b/tests/Foundation/Entries/Fields/CreatedByFieldTest.php @@ -12,10 +12,10 @@ afterEach(function (): void { test('test CreatedByField', function () { flextype('entries')->create('foo', []); - $created_by = flextype('entries')->fetch('foo')['created_by']; + $created_by = flextype('entries')->fetchSingle('foo')['created_by']; $this->assertEquals('', $created_by); flextype('entries')->create('bar', ['created_by' => 'Zed']); - $created_by = flextype('entries')->fetch('bar')['created_by']; + $created_by = flextype('entries')->fetchSingle('bar')['created_by']; $this->assertEquals('Zed', $created_by); }); diff --git a/tests/Foundation/Entries/Fields/FetchFieldTest.php b/tests/Foundation/Entries/Fields/EntriesFieldTest.php similarity index 87% rename from tests/Foundation/Entries/Fields/FetchFieldTest.php rename to tests/Foundation/Entries/Fields/EntriesFieldTest.php index 2923aafa..5d186c8c 100644 --- a/tests/Foundation/Entries/Fields/FetchFieldTest.php +++ b/tests/Foundation/Entries/Fields/EntriesFieldTest.php @@ -10,17 +10,17 @@ afterEach(function (): void { filesystem()->directory(PATH['project'] . '/entries')->delete(); }); -test('test fetchField for blog', function () { +test('test entries field for blog', function () { flextype('entries')->create('blog', flextype('frontmatter')->decode(filesystem()->file(ROOT_DIR . '/tests/Foundation/Entries/Fields/fixtures/entries/blog/entry.md')->get())); flextype('entries')->create('blog/post-1', flextype('frontmatter')->decode(filesystem()->file(ROOT_DIR . '/tests/Foundation/Entries/Fields/fixtures/entries/blog/post-1/entry.md')->get())); flextype('entries')->create('blog/post-2', flextype('frontmatter')->decode(filesystem()->file(ROOT_DIR . '/tests/Foundation/Entries/Fields/fixtures/entries/blog/post-2/entry.md')->get())); - $blog = flextype('entries')->fetch('blog'); + $blog = flextype('entries')->fetchSingle('blog'); $this->assertEquals(14, $blog->count()); }); -test('test fetchField for catalog', function () { +test('test entries field for catalog', function () { // Create catalog flextype('entries')->create('catalog', flextype('frontmatter')->decode(filesystem()->file(ROOT_DIR . '/tests/Foundation/Entries/Fields/fixtures/entries/catalog/entry.md')->get())); @@ -38,7 +38,7 @@ test('test fetchField for catalog', function () { // Create banner flextype('entries')->create('banner', ['title' => 'Banner']); - $catalogSingle = flextype('entries')->fetch('catalog'); + $catalogSingle = flextype('entries')->fetchSingle('catalog'); $this->assertEquals(16, $catalogSingle->count()); $this->assertEquals('Catalog', $catalogSingle['title']); @@ -50,20 +50,20 @@ test('test fetchField for catalog', function () { $this->assertTrue(isset($catalogSingle['discounts']['discounts/30-off'])); $this->assertEquals('30% off', $catalogSingle['discounts']['discounts/30-off']['title']); - $catalogCollection = flextype('entries')->fetch('catalog', ['from' => 'collection']); + $catalogCollection = flextype('entries')->fetchCollection('catalog'); $this->assertEquals(1, $catalogCollection->count()); $this->assertEquals('Bikes', $catalogCollection['catalog/bikes']['title']); $this->assertEquals('catalog/bikes', $catalogCollection['catalog/bikes']['id']); - $catalogLongCollecion = flextype('entries')->fetch('catalog', ['from' => 'collection', 'find' => ['depth' => ['>0', '<4']]]); + $catalogLongCollecion = flextype('entries')->fetchCollection('catalog', ['find' => ['depth' => ['>0', '<4']]]); $this->assertEquals(5, $catalogLongCollecion->count()); - $banner = flextype('entries')->fetch('banner'); + $banner = flextype('entries')->fetchSingle('banner'); $this->assertEquals('Banner', $banner['title']); $this->assertEquals('banner', $banner['id']); }); -test('test fetchField for albmus', function () { +test('test entries field for albmus', function () { flextype('entries')->create('root', flextype('frontmatter')->decode(filesystem()->file(ROOT_DIR . '/tests/Foundation/Entries/Fields/fixtures/entries/root/entry.md')->get())); flextype('entries')->create('albums', flextype('frontmatter')->decode(filesystem()->file(ROOT_DIR . '/tests/Foundation/Entries/Fields/fixtures/entries/root/albums/entry.md')->get())); @@ -74,16 +74,18 @@ test('test fetchField for albmus', function () { flextype('entries')->create('banners/1', ['title' => 'Banner1']); flextype('entries')->create('banners/2', ['title' => 'Banner2']); + $root = flextype('entries')->fetchSingle('root'); + $this->assertEquals(16, $root->count()); }); -test('test fetchField for long nested entries', function () { +test('test entries field for long nested entries', function () { flextype('entries')->create('level1', flextype('frontmatter')->decode(filesystem()->file(ROOT_DIR . '/tests/Foundation/Entries/Fields/fixtures/entries/level1/entry.md')->get())); flextype('entries')->create('level1/level2', flextype('frontmatter')->decode(filesystem()->file(ROOT_DIR . '/tests/Foundation/Entries/Fields/fixtures/entries/level1/level2/entry.md')->get())); flextype('entries')->create('level1/level2/level3', flextype('frontmatter')->decode(filesystem()->file(ROOT_DIR . '/tests/Foundation/Entries/Fields/fixtures/entries/level1/level2/level3/entry.md')->get())); flextype('entries')->create('level1/level2/level3/level4', flextype('frontmatter')->decode(filesystem()->file(ROOT_DIR . '/tests/Foundation/Entries/Fields/fixtures/entries/level1/level2/level3/level4/entry.md')->get())); - $level = flextype('entries')->fetch('level1')->sortKeys(); + $level = flextype('entries')->fetchSingle('level1'); $this->assertEquals(14, $level->count()); $this->assertEquals('level1/level2', $level['root']['id']); diff --git a/tests/Foundation/Entries/Fields/IdFieldTest.php b/tests/Foundation/Entries/Fields/IdFieldTest.php index 129ff73c..f4816037 100644 --- a/tests/Foundation/Entries/Fields/IdFieldTest.php +++ b/tests/Foundation/Entries/Fields/IdFieldTest.php @@ -12,10 +12,10 @@ afterEach(function (): void { test('test IdField', function () { flextype('entries')->create('foo', []); - $id = flextype('entries')->fetch('foo')['id']; + $id = flextype('entries')->fetchSingle('foo')['id']; $this->assertEquals('foo', $id); flextype('entries')->create('foo/bar', []); - $id = flextype('entries')->fetch('foo/bar')['id']; + $id = flextype('entries')->fetchSingle('foo/bar')['id']; $this->assertEquals('foo/bar', $id); }); diff --git a/tests/Foundation/Entries/Fields/ModifiedAtFieldTest.php b/tests/Foundation/Entries/Fields/ModifiedAtFieldTest.php index 549b60af..6e7a9802 100644 --- a/tests/Foundation/Entries/Fields/ModifiedAtFieldTest.php +++ b/tests/Foundation/Entries/Fields/ModifiedAtFieldTest.php @@ -13,7 +13,7 @@ afterEach(function (): void { test('test ModifiedAtField', function () { flextype('entries')->create('foo', []); - $modified_at = flextype('entries')->fetch('foo')['modified_at']; + $modified_at = flextype('entries')->fetchSingle('foo')['modified_at']; $this->assertTrue(strlen($modified_at) > 0); $this->assertTrue((ctype_digit($modified_at) && strtotime(date('Y-m-d H:i:s', $modified_at)) === (int)$modified_at)); diff --git a/tests/Foundation/Entries/Fields/ParsersFieldTest.php b/tests/Foundation/Entries/Fields/ParsersFieldTest.php index b7d92fcf..61cf9136 100644 --- a/tests/Foundation/Entries/Fields/ParsersFieldTest.php +++ b/tests/Foundation/Entries/Fields/ParsersFieldTest.php @@ -12,8 +12,8 @@ afterEach(function (): void { test('test ParsersField', function () { flextype('entries')->create('foo', ['content' => '#Foo', 'parsers' => ['markdown' => ['enabled' => true, 'fields' => ['content']]]]); - $this->assertEquals('