From 2a124cfdd8985d443435fb0dbaf6bb1e9a6b9707 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=B0=D0=B2=D0=B5=D0=BB?= Date: Mon, 31 May 2021 13:25:30 +0300 Subject: [PATCH] `RecursiveLtrArrayComparator` improvement and test --- .../RecursiveLtrArrayComparator.php | 5 ++- .../RecursiveArrayLtrComparatorTest.php | 42 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 tests/src/Comparator/RecursiveArrayLtrComparatorTest.php diff --git a/src/Comparator/RecursiveLtrArrayComparator.php b/src/Comparator/RecursiveLtrArrayComparator.php index 94c8876..de67a5e 100644 --- a/src/Comparator/RecursiveLtrArrayComparator.php +++ b/src/Comparator/RecursiveLtrArrayComparator.php @@ -47,7 +47,10 @@ class RecursiveLtrArrayComparator extends RecursiveArrayComparator } foreach ($needle as $key => $value) { - if (is_array($value) && !self::recursiveCompareArrays($value, $haystack[$key])) { + if ( + is_array($value) && + (!is_array($haystack[$key]) || !self::recursiveCompareArrays($value, $haystack[$key])) + ) { return false; } diff --git a/tests/src/Comparator/RecursiveArrayLtrComparatorTest.php b/tests/src/Comparator/RecursiveArrayLtrComparatorTest.php new file mode 100644 index 0000000..c72a5bc --- /dev/null +++ b/tests/src/Comparator/RecursiveArrayLtrComparatorTest.php @@ -0,0 +1,42 @@ + [ + 'createdAtFrom' => '2020-01-01 00:00:00', + 'createdAtTo' => '2021-08-01 00:00:00', + ] + ]; + $haystack = [ + 'filter' => [ + 'createdAtFrom' => '2020-01-01 00:00:00', + 'createdAtTo' => '2021-08-01 00:00:00', + ], + 'test' => '' + ]; + + self::assertTrue(ComparatorLocator::get(RecursiveLtrArrayComparator::class)->compare($needle, $haystack)); + } +}