From 2947e85e20d1082972be655314d3878d6c98ead8 Mon Sep 17 00:00:00 2001 From: Benoit Jouhaud Date: Mon, 20 May 2024 00:04:42 +0200 Subject: [PATCH] Adapters FunctionExpressionMatcher should return as is if nothing matches --- src/Adapter/ConstantAdapter.php | 7 ++++++- src/Adapter/DateTimeAdapter.php | 7 ++++++- src/Adapter/EnumAdapter.php | 7 ++++++- src/Adapter/FactoryAdapter.php | 7 ++++++- 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/Adapter/ConstantAdapter.php b/src/Adapter/ConstantAdapter.php index af82ad9..440eff8 100644 --- a/src/Adapter/ConstantAdapter.php +++ b/src/Adapter/ConstantAdapter.php @@ -21,7 +21,12 @@ public function __invoke(mixed $value): mixed $match = new FunctionExpressionMatcher(); - foreach ($match('constant', $value) as $expression) { + $expressions = $match('constant', $value); + if ([] === $expressions) { + return $value; + } + + foreach ($expressions as $expression) { $evaluated = $this->expressionLanguage->evaluate($expression); // the evaluation did not end up with a transformation diff --git a/src/Adapter/DateTimeAdapter.php b/src/Adapter/DateTimeAdapter.php index 99b2f4a..9581925 100644 --- a/src/Adapter/DateTimeAdapter.php +++ b/src/Adapter/DateTimeAdapter.php @@ -34,7 +34,12 @@ public function __invoke(mixed $value): mixed $match = new FunctionExpressionMatcher(); - foreach ($match('datetime(_immutable)?', $value) as $expression) { + $expressions = $match('datetime(_immutable)?', $value); + if ([] === $expressions) { + return $value; + } + + foreach ($expressions as $expression) { // surround named parameters arguments by double quotes // so that the named parameter part is not interpreted by the expression language // ex. 'format: "Y-m-d"' will be transformed to '"format: \"Y-m-d\""' diff --git a/src/Adapter/EnumAdapter.php b/src/Adapter/EnumAdapter.php index 555b3cd..b7a70ce 100644 --- a/src/Adapter/EnumAdapter.php +++ b/src/Adapter/EnumAdapter.php @@ -21,7 +21,12 @@ public function __invoke(mixed $value): mixed $match = new FunctionExpressionMatcher(); - foreach ($match('enum', $value) as $expression) { + $expressions = $match('enum', $value); + if ([] === $expressions) { + return $value; + } + + foreach ($expressions as $expression) { try { $evaluated = $this->expressionLanguage->evaluate($expression); } catch (\Throwable $exception) { diff --git a/src/Adapter/FactoryAdapter.php b/src/Adapter/FactoryAdapter.php index 03788c1..3bed4c4 100644 --- a/src/Adapter/FactoryAdapter.php +++ b/src/Adapter/FactoryAdapter.php @@ -29,7 +29,12 @@ public function __invoke(mixed $value): mixed $match = new FunctionExpressionMatcher(); - foreach ($match('factory', $value) as $expression) { + $expressions = $match('factory', $value); + if ([] === $expressions) { + return $value; + } + + foreach ($expressions as $expression) { $evaluated = $this->expressionLanguage->evaluate($expression); if ("<$expression>" === $value) { return $evaluated;