FileExtensionFilterIterator

Class

A filter iterator that only accepts files matching specified extensions.

Description

This iterator is designed to traverse a directory and filter files by their extension. It supports both FilesystemIterator and RecursiveIterator.

Usage Example:

Tags
example

Description

use FastForward\Iterator\FileExtensionFilterIterator;
use FilesystemIterator;

$directory = new FilesystemIterator('/path/to/directory');

$iterator = new FileExtensionFilterIterator($directory, 'txt', 'php');

foreach ($iterator as $file) {
    echo $file->getFilename(); // Outputs only .txt and .php files
}

Note: If a RecursiveIterator is provided, the iterator will traverse subdirectories.

since
1.0.0

Table of Contents

Properties

$extensions

The file extensions to accept.

 : array<string|int, string>

Methods

__construct()

Initializes the FileExtensionFilterIterator.

 : mixed
accept()

Determines whether the current file should be accepted.

 : bool
count()

Counts the number of elements exposed by the inner iterator.

 : int
Properties
Methods

__construct()

Public

Initializes the FileExtensionFilterIterator.

public __construct(FilesystemIterator|RecursiveDirectoryIterator  $iterator, string  ...$extensions) : mixed
Parameters
$iterator : FilesystemIterator|RecursiveDirectoryIterator

Description

the directory iterator to wrap

$extensions : string

Description

The allowed file extensions.

accept()

Public

Determines whether the current file should be accepted.

public accept() : bool

Description

  • Directories are accepted only if the underlying iterator is recursive.
  • Files are accepted only if their extension matches the allowed list.
Return values
bool

Description

true if the file is accepted, false otherwise

count()

Public

Counts the number of elements exposed by the inner iterator.

public count() : int

Description

If the inner iterator implements Countable, this method SHALL return the value provided by that implementation. Otherwise, it MUST count elements by iterating over the iterator. If the inner iterator is not cloneable, this method SHALL wrap the current object in an IteratorIterator instance and count through that wrapper to avoid performing an invalid clone operation. If the inner iterator is cloneable, this method SHOULD count over a clone so that the original iterator state is preserved as much as possible.

Return values
int

Description

the total number of elements available from the inner iterator