diff --git a/src/flextype/core/Entries/Directives/TypesDirective.php b/src/flextype/core/Entries/Directives/TypesDirective.php index 9db6013f..d55b8962 100644 --- a/src/flextype/core/Entries/Directives/TypesDirective.php +++ b/src/flextype/core/Entries/Directives/TypesDirective.php @@ -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); diff --git a/tests/src/flextype/core/Entries/Directives/TypesDirectiveTest.php b/tests/src/flextype/core/Entries/Directives/TypesDirectiveTest.php index 0a932380..34a94951 100644 --- a/tests/src/flextype/core/Entries/Directives/TypesDirectiveTest.php +++ b/tests/src/flextype/core/Entries/Directives/TypesDirectiveTest.php @@ -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']); }); \ No newline at end of file