| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Plausible\Analytics\WP\Client\Lib\GuzzleHttp\Promise; |
| 6 |
|
| 7 |
final class Each |
| 8 |
{ |
| 9 |
/** |
| 10 |
* Given an iterator that yields promises or values, returns a promise that |
| 11 |
* is fulfilled with a null value when the iterator has been consumed or |
| 12 |
* the aggregate promise has been fulfilled or rejected. |
| 13 |
* |
| 14 |
* $onFulfilled is a function that accepts the fulfilled value, iterator |
| 15 |
* index, and the aggregate promise. The callback can invoke any necessary |
| 16 |
* side effects and choose to resolve or reject the aggregate if needed. |
| 17 |
* |
| 18 |
* $onRejected is a function that accepts the rejection reason, iterator |
| 19 |
* index, and the aggregate promise. The callback can invoke any necessary |
| 20 |
* side effects and choose to resolve or reject the aggregate if needed. |
| 21 |
* |
| 22 |
* @param mixed $iterable Iterator or array to iterate over. |
| 23 |
* @param callable $onFulfilled |
| 24 |
* @param callable $onRejected |
| 25 |
*/ |
| 26 |
public static function of( |
| 27 |
$iterable, |
| 28 |
?callable $onFulfilled = null, |
| 29 |
?callable $onRejected = null |
| 30 |
): PromiseInterface { |
| 31 |
return (new EachPromise($iterable, [ |
| 32 |
'fulfilled' => $onFulfilled, |
| 33 |
'rejected' => $onRejected, |
| 34 |
]))->promise(); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Like of, but only allows a certain number of outstanding promises at any |
| 39 |
* given time. |
| 40 |
* |
| 41 |
* $concurrency may be an integer or a function that accepts the number of |
| 42 |
* pending promises and returns a numeric concurrency limit value to allow |
| 43 |
* for dynamic a concurrency size. |
| 44 |
* |
| 45 |
* @param mixed $iterable |
| 46 |
* @param int|callable $concurrency |
| 47 |
* @param callable $onFulfilled |
| 48 |
* @param callable $onRejected |
| 49 |
*/ |
| 50 |
public static function ofLimit( |
| 51 |
$iterable, |
| 52 |
$concurrency, |
| 53 |
?callable $onFulfilled = null, |
| 54 |
?callable $onRejected = null |
| 55 |
): PromiseInterface { |
| 56 |
return (new EachPromise($iterable, [ |
| 57 |
'fulfilled' => $onFulfilled, |
| 58 |
'rejected' => $onRejected, |
| 59 |
'concurrency' => $concurrency, |
| 60 |
]))->promise(); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Like limit, but ensures that no promise in the given $iterable argument |
| 65 |
* is rejected. If any promise is rejected, then the aggregate promise is |
| 66 |
* rejected with the encountered rejection. |
| 67 |
* |
| 68 |
* @param mixed $iterable |
| 69 |
* @param int|callable $concurrency |
| 70 |
* @param callable $onFulfilled |
| 71 |
*/ |
| 72 |
public static function ofLimitAll( |
| 73 |
$iterable, |
| 74 |
$concurrency, |
| 75 |
?callable $onFulfilled = null |
| 76 |
): PromiseInterface { |
| 77 |
return self::ofLimit( |
| 78 |
$iterable, |
| 79 |
$concurrency, |
| 80 |
$onFulfilled, |
| 81 |
function ($reason, $idx, PromiseInterface $aggregate): void { |
| 82 |
$aggregate->reject($reason); |
| 83 |
} |
| 84 |
); |
| 85 |
} |
| 86 |
} |
| 87 |
|