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:
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.
Properties
The file extensions to accept.
Methods
Initializes the FileExtensionFilterIterator.
Determines whether the current file should be accepted.
Counts the number of elements exposed by the inner iterator.
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.
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
Description
true if the file is accepted, false otherwise
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
Description
the total number of elements available from the inner iterator