The FilterIterator class
(PHP 5 >= 5.1.0)
Introduction
This abstract iterator filters out unwanted values. This class should be extended to implement custom iterator filters. The FilterIterator::accept() must be implemented in the subclass.
Class synopsis
/* Methods */
}Table of Contents
- FilterIterator::accept — Check whether the current element of the iterator is acceptable
- FilterIterator::__construct — Construct a filterIterator
- FilterIterator::current — Get the current element value
- FilterIterator::getInnerIterator — Get the inner iterator
- FilterIterator::key — Get the current key
- FilterIterator::next — Move the iterator forward
- FilterIterator::rewind — Rewind the iterator
- FilterIterator::valid — Check whether the current element is valid
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Другие базовые расширения
- Стандартная библиотека PHP (SPL)
- Класс AppendIterator
- Класс ArrayIterator
- Класс CachingIterator
- Класс CallbackFilterIterator
- Класс DirectoryIterator
- Класс EmptyIterator
- Класс FilesystemIterator
- Класс FilterIterator
- Класс GlobIterator
- Класс InfiniteIterator
- Класс IteratorIterator
- Класс LimitIterator
- Класс MultipleIterator
- Класс NoRewindIterator
- Класс ParentIterator
- Класс RecursiveArrayIterator
- Класс RecursiveCachingIterator
- Класс RecursiveCallbackFilterIterator
- Класс RecursiveDirectoryIterator
- Класс RecursiveFilterIterator
- Класс RecursiveIteratorIterator
- Класс RecursiveRegexIterator
- Класс RecursiveTreeIterator
- Класс RegexIterator
Коментарии
The code below is a simple example of usage . Note that the method which does the actual job is accept.
<?php
class UserFilter extends FilterIterator
{
private $userFilter;
public function __construct(Iterator $iterator , $filter )
{
parent::__construct($iterator);
$this->userFilter = $filter;
}
public function accept()
{
$user = $this->getInnerIterator()->current();
if( strcasecmp($user['name'],$this->userFilter) == 0) {
return false;
}
return true;
}
}
$array = array(
array('name' => 'Jonathan','id' => '5'),
array('name' => 'Abdul' ,'id' => '22')
);
$object = new ArrayObject($array);
// Note it is case insensitive check in our example due the usage of strcasecmp function
$iterator = new UserFilter($object->getIterator(),'abdul');
foreach ($iterator as $result) {
echo $result['name'];
}
/* Outputs Jonathan */
?>
Regards.
A little test about the function call order:
<?php
class TestIterator extends IteratorIterator
{
public function key()
{
echo __FUNCTION__, PHP_EOL;
return parent::key();
}
public function next()
{
echo __FUNCTION__, PHP_EOL;
return parent::next();
}
public function rewind()
{
echo __FUNCTION__, PHP_EOL;
return parent::rewind();
}
public function valid()
{
echo __FUNCTION__, PHP_EOL;
return parent::valid();
}
}
class TestFilterIterator extends FilterIterator
{
public function accept()
{
echo __FUNCTION__, PHP_EOL;
return true;
}
}
$iterator = new ArrayIterator(array('a', 'b', 'c'));
foreach (new TestFilterIterator(new TestIterator($iterator)) as $k => $v) {
echo PHP_EOL;
}
?>
This will output the following:
rewind
valid
key
accept
next
valid
key
accept
next
valid
key
accept
next
valid
Filter object collection by method:
<?php
/**
* @method object current()
*/
class CollectionFilterIterator extends FilterIterator
{
private $methodName;
private $methodArguments;
public function function __construct(Iterator $collection, string $methodName, array $methodArguments = [])
{
parent::__construct($collection);
$this->methodName = $methodName;
$this->methodArguments = array_values($methodArguments);
}
public function accept(): bool
{
return (bool) $this->current()->{$this->methodName}(...$this->methodArguments);
// or call_user_func_array([$this->current(), $this->methodName], $this->methodArguments);
}
}
?>