Classes List

CallbackFilterIterator

 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 42 
 43 
 44 
 45 
 46 
 47 
 48 
 49 
 50 
 51 
 52 
 53 
 54 
 55 
 56 
 57 
 58 
 59 
 60 
 61 
 62 
 63 
 64 
 65 
 66 
 67 
 68 
 69 
 70 
 71 
 72 
 73 
 74 
 75 
 76 
 77 
 78 
 79 
<?php
/** * Part of the Joomla Framework Compat Package * * @copyright Copyright (C) 2005 - 2014 Open Source Matters, Inc. All rights reserved. * @license GNU General Public License version 2 or later; see LICENSE */

/** * CallbackFilterIterator using the callback to determine which items are accepted or rejected. * * @link http://php.net/manual/en/class.callbackfilteriterator.php * @since 1.2.0 */
class CallbackFilterIterator extends \FilterIterator
{
/** * The callback to check value. * * @var callable * * @since 1.2.0 */
protected $callback = null;

/** * Creates a filtered iterator using the callback to determine * which items are accepted or rejected. * * @param \Iterator $iterator The iterator to be filtered. * @param callable $callback The callback, which should return TRUE to accept the current item * or FALSE otherwise. May be any valid callable value. * The callback should accept up to three arguments: the current item, * the current key and the iterator, respectively. * ``` php * function my_callback($current, $key, $iterator) * ``` * * @throws InvalidArgumentException * * @since 1.2.0 */
public function __construct(\Iterator $iterator$callback)
{
if (!is_callable($callback))
{
throw new \InvalidArgumentException("Argument 2 of CallbackFilterIterator should be callable.");
}

$this->callback = $callback;

parent::__construct($iterator);
}

/** * This method calls the callback with the current value, current key and the inner iterator. * The callback is expected to return TRUE if the current item is to be accepted, or FALSE otherwise. * * @link http://www.php.net/manual/en/callbackfilteriterator.accept.php * * @return boolean True if the current element is acceptable, otherwise false. * * @since 1.2.0 */
public function accept()
{
$inner = $this->getInnerIterator();

return call_user_func_array(
$this->callback,
array(
$inner->current(),
$inner->key(),
$inner
)
);
}
}