BatchClosureDivisor.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. namespace Guzzle\Batch;
  3. use Guzzle\Common\Exception\InvalidArgumentException;
  4. /**
  5. * Divides batches using a callable
  6. */
  7. class BatchClosureDivisor implements BatchDivisorInterface
  8. {
  9. /** @var callable Method used to divide the batches */
  10. protected $callable;
  11. /** @var mixed $context Context passed to the callable */
  12. protected $context;
  13. /**
  14. * @param callable $callable Method used to divide the batches. The method must accept an \SplQueue and return an
  15. * array of arrays containing the divided items.
  16. * @param mixed $context Optional context to pass to the batch divisor
  17. *
  18. * @throws InvalidArgumentException if the callable is not callable
  19. */
  20. public function __construct($callable, $context = null)
  21. {
  22. if (!is_callable($callable)) {
  23. throw new InvalidArgumentException('Must pass a callable');
  24. }
  25. $this->callable = $callable;
  26. $this->context = $context;
  27. }
  28. public function createBatches(\SplQueue $queue)
  29. {
  30. return call_user_func($this->callable, $queue, $this->context);
  31. }
  32. }