From 0f17c5a610b4eab59d7faa8120c40ba8f2b3b388 Mon Sep 17 00:00:00 2001 From: Eric GELOEN Date: Thu, 3 Oct 2024 18:57:57 +0200 Subject: [PATCH 1/2] Fix matching for '@array@||@null@' --- src/Factory/MatcherFactory.php | 9 +++++---- src/Matcher/ChainMatcher.php | 5 +++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Factory/MatcherFactory.php b/src/Factory/MatcherFactory.php index 3bcd34f6..19039b8b 100644 --- a/src/Factory/MatcherFactory.php +++ b/src/Factory/MatcherFactory.php @@ -49,14 +49,12 @@ private function buildMatchers(Parser $parser, Backtrace $backtrace) : Matcher\C private function buildArrayMatcher(Matcher\ChainMatcher $scalarMatchers, Parser $parser, Backtrace $backtrace) : Matcher\ArrayMatcher { - $orMatcher = new Matcher\OrMatcher($backtrace, $scalarMatchers); - - return new Matcher\ArrayMatcher( + $arrayMatcher = new Matcher\ArrayMatcher( new Matcher\ChainMatcher( 'array', $backtrace, [ - $orMatcher, + new Matcher\OrMatcher($backtrace, $orMatchers = clone $scalarMatchers), $scalarMatchers, new Matcher\TextMatcher($backtrace, $parser), ] @@ -64,6 +62,9 @@ private function buildArrayMatcher(Matcher\ChainMatcher $scalarMatchers, Parser $backtrace, $parser ); + $orMatchers->registerMatcher($arrayMatcher); + + return $arrayMatcher; } private function buildScalarMatchers(Parser $parser, Backtrace $backtrace) : Matcher\ChainMatcher diff --git a/src/Matcher/ChainMatcher.php b/src/Matcher/ChainMatcher.php index 714ffb18..ff3123d6 100644 --- a/src/Matcher/ChainMatcher.php +++ b/src/Matcher/ChainMatcher.php @@ -36,6 +36,11 @@ public function __construct(string $name, Backtrace $backtrace, array $matchers $this->name = $name; } + public function registerMatcher(ValueMatcher $matcher) : void + { + $this->matchers[] = $matcher; + } + public function match($value, $pattern) : bool { $this->backtrace->matcherEntrance($this->matcherName(), $value, $pattern); From ce67ab12df98f0ec7ed5c761d2d42410ec3e9cb2 Mon Sep 17 00:00:00 2001 From: Eric GELOEN Date: Sun, 6 Oct 2024 20:17:07 +0200 Subject: [PATCH 2/2] Add test for '@array@||@null@' support --- tests/Matcher/OrMatcherTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/Matcher/OrMatcherTest.php b/tests/Matcher/OrMatcherTest.php index bf6aa9ba..a87a6dd3 100644 --- a/tests/Matcher/OrMatcherTest.php +++ b/tests/Matcher/OrMatcherTest.php @@ -65,6 +65,14 @@ public static function positiveMatchData() 'test' => '@integer@||@null@', ], ], + [ + [ + 'test' => [], + ], + [ + 'test' => '@array@||@null@', + ], + ], [ [ 'first_level' => ['second_level', ['third_level']],