From 7f92ad1b28a8141c72b0ccde8d3448513d12f26e Mon Sep 17 00:00:00 2001 From: Awilum Date: Mon, 9 May 2022 20:39:06 +0300 Subject: [PATCH] feat(directive): check if field value is string --- .../Entries/Directives/MarkdownDirective.php | 8 ++- .../Directives/ShortcodesDirective.php | 10 +-- .../Entries/Directives/TypesDirective.php | 68 ++++++++++--------- 3 files changed, 46 insertions(+), 40 deletions(-) diff --git a/src/flextype/core/Entries/Directives/MarkdownDirective.php b/src/flextype/core/Entries/Directives/MarkdownDirective.php index c73949cc..310a5feb 100644 --- a/src/flextype/core/Entries/Directives/MarkdownDirective.php +++ b/src/flextype/core/Entries/Directives/MarkdownDirective.php @@ -24,9 +24,11 @@ emitter()->addListener('onEntriesFetchSingleDirectives', static function (): voi $field = entries()->registry()->get('methods.fetch.field'); - if (strings($field)->contains('@parser:markdown')) { - $field = strings(parsers()->markdown()->parse($field))->replace('@parser:markdown', '')->trim()->toString(); - } + if (is_string($field)) { + if (strings($field)->contains('@parser:markdown')) { + $field = strings(parsers()->markdown()->parse($field))->replace('@parser:markdown', '')->trim()->toString(); + } + } entries()->registry()->set('methods.fetch.field', $field); }); \ No newline at end of file diff --git a/src/flextype/core/Entries/Directives/ShortcodesDirective.php b/src/flextype/core/Entries/Directives/ShortcodesDirective.php index 9c33ed70..e8917bb6 100644 --- a/src/flextype/core/Entries/Directives/ShortcodesDirective.php +++ b/src/flextype/core/Entries/Directives/ShortcodesDirective.php @@ -24,10 +24,12 @@ emitter()->addListener('onEntriesFetchSingleDirectives', static function (): voi $field = entries()->registry()->get('methods.fetch.field'); - if (strings($field)->contains('@parser:shortcodes')) { - $field = strings(parsers()->shortcodes()->parse($field))->replace('@parser:shortcodes', '')->trim()->toString(); - } elseif (registry()->get('flextype.settings.entries.parsers.shortcodes.enabled') !== false) { - $field = parsers()->shortcodes()->parse($field); + if (is_string($field)) { + if (strings($field)->contains('@parser:shortcodes')) { + $field = strings(parsers()->shortcodes()->parse($field))->replace('@parser:shortcodes', '')->trim()->toString(); + } elseif (registry()->get('flextype.settings.entries.parsers.shortcodes.enabled') !== false) { + $field = parsers()->shortcodes()->parse($field); + } } entries()->registry()->set('methods.fetch.field', $field); diff --git a/src/flextype/core/Entries/Directives/TypesDirective.php b/src/flextype/core/Entries/Directives/TypesDirective.php index 9bc99c99..d8f2ae8d 100644 --- a/src/flextype/core/Entries/Directives/TypesDirective.php +++ b/src/flextype/core/Entries/Directives/TypesDirective.php @@ -24,40 +24,42 @@ emitter()->addListener('onEntriesFetchSingleDirectives', static function (): voi $field = entries()->registry()->get('methods.fetch.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)->isJson()) { - $field = $field; - } else { - $field = collectionFromQueryString($field->toString())->toJson(); + 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)->isJson()) { + $field = $field; + } else { + $field = collectionFromQueryString($field->toString())->toJson(); + } + } 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(); + if (strings($field)->isJson()) { + $field = collection(serializers()->json()->decode($field->toString())); + } else { + $field = collectionFromQueryString($field->toString()); + } + } elseif (strings($field)->contains('@type:null')) { + $field = null; } - } 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(); - if (strings($field)->isJson()) { - $field = collection(serializers()->json()->decode($field->toString())); - } else { - $field = collectionFromQueryString($field->toString()); - } - } elseif (strings($field)->contains('@type:null')) { - $field = null; } - + entries()->registry()->set('methods.fetch.field', $field); }); \ No newline at end of file