| @@ -1,6 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | +declare (strict_types=1); | |
| 3 | 4 | namespace YoastSEO_Vendor\GuzzleHttp\Promise; |
| 4 | 5 | |
| 5 | 6 | final class Each |
| 6 | 7 | { |
| @@ -16,16 +17,13 @@ | ||
| 16 | 17 | * $onRejected is a function that accepts the rejection reason, iterator |
| 17 | 18 | * index, and the aggregate promise. The callback can invoke any necessary |
| 18 | 19 | * side effects and choose to resolve or reject the aggregate if needed. |
| 19 | 20 | * |
| 20 | - * @param mixed $iterable Iterator or array to iterate over. | |
| 21 | - * @param callable $onFulfilled | |
| 22 | - * @param callable $onRejected | |
| 23 | - * | |
| 24 | - * @return PromiseInterface | |
| 21 | + * @param mixed $iterable Iterator or array to iterate over. | |
| 25 | 22 | */ |
| 26 | - public static function of($iterable, callable $onFulfilled = null, callable $onRejected = null) | |
| 23 | + public static function of($iterable, ?callable $onFulfilled = null, ?callable $onRejected = null) : \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface | |
| 27 | 24 | { |
| 25 | + $iterable = self::prepareIterable($iterable, __FUNCTION__); | |
| 28 | 26 | return (new \YoastSEO_Vendor\GuzzleHttp\Promise\EachPromise($iterable, ['fulfilled' => $onFulfilled, 'rejected' => $onRejected]))->promise(); |
| 29 | 27 | } |
| 30 | 28 | /** |
| 31 | 29 | * Like of, but only allows a certain number of outstanding promises at any |
| @@ -36,15 +34,12 @@ | ||
| 36 | 34 | * for dynamic a concurrency size. |
| 37 | 35 | * |
| 38 | 36 | * @param mixed $iterable |
| 39 | 37 | * @param int|callable $concurrency |
| 40 | - * @param callable $onFulfilled | |
| 41 | - * @param callable $onRejected | |
| 42 | - * | |
| 43 | - * @return PromiseInterface | |
| 44 | 38 | */ |
| 45 | - public static function ofLimit($iterable, $concurrency, callable $onFulfilled = null, callable $onRejected = null) | |
| 39 | + public static function ofLimit($iterable, $concurrency, ?callable $onFulfilled = null, ?callable $onRejected = null) : \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface | |
| 46 | 40 | { |
| 41 | + $iterable = self::prepareIterable($iterable, __FUNCTION__); | |
| 47 | 42 | return (new \YoastSEO_Vendor\GuzzleHttp\Promise\EachPromise($iterable, ['fulfilled' => $onFulfilled, 'rejected' => $onRejected, 'concurrency' => $concurrency]))->promise(); |
| 48 | 43 | } |
| 49 | 44 | /** |
| 50 | 45 | * Like limit, but ensures that no promise in the given $iterable argument |
| @@ -52,15 +47,21 @@ | ||
| 52 | 47 | * rejected with the encountered rejection. |
| 53 | 48 | * |
| 54 | 49 | * @param mixed $iterable |
| 55 | 50 | * @param int|callable $concurrency |
| 56 | - * @param callable $onFulfilled | |
| 57 | - * | |
| 58 | - * @return PromiseInterface | |
| 59 | 51 | */ |
| 60 | - public static function ofLimitAll($iterable, $concurrency, callable $onFulfilled = null) | |
| 52 | + public static function ofLimitAll($iterable, $concurrency, ?callable $onFulfilled = null) : \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface | |
| 61 | 53 | { |
| 62 | - return each_limit($iterable, $concurrency, $onFulfilled, function ($reason, $idx, \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface $aggregate) { | |
| 54 | + $iterable = self::prepareIterable($iterable, __FUNCTION__); | |
| 55 | + return self::ofLimit($iterable, $concurrency, $onFulfilled, function ($reason, $idx, \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface $aggregate) : void { | |
| 63 | 56 | $aggregate->reject($reason); |
| 64 | 57 | }); |
| 58 | + } | |
| 59 | + private static function prepareIterable($iterable, string $method) : iterable | |
| 60 | + { | |
| 61 | + if (\is_iterable($iterable)) { | |
| 62 | + return $iterable; | |
| 63 | + } | |
| 64 | + \YoastSEO_Vendor\trigger_deprecation('guzzlehttp/promises', '2.5', 'Passing a non-iterable to %s::%s() is deprecated; guzzlehttp/promises 3.0 will require an iterable.', self::class, $method); | |
| 65 | + return [$iterable]; | |
| 65 | 66 | } |
| 66 | 67 | } |