From 865897f80fd40d1ef0dbd63361e788334dcae862 Mon Sep 17 00:00:00 2001 From: Awilum Date: Wed, 11 May 2022 19:39:19 +0300 Subject: [PATCH] feat(directives): refactor directives --- .../Entries/Directives/ConstantsDirective.php | 10 +++--- .../Entries/Directives/MarkdownDirective.php | 4 +-- .../Directives/ShortcodesDirective.php | 4 +-- .../Entries/Directives/TypesDirective.php | 36 +++++++++---------- .../Directives/ConstantsDirectiveTest.php | 4 +-- .../Directives/MarkdownDirectiveTest.php | 2 +- .../Directives/ShortcodesDirectiveTest.php | 2 +- .../Entries/Directives/TypesDirectiveTest.php | 28 +++++++-------- .../Shortcodes/EntriesShortcodeTest.php | 2 +- 9 files changed, 45 insertions(+), 47 deletions(-) diff --git a/src/flextype/core/Entries/Directives/ConstantsDirective.php b/src/flextype/core/Entries/Directives/ConstantsDirective.php index 5bc067fb..1b9d57d9 100644 --- a/src/flextype/core/Entries/Directives/ConstantsDirective.php +++ b/src/flextype/core/Entries/Directives/ConstantsDirective.php @@ -23,13 +23,11 @@ emitter()->addListener('onEntriesFetchSingleDirectives', static function (): voi } $field = entries()->registry()->get('methods.fetch.field'); - if (is_string($field)) { - if (strings($field)->contains('@const:ROOT_DIR')) { - $field = strings($field)->replace('@const:ROOT_DIR', ROOT_DIR)->trim()->toString(); - } elseif (strings($field)->contains('@const:PATH_PROJECT')) { - $field = strings($field)->replace('@const:PATH_PROJECT', PATH['project'])->trim()->toString(); - } + $field = strings($field) + ->replace('@const(ROOT_DIR)', ROOT_DIR) + ->replace('@const(PATH_PROJECT)', PATH['project']) + ->toString(); } entries()->registry()->set('methods.fetch.field', $field); diff --git a/src/flextype/core/Entries/Directives/MarkdownDirective.php b/src/flextype/core/Entries/Directives/MarkdownDirective.php index 310a5feb..1fbcb5a8 100644 --- a/src/flextype/core/Entries/Directives/MarkdownDirective.php +++ b/src/flextype/core/Entries/Directives/MarkdownDirective.php @@ -25,8 +25,8 @@ emitter()->addListener('onEntriesFetchSingleDirectives', static function (): voi $field = entries()->registry()->get('methods.fetch.field'); if (is_string($field)) { - if (strings($field)->contains('@parser:markdown')) { - $field = strings(parsers()->markdown()->parse($field))->replace('@parser:markdown', '')->trim()->toString(); + if (strings($field)->contains('@markdown')) { + $field = strings(parsers()->markdown()->parse($field))->replace('@markdown', '')->trim()->toString(); } } diff --git a/src/flextype/core/Entries/Directives/ShortcodesDirective.php b/src/flextype/core/Entries/Directives/ShortcodesDirective.php index e8917bb6..0a978e6c 100644 --- a/src/flextype/core/Entries/Directives/ShortcodesDirective.php +++ b/src/flextype/core/Entries/Directives/ShortcodesDirective.php @@ -25,8 +25,8 @@ emitter()->addListener('onEntriesFetchSingleDirectives', static function (): voi $field = entries()->registry()->get('methods.fetch.field'); if (is_string($field)) { - if (strings($field)->contains('@parser:shortcodes')) { - $field = strings(parsers()->shortcodes()->parse($field))->replace('@parser:shortcodes', '')->trim()->toString(); + if (strings($field)->contains('@shortcodes')) { + $field = strings(parsers()->shortcodes()->parse($field))->replace('@shortcodes', '')->trim()->toString(); } elseif (registry()->get('flextype.settings.entries.parsers.shortcodes.enabled') !== false) { $field = parsers()->shortcodes()->parse($field); } diff --git a/src/flextype/core/Entries/Directives/TypesDirective.php b/src/flextype/core/Entries/Directives/TypesDirective.php index d8f2ae8d..6feaa5ca 100644 --- a/src/flextype/core/Entries/Directives/TypesDirective.php +++ b/src/flextype/core/Entries/Directives/TypesDirective.php @@ -25,41 +25,41 @@ emitter()->addListener('onEntriesFetchSingleDirectives', static function (): voi $field = entries()->registry()->get('methods.fetch.field'); if (is_string($field)) { - if (strings($field)->contains('@type:integer')) { - $field = strings(strings($field)->replace('@type:integer', '')->trim())->toInteger(); - } elseif (strings($field)->contains('@type:int')) { - $field = strings(strings($field)->replace('@type:int', '')->trim())->toInteger(); - } elseif (strings($field)->contains('@type:float')) { - $field = strings(strings($field)->replace('@type:float', '')->trim())->toFloat(); - } elseif (strings($field)->contains('@type:boolean')) { - $field = strings(strings($field)->replace('@type:boolean', '')->trim())->toBoolean(); - } elseif (strings($field)->contains('@type:bool')) { - $field = strings(strings($field)->replace('@type:bool', '')->trim())->toBoolean(); - } elseif (strings($field)->contains('@type:json')) { - $field = strings($field)->replace('@type:json', '')->trim(); + if (strings($field)->contains('@type(integer)')) { + $field = strings(strings($field)->replace('@type(integer)', '')->trim())->toInteger(); + } elseif (strings($field)->contains('@type(int)')) { + $field = strings(strings($field)->replace('@type(int)', '')->trim())->toInteger(); + } elseif (strings($field)->contains('@type(float)')) { + $field = strings(strings($field)->replace('@type(float)', '')->trim())->toFloat(); + } elseif (strings($field)->contains('@type(boolean)')) { + $field = strings(strings($field)->replace('@type(boolean)', '')->trim())->toBoolean(); + } elseif (strings($field)->contains('@type(bool)')) { + $field = strings(strings($field)->replace('@type(bool)', '')->trim())->toBoolean(); + } elseif (strings($field)->contains('@type(json)')) { + $field = strings($field)->replace('@type(json)', '')->trim(); if (strings($field)->isJson()) { $field = $field; } else { $field = collectionFromQueryString($field->toString())->toJson(); } - } elseif (strings($field)->contains('@type:array')) { - $field = strings($field)->replace('@type:array', '')->trim(); + } elseif (strings($field)->contains('@type(array)')) { + $field = strings($field)->replace('@type(array)', '')->trim(); if (strings($field)->isJson()) { $field = serializers()->json()->decode($field->toString()); } else { $field = collectionFromQueryString($field->toString())->toArray(); } - } elseif (strings($field)->contains('@type:collection')) { - $field = strings($field)->replace('@type:collection', '')->trim(); + } elseif (strings($field)->contains('@type(collection)')) { + $field = strings($field)->replace('@type(collection)', '')->trim(); if (strings($field)->isJson()) { $field = collection(serializers()->json()->decode($field->toString())); } else { $field = collectionFromQueryString($field->toString()); } - } elseif (strings($field)->contains('@type:null')) { + } elseif (strings($field)->contains('@type(null)')) { $field = null; } } - + entries()->registry()->set('methods.fetch.field', $field); }); \ No newline at end of file diff --git a/tests/src/flextype/core/Entries/Directives/ConstantsDirectiveTest.php b/tests/src/flextype/core/Entries/Directives/ConstantsDirectiveTest.php index 7f071fdc..d4ae28c7 100644 --- a/tests/src/flextype/core/Entries/Directives/ConstantsDirectiveTest.php +++ b/tests/src/flextype/core/Entries/Directives/ConstantsDirectiveTest.php @@ -11,8 +11,8 @@ afterEach(function (): void { }); test('constants directive', function () { - entries()->create('const-root-dir', ['path' => '@const:ROOT_DIR']); - entries()->create('const-root-dir-2', ['path' => '@const:PATH_PROJECT']); + entries()->create('const-root-dir', ['path' => '@const(ROOT_DIR)']); + entries()->create('const-root-dir-2', ['path' => '@const(PATH_PROJECT)']); $this->assertEquals(ROOT_DIR, entries()->fetch('const-root-dir')['path']); $this->assertEquals(PATH['project'], entries()->fetch('const-root-dir-2')['path']); diff --git a/tests/src/flextype/core/Entries/Directives/MarkdownDirectiveTest.php b/tests/src/flextype/core/Entries/Directives/MarkdownDirectiveTest.php index 27d599f6..1c85aebe 100644 --- a/tests/src/flextype/core/Entries/Directives/MarkdownDirectiveTest.php +++ b/tests/src/flextype/core/Entries/Directives/MarkdownDirectiveTest.php @@ -11,7 +11,7 @@ afterEach(function (): void { }); test('markdown directive', function () { - entries()->create('markdown', ['foo' => '@parser:markdown **Hello world!**']); + entries()->create('markdown', ['foo' => '@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 index 65649670..423da246 100644 --- a/tests/src/flextype/core/Entries/Directives/ShortcodesDirectiveTest.php +++ b/tests/src/flextype/core/Entries/Directives/ShortcodesDirectiveTest.php @@ -11,7 +11,7 @@ afterEach(function (): void { }); test('shortcodes directive', function () { - entries()->create('shortcodes', ['foo' => '@parser:shortcodes [strings prepend="Hello "]World[/strings]']); + entries()->create('shortcodes', ['foo' => '@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 index 355af9b5..8ced68ce 100644 --- a/tests/src/flextype/core/Entries/Directives/TypesDirectiveTest.php +++ b/tests/src/flextype/core/Entries/Directives/TypesDirectiveTest.php @@ -11,20 +11,20 @@ afterEach(function (): void { }); 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 foo=bar']); - entries()->create('type-array-2', ['foo' => '@type:array [1,2,3,4,5]']); - entries()->create('type-array-3', ['foo' => '@type:array {"foo": "Foo"}']); - entries()->create('type-array-4', ['foo' => '@type:array foo']); - entries()->create('type-collection', ['foo' => '@type:collection foo']); - entries()->create('type-null', ['foo' => '@type:null foo']); - entries()->create('type-json', ['foo' => '@type:json foo=Foo']); - entries()->create('type-json-2', ['foo' => '@type:json {"foo": "Foo"}']); - entries()->create('type-json-3', ['foo' => '@type:json [1,2,3,4,5]']); + 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) foo=bar']); + entries()->create('type-array-2', ['foo' => '@type(array) [1,2,3,4,5]']); + entries()->create('type-array-3', ['foo' => '@type(array) {"foo": "Foo"}']); + entries()->create('type-array-4', ['foo' => '@type(array) foo']); + entries()->create('type-collection', ['foo' => '@type(collection) foo']); + entries()->create('type-null', ['foo' => '@type(null) foo']); + entries()->create('type-json', ['foo' => '@type(json) foo=Foo']); + entries()->create('type-json-2', ['foo' => '@type(json) {"foo": "Foo"}']); + entries()->create('type-json-3', ['foo' => '@type(json) [1,2,3,4,5]']); $this->assertEquals(100, entries()->fetch('type-int')['foo']); $this->assertEquals(100, entries()->fetch('type-integer')['foo']); diff --git a/tests/src/flextype/core/Parsers/Shortcodes/EntriesShortcodeTest.php b/tests/src/flextype/core/Parsers/Shortcodes/EntriesShortcodeTest.php index e8e05cdc..8cdf5027 100644 --- a/tests/src/flextype/core/Parsers/Shortcodes/EntriesShortcodeTest.php +++ b/tests/src/flextype/core/Parsers/Shortcodes/EntriesShortcodeTest.php @@ -11,7 +11,7 @@ afterEach(function () { }); test('[entries-fetch] shortcode', function () { - $this->assertTrue(entries()->create('blog', ['title' => 'Blog', 'categories' => "@type:array [entries fetch=\"blog|collection=true&filter[sort_by][key]=date&filter[sort_by][direction]=ASC\" /]"])); + $this->assertTrue(entries()->create('blog', ['title' => 'Blog', 'categories' => "@type(array) [entries fetch=\"blog|collection=true&filter[sort_by][key]=date&filter[sort_by][direction]=ASC\" /]"])); $this->assertTrue(entries()->create('blog/post-1', ['title' => 'Post 1'])); $this->assertTrue(entries()->create('blog/post-2', ['title' => 'Post 2'])); $this->assertTrue(entries()->create('blog/post-3', ['title' => 'Post 3']));