Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arrays::updateDiff() #246

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/Utils/Arrays.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
13 changes: 13 additions & 0 deletions src/Utils/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
71 changes: 71 additions & 0 deletions tests/Utils/Arrays.updateDiff().phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/**
* Test: Nette\Utils\Arrays::updateDiff()
*/

declare(strict_types=1);

use Nette\Utils\Arrays;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


test('Basics', function () {
Assert::same([], Arrays::updateDiff([], []));
Assert::same([], Arrays::updateDiff(['a' => ''], []));
});


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));
});
38 changes: 38 additions & 0 deletions tests/Utils/Callback.invokeAll.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/**
* Test: Nette\Utils\Callback::invokeAll()
*/

declare(strict_types=1);

use Nette\Utils\Callback;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


class Test
{
public function fn1(...$args)
{
return __METHOD__ . ' ' . implode(',', $args);
}


public function fn2(...$args)
{
return __METHOD__ . ' ' . implode(',', $args);
}
}


$list = [];
$list[] = [new Test, 'fn1'];
$list[] = [new Test, 'fn2'];

Assert::same(
['Test::fn1 a,b', 'Test::fn2 a,b'],
Callback::invokeAll($list, 'a', 'b')
);