Each.php 2.8 KB

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