diff --git a/src/flextype/core/Entries/Directives/MarkdownDirective.php b/src/flextype/core/Entries/Directives/MarkdownDirective.php new file mode 100644 index 00000000..c73949cc --- /dev/null +++ b/src/flextype/core/Entries/Directives/MarkdownDirective.php @@ -0,0 +1,32 @@ +addListener('onEntriesFetchSingleDirectives', static function (): void { + + if (! registry()->get('flextype.settings.entries.directives.markdown.enabled')) { + return; + } + + $field = entries()->registry()->get('methods.fetch.field'); + + if (strings($field)->contains('@parser:markdown')) { + $field = strings(parsers()->markdown()->parse($field))->replace('@parser:markdown', '')->trim()->toString(); + } + + entries()->registry()->set('methods.fetch.field', $field); +}); \ No newline at end of file diff --git a/src/flextype/core/Entries/Directives/ShortcodesDirective.php b/src/flextype/core/Entries/Directives/ShortcodesDirective.php new file mode 100644 index 00000000..65a47a2a --- /dev/null +++ b/src/flextype/core/Entries/Directives/ShortcodesDirective.php @@ -0,0 +1,32 @@ +addListener('onEntriesFetchSingleDirectives', static function (): void { + + if (! registry()->get('flextype.settings.entries.directives.shortcodes.enabled')) { + return; + } + + $field = entries()->registry()->get('methods.fetch.field'); + + if (strings($field)->contains('@parser:shortcodes') && registry()->get('flextype.settings.entries.parsers.shortcodes.enabled') != false) { + $field = strings(parsers()->shortcodes()->parse($field))->replace('@parser:shortcodes', '')->trim()->toString(); + } + + entries()->registry()->set('methods.fetch.field', $field); +}); \ No newline at end of file diff --git a/src/flextype/core/Entries/Directives/TypesDirective.php b/src/flextype/core/Entries/Directives/TypesDirective.php new file mode 100644 index 00000000..9db6013f --- /dev/null +++ b/src/flextype/core/Entries/Directives/TypesDirective.php @@ -0,0 +1,43 @@ +addListener('onEntriesFetchSingleDirectives', static function (): void { + + if (! registry()->get('flextype.settings.entries.directives.types.enabled')) { + return; + } + + $field = entries()->registry()->get('methods.fetch.field'); + + strings($field)->contains('@type:integer') and $field = strings(strings($field)->replace('@type:integer', '')->trim())->toInteger(); + strings($field)->contains('@type:int') and $field = strings(strings($field)->replace('@type:int', '')->trim())->toInteger(); + strings($field)->contains('@type:float') and $field = strings(strings($field)->replace('@type:float', '')->trim())->toFloat(); + strings($field)->contains('@type:boolean') and $field = strings(strings($field)->replace('@type:boolean', '')->trim())->toBoolean(); + strings($field)->contains('@type:bool') and $field = strings(strings($field)->replace('@type:bool', '')->trim())->toBoolean(); + + if (strings($field)->contains('@type:array')) { + $field = strings($field)->replace('@type:array', '')->trim(); + if (strings($field)->isJson()) { + $field = serializers()->json()->decode($field->toString()); + } else { + $field = strings($field)->toArray(','); + } + } + + entries()->registry()->set('methods.fetch.field', $field); +}); \ No newline at end of file diff --git a/src/flextype/core/Entries/Entries.php b/src/flextype/core/Entries/Entries.php index e6fb5685..082d84c9 100755 --- a/src/flextype/core/Entries/Entries.php +++ b/src/flextype/core/Entries/Entries.php @@ -80,6 +80,7 @@ class Entries $this->setRegistry($registry); $this->setOptions($options); + $this->loadCollectionsDirectives(); $this->loadCollectionsMacros(); $this->loadCollectionsEvents(); $this->loadCollectionsFields(); @@ -123,6 +124,20 @@ class Entries } } + /** + * Load Collections Directives + * + * @access public + */ + private function loadCollectionsDirectives(): void + { + foreach (registry()->get('flextype.settings.entries.directives') as $key => $value) { + if (filesystem()->file(ROOT_DIR . $value['path'])->exists()) { + include_once ROOT_DIR . $value['path']; + } + } + } + /** * Load Collections Events * @@ -354,6 +369,20 @@ class Entries // Get the result. $result = $this->registry()->get('methods.fetch.result'); + // Directives processor + $result = collection($result)->dot()->map(function ($field) { + $this->registry()->set('methods.fetch.field', $field); + if (is_string($field)) { + + // Run event + emitter()->emit('onEntriesFetchSingleDirectives'); + + return $this->registry()->get('methods.fetch.field'); + } else { + return $this->registry()->get('methods.fetch.field'); + } + })->undot(); + // Apply `filterCollection` filter for fetch result $this->registry()->set('methods.fetch.result', filterCollection($result, $this->registry()->get('methods.fetch.params.options.filter', []))); diff --git a/src/flextype/core/Entries/Fields/Default/IdField.php b/src/flextype/core/Entries/Fields/Default/IdField.php index 628e67bb..ba14b804 100644 --- a/src/flextype/core/Entries/Fields/Default/IdField.php +++ b/src/flextype/core/Entries/Fields/Default/IdField.php @@ -19,7 +19,7 @@ emitter()->addListener('onEntriesFetchSingleHasResult', static function (): void if (! entries()->registry()->get('methods.fetch.collection.fields.id.enabled')) { return; } - + if (entries()->registry()->get('methods.fetch.result.id') !== null) { return; } diff --git a/src/flextype/settings.yaml b/src/flextype/settings.yaml index 34e3065e..3c709bf1 100644 --- a/src/flextype/settings.yaml +++ b/src/flextype/settings.yaml @@ -74,6 +74,19 @@ errors: # Entries entries: directory: '/entries' + parsers: + shortcodes: + enabled: true + directives: + markdown: + enabled: true + path: "/src/flextype/core/Entries/Directives/MarkdownDirective.php" + shortcodes: + enabled: true + path: "/src/flextype/core/Entries/Directives/ShortcodesDirective.php" + types: + enabled: true + path: "/src/flextype/core/Entries/Directives/TypesDirective.php" macros: debug: false registry: diff --git a/tests/fixtures/settings/settings.yaml b/tests/fixtures/settings/settings.yaml index ff5fe621..700cea80 100644 --- a/tests/fixtures/settings/settings.yaml +++ b/tests/fixtures/settings/settings.yaml @@ -70,6 +70,19 @@ errors: # Entries entries: directory: '/entries' + parsers: + shortcodes: + enabled: true + directives: + markdown: + enabled: true + path: "/src/flextype/core/Entries/Directives/MarkdownDirective.php" + shortcodes: + enabled: true + path: "/src/flextype/core/Entries/Directives/ShortcodesDirective.php" + types: + enabled: true + path: "/src/flextype/core/Entries/Directives/TypesDirective.php" macros: debug: false registry: diff --git a/tests/src/flextype/core/Entries/Directives/MarkdownDirectiveTest.php b/tests/src/flextype/core/Entries/Directives/MarkdownDirectiveTest.php new file mode 100644 index 00000000..27d599f6 --- /dev/null +++ b/tests/src/flextype/core/Entries/Directives/MarkdownDirectiveTest.php @@ -0,0 +1,17 @@ +directory(PATH['project'] . '/entries')->create(); +}); + +afterEach(function (): void { + filesystem()->directory(PATH['project'] . '/entries')->delete(); +}); + +test('markdown directive', function () { + entries()->create('markdown', ['foo' => '@parser:markdown **Hello world!**']); + + $this->assertEquals('
Hello world!
', entries()->fetch('markdown')['foo']); +}); \ No newline at end of file diff --git a/tests/src/flextype/core/Entries/Directives/ShortcodesDirectiveTest.php b/tests/src/flextype/core/Entries/Directives/ShortcodesDirectiveTest.php new file mode 100644 index 00000000..65649670 --- /dev/null +++ b/tests/src/flextype/core/Entries/Directives/ShortcodesDirectiveTest.php @@ -0,0 +1,17 @@ +directory(PATH['project'] . '/entries')->create(); +}); + +afterEach(function (): void { + filesystem()->directory(PATH['project'] . '/entries')->delete(); +}); + +test('shortcodes directive', function () { + entries()->create('shortcodes', ['foo' => '@parser:shortcodes [strings prepend="Hello "]World[/strings]']); + + $this->assertEquals('Hello World', entries()->fetch('shortcodes')['foo']); +}); \ No newline at end of file diff --git a/tests/src/flextype/core/Entries/Directives/TypesDirectiveTest.php b/tests/src/flextype/core/Entries/Directives/TypesDirectiveTest.php new file mode 100644 index 00000000..22977cc1 --- /dev/null +++ b/tests/src/flextype/core/Entries/Directives/TypesDirectiveTest.php @@ -0,0 +1,31 @@ +directory(PATH['project'] . '/entries')->create(); +}); + +afterEach(function (): void { + filesystem()->directory(PATH['project'] . '/entries')->delete(); +}); + +test('types directive', function () { + entries()->create('type-int', ['foo' => '@type:int 100']); + entries()->create('type-integer', ['foo' => '@type:integer 100']); + entries()->create('type-bool', ['foo' => '@type:bool true']); + entries()->create('type-boolean', ['foo' => '@type:boolean false']); + entries()->create('type-float', ['foo' => '@type:float 1.5']); + entries()->create('type-array', ['foo' => '@type:array 1,2,3,4,5']); + entries()->create('type-array-2', ['foo' => '@type:array [1,2,3,4,5]']); + entries()->create('type-array-3', ['foo' => '@type:array {"foo": "Foo"}']); + + $this->assertEquals(100, entries()->fetch('type-int')['foo']); + $this->assertEquals(100, entries()->fetch('type-integer')['foo']); + $this->assertEquals(true, entries()->fetch('type-bool')['foo']); + $this->assertEquals(false, entries()->fetch('type-boolean')['foo']); + $this->assertEquals(1.5, entries()->fetch('type-float')['foo']); + $this->assertEquals([1,2,3,4,5], entries()->fetch('type-array')['foo']); + $this->assertEquals([1,2,3,4,5], entries()->fetch('type-array-2')['foo']); + $this->assertEquals(['foo' => 'Foo'], entries()->fetch('type-array-3')['foo']); +}); \ No newline at end of file