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

feat(directives): refactor directives

This commit is contained in:
Awilum
2022-05-11 19:39:19 +03:00
parent 98752121c9
commit 865897f80f
9 changed files with 45 additions and 47 deletions

View File

@@ -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);

View File

@@ -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();
}
}

View File

@@ -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);
}

View File

@@ -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);
});

View File

@@ -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']);

View File

@@ -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('<p> <strong>Hello world!</strong></p>', entries()->fetch('markdown')['foo']);
});

View File

@@ -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']);
});

View File

@@ -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']);

View File

@@ -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']));