From 3d71c31571b8c1d78f4884f714eac0713434b8fa Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 5 Jan 2021 23:55:00 +0100 Subject: [PATCH 1/2] added Callback::invokeAll() --- src/Utils/Callback.php | 13 ++++++++++ tests/Utils/Callback.invokeAll.phpt | 38 +++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 tests/Utils/Callback.invokeAll.phpt diff --git a/src/Utils/Callback.php b/src/Utils/Callback.php index 73d5cecd9..4631413d4 100644 --- a/src/Utils/Callback.php +++ b/src/Utils/Callback.php @@ -35,6 +35,19 @@ public static function closure($callable, string $method = null): \Closure } + /** + * Invokes all callables. + * @param callable[] $callables + */ + public static function invokeAll(array $callables, ...$args): array + { + foreach ($callables as $k => $cb) { + $callables[$k] = $cb(...$args); + } + return $callables; + } + + /** * Invokes callback. * @return mixed diff --git a/tests/Utils/Callback.invokeAll.phpt b/tests/Utils/Callback.invokeAll.phpt new file mode 100644 index 000000000..2edab06dc --- /dev/null +++ b/tests/Utils/Callback.invokeAll.phpt @@ -0,0 +1,38 @@ + Date: Thu, 7 Jan 2021 21:15:30 +0100 Subject: [PATCH 2/2] Arrays: added updateDiff() (#246) --- src/Utils/Arrays.php | 15 ++++++ tests/Utils/Arrays.updateDiff().phpt | 71 ++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 tests/Utils/Arrays.updateDiff().phpt diff --git a/src/Utils/Arrays.php b/src/Utils/Arrays.php index ffbb7cb0e..00704ed40 100644 --- a/src/Utils/Arrays.php +++ b/src/Utils/Arrays.php @@ -343,4 +343,19 @@ public static function toKey($value) { return key([$value => null]); } + + + /** + * Returns items required to synchronize associative array $from to array $to. + */ + public static function updateDiff(array $from, array $to): array + { + $diff = []; + foreach ($to as $k => $v) { + if (!array_key_exists($k, $from) || $v !== $from[$k]) { + $diff[$k] = $v; + } + } + return $diff; + } } diff --git a/tests/Utils/Arrays.updateDiff().phpt b/tests/Utils/Arrays.updateDiff().phpt new file mode 100644 index 000000000..83123f0d1 --- /dev/null +++ b/tests/Utils/Arrays.updateDiff().phpt @@ -0,0 +1,71 @@ + ''], [])); +}); + + +test('New keys', function () { + $to = [ + 'a' => null, + 'b' => false, + 'c' => '', + 'd' => 0, + ]; + Assert::same($to, Arrays::updateDiff([], $to)); +}); + + +test('To falsy values', function () { + $from = [ + 'a' => null, + 'b' => false, + 'c' => '', + 'd' => 0, + ]; + + $toNull = ['a' => null, 'b' => null, 'c' => null, 'd' => null]; + Assert::same([ + 'b' => null, + 'c' => null, + 'd' => null, + ], Arrays::updateDiff($from, $toNull)); + + + $toFalse = ['a' => false, 'b' => false, 'c' => false, 'd' => false]; + Assert::same([ + 'a' => false, + 'c' => false, + 'd' => false, + ], Arrays::updateDiff($from, $toFalse)); + + + $toEmpty = ['a' => '', 'b' => '', 'c' => '', 'd' => '']; + Assert::same([ + 'a' => '', + 'b' => '', + 'd' => '', + ], Arrays::updateDiff($from, $toEmpty)); + + + $toZero = ['a' => 0, 'b' => 0, 'c' => 0, 'd' => 0]; + Assert::same([ + 'a' => 0, + 'b' => 0, + 'c' => 0, + ], Arrays::updateDiff($from, $toZero)); +});