CakeFest 2017 NYC, the Official CakePHP Conference

کلاس RecursiveFilterIterator

Introduction

این تکرار کننده انتزاعی مقدارهای ناخواسته RecursiveIterator را فیلتر می‌کند. این کلاس باید برای پیاده‌سازی فیلترهای تغییر یافته گسترش یابد. RecursiveFilterIterator::accept()() باید در زیرکلاس پیاده شود.

Class synopsis

RecursiveFilterIterator extends FilterIterator implements Iterator , Traversable , OuterIterator , RecursiveIterator {
/* Methods */
public void getChildren ( void )
public void hasChildren ( void )
/* Inherited methods */
abstract bool FilterIterator::accept ( void )
mixed FilterIterator::current ( void )
mixed FilterIterator::key ( void )
void FilterIterator::next ( void )
void FilterIterator::rewind ( void )
bool FilterIterator::valid ( void )
}

Table of Contents

add a note add a note

User Contributed Notes 2 notes

up
5
lemats
5 years ago
Recursive directory/file listing, filteres ".svn":

<?php

class MyRecursiveFilterIterator extends RecursiveFilterIterator {

    public static
$FILTERS = array(
       
'.svn',
    );

    public function
accept() {
        return !
in_array(
           
$this->current()->getFilename(),
           
self::$FILTERS,
           
true
       
);
    }

}

$dirItr    = new RecursiveDirectoryIterator('/sample/path');
$filterItr = new MyRecursiveFilterIterator($dirItr);
$itr       = new RecursiveIteratorIterator($filterItr, RecursiveIteratorIterator::SELF_FIRST);
foreach (
$itr as $filePath => $fileInfo) {
    echo
$fileInfo->getFilename() . PHP_EOL;
}

?>
up
1
Russell Dias
6 years ago
I had to create a filter to get values greater than a certain number in a multidimensional array.

Hope someone else finds it helpful:

<?php
class GreaterThanFilter extends RecursiveFilterIterator {
    protected
$limit;

    public function
__construct(RecursiveIterator $recursiveIter, $limit) {
       
$this->limit = $limit;
       
parent::__construct($recursiveIter);
    }
    public function
accept() {
        return
$this->hasChildren() || strlen($this->current()) >= $this->limit;
    }
     public function
getChildren() {
        return new
self($this->getInnerIterator()->getChildren(), $this->limit);
    }
}
$iter = new RecursiveArrayIterator($array);
$filter   = new GreaterThanFilter($iter, 5);

foreach(new
RecursiveIteratorIterator($filter) as $key => $value)
{
    echo
$value . "\n";
}
?>
To Top