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