1: <?php
2:
3: namespace Guzzle\Iterator;
4:
5: use Guzzle\Common\Exception\InvalidArgumentException;
6:
7: /**
8: * Filters values using a callback
9: *
10: * Used when PHP 5.4's {@see \CallbackFilterIterator} is not available
11: */
12: class FilterIterator extends \FilterIterator
13: {
14: /**
15: * @var mixed Callback used for filtering
16: */
17: protected $callback;
18:
19: /**
20: * @param \Traversable $iterator Traversable iterator
21: * @param array|\Closure $callback Callback used for filtering. Return true to keep or false to filter.
22: *
23: * @throws InvalidArgumentException if the callback if not callable
24: */
25: public function __construct(\Traversable $iterator, $callback)
26: {
27: parent::__construct($iterator);
28: if (!is_callable($callback)) {
29: throw new InvalidArgumentException('The callback must be callable');
30: }
31: $this->callback = $callback;
32: }
33:
34: /**
35: * {@inheritdoc}
36: */
37: public function accept()
38: {
39: return call_user_func($this->callback, $this->current());
40: }
41: }
42: