1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-07 05:36:54 +02:00

feat(directives): implement new syntax for directives

This commit is contained in:
Awilum
2022-05-21 16:20:41 +03:00
parent e8ffb95acf
commit f102755072
11 changed files with 48 additions and 47 deletions

View File

@@ -28,7 +28,7 @@ emitter()->addListener('onEntriesFetchSingleField', static function (): void {
$result = entries()->registry()->get('methods.fetch.result');
if (is_string($field['value'])) {
$field['value'] = preg_replace_callback('/@calc\((.*?)\)/s', function($matches) use ($result) {
$field['value'] = preg_replace_callback('/@calc\[(.*?)\]/s', function($matches) use ($result) {
return (new StringCalc())->calculate($matches[1]);
}, $field['value']);
}

View File

@@ -27,8 +27,8 @@ emitter()->addListener('onEntriesFetchSingleField', static function (): void {
if (is_string($field['value'])) {
$field['value'] = strings($field['value'])
->replace('@const(ROOT_DIR)', ROOT_DIR)
->replace('@const(PATH_PROJECT)', PATH['project'])
->replace('@const[ROOT_DIR]', ROOT_DIR)
->replace('@const[PATH_PROJECT]', PATH['project'])
->toString();
}

View File

@@ -27,7 +27,7 @@ emitter()->addListener('onEntriesFetchSingleField', static function (): void {
$result = entries()->registry()->get('methods.fetch.result');
if (is_string($field['value'])) {
$field['value'] = preg_replace_callback('/@field\((.*?)\)/s', function($matches) use ($result) {
$field['value'] = preg_replace_callback('/@field\[(.*?)\]/s', function($matches) use ($result) {
return collection($result)->get($matches[1]);
}, $field['value']);
}

View File

@@ -16,7 +16,7 @@ declare(strict_types=1);
use Glowy\Arrays\Arrays as Collection;
// Directive: @type()
// Directive: @type[)
emitter()->addListener('onEntriesFetchSingleField', static function (): void {
if (! registry()->get('flextype.settings.entries.directives.types.enabled')) {
@@ -26,40 +26,40 @@ emitter()->addListener('onEntriesFetchSingleField', static function (): void {
$field = entries()->registry()->get('methods.fetch.field');
if (is_string($field['value'])) {
if (strings($field['value'])->contains('@type(integer)')) {
$field['value'] = strings(strings($field['value'])->replace('@type(integer)', '')->trim())->toInteger();
} elseif (strings($field['value'])->contains('@type(int)')) {
$field['value'] = strings(strings($field['value'])->replace('@type(int)', '')->trim())->toInteger();
} elseif (strings($field['value'])->contains('@type(float)')) {
$field['value'] = strings(strings($field['value'])->replace('@type(float)', '')->trim())->toFloat();
} elseif (strings($field['value'])->contains('@type(boolean)')) {
$field['value'] = strings(strings($field['value'])->replace('@type(boolean)', '')->trim())->toBoolean();
} elseif (strings($field['value'])->contains('@type(bool)')) {
$field['value'] = strings(strings($field['value'])->replace('@type(bool)', '')->trim())->toBoolean();
} elseif (strings($field['value'])->contains('@type(json)')) {
$field['value'] = strings($field['value'])->replace('@type(json)', '')->trim();
if (strings($field['value'])->contains('@type[integer]')) {
$field['value'] = strings(strings($field['value'])->replace('@type[integer]', '')->trim())->toInteger();
} elseif (strings($field['value'])->contains('@type[int]')) {
$field['value'] = strings(strings($field['value'])->replace('@type[int]', '')->trim())->toInteger();
} elseif (strings($field['value'])->contains('@type[float]')) {
$field['value'] = strings(strings($field['value'])->replace('@type[float]', '')->trim())->toFloat();
} elseif (strings($field['value'])->contains('@type[boolean]')) {
$field['value'] = strings(strings($field['value'])->replace('@type[boolean]', '')->trim())->toBoolean();
} elseif (strings($field['value'])->contains('@type[bool]')) {
$field['value'] = strings(strings($field['value'])->replace('@type[bool]', '')->trim())->toBoolean();
} elseif (strings($field['value'])->contains('@type[json]')) {
$field['value'] = strings($field['value'])->replace('@type[json]', '')->trim();
if (strings($field['value'])->isJson()) {
$field['value'] = $field['value'];
} else {
$field['value'] = collectionFromQueryString($field['value']->toString())->toJson();
}
} elseif (strings($field['value'])->contains('@type(array)')) {
$field['value'] = strings($field['value'])->replace('@type(array)', '')->trim();
} elseif (strings($field['value'])->contains('@type[array]')) {
$field['value'] = strings($field['value'])->replace('@type[array]', '')->trim();
if (strings($field['value'])->isJson()) {
$field['value'] = serializers()->json()->decode($field['value']->toString());
} else {
$field['value'] = collectionFromQueryString($field['value']->toString())->toArray();
}
} elseif (strings($field['value'])->contains('@type(collection)')) {
$field['value'] = strings($field['value'])->replace('@type(collection)', '')->trim();
} elseif (strings($field['value'])->contains('@type[collection]')) {
$field['value'] = strings($field['value'])->replace('@type[collection]', '')->trim();
if (strings($field['value'])->isJson()) {
$field['value'] = collection(serializers()->json()->decode($field['value']->toString()));
} else {
$field['value'] = collectionFromQueryString($field['value']->toString());
}
} elseif (strings($field['value'])->contains('@type(string)')) {
$field['value'] = strings(strings($field['value'])->replace('@type(string)', '')->trim())->toString();
} elseif (strings($field['value'])->contains('@type(null)')) {
} elseif (strings($field['value'])->contains('@type[string]')) {
$field['value'] = strings(strings($field['value'])->replace('@type[string]', '')->trim())->toString();
} elseif (strings($field['value'])->contains('@type[null]')) {
$field['value'] = null;
}
}

View File

@@ -27,8 +27,8 @@ emitter()->addListener('onEntriesFetchSingleField', static function (): void {
$result = entries()->registry()->get('methods.fetch.result');
if (is_string($field['value'])) {
$field['value'] = preg_replace_callback('/@var\((.*?)\)/s', function($matches) use ($result) {
return collection($result['vars'])->get($matches[1]);
$field['value'] = preg_replace_callback('/@var\[(.*?)\]/s', function($matches) use ($result) {
return collection($result['vars'])->get(trim($matches[1]));
}, $field['value']);
}

View File

@@ -11,6 +11,7 @@ afterEach(function (): void {
});
test('calc directive', function () {
entries()->create('field', ['foo' => '@calc(2+2)']);
// (calc:1+1)
entries()->create('field', ['foo' => '@calc[2+2]']);
$this->assertEquals(4, entries()->fetch('field')['foo']);
});

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,6 +11,6 @@ afterEach(function (): void {
});
test('fields directive', function () {
entries()->create('field', ['foo' => '@field(id)']);
entries()->create('field', ['foo' => '@field[id]']);
$this->assertEquals('field', entries()->fetch('field')['foo']);
});

View File

@@ -11,7 +11,7 @@ afterEach(function (): void {
});
test('shortcodes directive', function () {
entries()->create('shortcodes', ['foo' => '@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,21 +11,21 @@ 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-string', ['foo' => '@type(string) 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-string', ['foo' => '@type[string] 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 (): void {
});
test('vars directive', function () {
entries()->create('type-vars', ['vars' => ['foo' => 'Foo'], 'title' => '@var(foo)']);
entries()->create('type-vars', ['vars' => ['foo' => 'Foo'], 'title' => '@var[foo]']);
$this->assertEquals('Foo', entries()->fetch('type-vars')['title']);
});