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

feat(directives): add type null and collection for types directive

This commit is contained in:
Awilum
2022-05-09 08:01:37 +03:00
parent 7d610f0a0e
commit 78d94dcdb5
2 changed files with 23 additions and 9 deletions

View File

@@ -24,19 +24,32 @@ emitter()->addListener('onEntriesFetchSingleDirectives', static function (): voi
$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')) {
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:array')) {
$field = strings($field)->replace('@type:array', '')->trim();
if (strings($field)->isJson()) {
$field = serializers()->json()->decode($field->toString());
} else {
$field = strings($field)->toArray(',');
}
} 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 = collection(strings($field)->toArray(','));
}
} elseif (strings($field)->contains('@type:null')) {
$field = null;
}
entries()->registry()->set('methods.fetch.field', $field);

View File

@@ -20,7 +20,8 @@ test('types directive', function () {
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']);
$this->assertEquals(100, entries()->fetch('type-int')['foo']);
$this->assertEquals(100, entries()->fetch('type-integer')['foo']);
@@ -31,5 +32,5 @@ test('types directive', function () {
$this->assertEquals([1,2,3,4,5], entries()->fetch('type-array-2')['foo']);
$this->assertEquals(['foo' => 'Foo'], entries()->fetch('type-array-3')['foo']);
$this->assertEquals(['foo'], entries()->fetch('type-array-4')['foo']);
$this->assertEquals(null, entries()->fetch('type-null')['foo']);
});