Skip to content

Commit

Permalink
added Callback::invokeAll()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jan 5, 2021
1 parent 745b477 commit 3d71c31
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
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
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')
);

0 comments on commit 3d71c31

Please sign in to comment.