vendor/guzzlehttp/promises/src/Each.php

104 lines · 3328 bytes

  1<?php
  2
  3declare(strict_types=1);
  4
  5namespace GuzzleHttp\Promise;
  6
  7final 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     */
 24    public static function of(
 25        $iterable,
 26        ?callable $onFulfilled = null,
 27        ?callable $onRejected = null
 28    ): PromiseInterface {
 29        $iterable = self::prepareIterable($iterable, __FUNCTION__);
 30
 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     */
 48    public static function ofLimit(
 49        $iterable,
 50        $concurrency,
 51        ?callable $onFulfilled = null,
 52        ?callable $onRejected = null
 53    ): PromiseInterface {
 54        $iterable = self::prepareIterable($iterable, __FUNCTION__);
 55
 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     */
 71    public static function ofLimitAll(
 72        $iterable,
 73        $concurrency,
 74        ?callable $onFulfilled = null
 75    ): PromiseInterface {
 76        $iterable = self::prepareIterable($iterable, __FUNCTION__);
 77
 78        return self::ofLimit(
 79            $iterable,
 80            $concurrency,
 81            $onFulfilled,
 82            function ($reason, $idx, PromiseInterface $aggregate): void {
 83                $aggregate->reject($reason);
 84            }
 85        );
 86    }
 87
 88    private static function prepareIterable($iterable, string $method): iterable
 89    {
 90        if (is_iterable($iterable)) {
 91            return $iterable;
 92        }
 93
 94        \trigger_deprecation(
 95            'guzzlehttp/promises',
 96            '2.5',
 97            'Passing a non-iterable to %s::%s() is deprecated; guzzlehttp/promises 3.0 will require an iterable.',
 98            self::class,
 99            $method
100        );
101
102        return [$iterable];
103    }
104}