CakeFest 2024: The Official CakePHP Conference

La clase CallbackFilterIterator

(PHP 5 >= 5.4.0, PHP 7, PHP 8)

Introducción

Sinopsis de la Clase

class CallbackFilterIterator extends FilterIterator {
/* Métodos */
public __construct(Iterator $iterator, callable $callback)
public accept(): bool
/* Métodos heredados */
}

Ejemplos

La llamada de retorno debería aceptar hasta tres argumentos: el elemento actual, la clave actual y el iterador, respectivamente.

Ejemplo #1 Argumentos disponibles de la llamada de retorno

<?php

/**
* Llamada de retorno para CallbackFilterIterator
*
* @param $current Valor del elemento actual
* @param $key Clave del elemento actual
* @param $iterator Iterador a filtrar
* @return boolean TRUE para aceptar el elemento actual, de lo contrario FALSE
*/
function my_callback($current, $key, $iterator) {
// Aquí el código de filtrado
}

?>

Se posría usar algún callable,como un string que contenga nombre de función, un array para un método, o una función anónima.

Ejemplo #2 Ejemplos básicos de llamada de retorno

<?php

$dir
= new FilesystemIterator(__DIR__);

// Filtrar ficheros de gran tamaño ( > 100MB)
function is_large_file($current) {
return
$current->isFile() && $current->getSize() > 104857600;
}
$large_files = new CallbackFilterIterator($dir, 'is_large_file');

// Filtrar directorios
$files = new CallbackFilterIterator($dir, function ($current, $key, $iterator) {
return
$current->isDir() && ! $iterator->isDot();
});

?>

Tabla de contenidos

add a note

User Contributed Notes 3 notes

up
10
dave1010 at gmail dot com
11 years ago
Implementation for PHP < 5.4:

<?php
if (!class_exists('CallbackFilterIterator')) {
class
CallbackFilterIterator extends FilterIterator {
protected
$callback;

// "Closure" type hint should be "callable" in PHP 5.4
public function __construct(Iterator $iterator, Closure $callback = null) {
$this->callback = $callback;
parent::__construct($iterator);
}

public function
accept() {
return
call_user_func(
$this->callback,
$this->current(),
$this->key(),
$this->getInnerIterator()
);
}
}
}

?>
up
2
asadi dot jabar at outlook dot com
5 years ago
CallbackFilterIterator class didnot implement OuterIterator interface directly

below is the correct hierarchy

CallbackFilterIterator
|__FilterIterator
|___ IteratorIterator
|____ OuterIterator

please fix the documentation
up
1
tunaabutbul[plus]php[at]gmail[dot]com
6 years ago
i PHP 7 you can use Anonymous classes to get the same results.

<?php

class MyIterator implements Iterator
{
/**
* @var \Iterator
*/
protected $next;

/**
* Collection constructor.
*
* @param \Iterator $next
*/
public function __construct(Iterator $next)
{
$this->next = $next;
}

/**
* @param callable $callback
*
* @return static
*/
public function filter(callable $callback = null)
{
return new static(new class(
$this, $callback) extends FilterIterator
{
protected
$callback;

public function
__construct(\Iterator $iterator, callable $callback = null)
{
parent::__construct($iterator);

$this->callback = $callback ?: function($current) {
return ! empty(
$current);
};;
}

public function
accept()
{
return
call_user_func($this->callback, parent::accept());
}
});
}
// Iterator methods ...
}
// .......
?>
To Top