dcarbone/file-object-plus

A simple extension of the PHP \SplFileObject class

0.4.0 2018-01-30 02:26 UTC

This package is auto-updated.

Last update: 2024-04-17 09:05:50 UTC


README

A simple extension of the PHP class SplFileObject

Build status: Build Status

Basics:

This class is a very simple extension of the base PHP SplFileObject. As such, it has all the same functionality as the base class with a few minor additions.

Countable Interface

I have implemented the Countable interface into this class. It utilizes my FileHelper helper class to determine the count

To use, simply execute:

$fileObject = new DCarbone\FileObjectPlus('myfile.txt');
$count = count($fileObject);
echo $count;

To count lines that contain a term, execute:

$fileObject = new DCarbone\FileObjectPlus('myfile.txt');
$count = $fileObject->countLinesContaining('my term');
echo $count;

Pagination

This class also implements some very simple pagination methods, modeled closely to how you would specify returning a portion of a database table.

To get a portion of a file irrespective of line content:

$fileObject = new DCarbone\FileObjectPlus('myfile.txt');
$offset = 0;
$limit = 25;
$lines = $fileObject->paginateLines($offset, $limit);
var_dump($lines);

By default, blank lines are also returned. You may alternatively ignore these by passing in 4 parameters:

$fileObject = new \DCarbone\FileObjectPlus('myfile.txt');
$offset = 0;
$limit = 25;
$search = null;
$includeEmpty = false;
$lines = $fileObject->paginateLines($offset, $limit, $search, $includeEmpty);
var_dump($lines);

If you wish to paginate through a file only matching lines that contain a certain term:

$fileObject = new \DCarbone\FileObjectPlus('myfile.txt');
$offset = 0;
$limit = 25;
$search = 'my term';
$lines = $fileObject->paginateLines($offset, $limit, $search);

Note: When searching, the fourth parameter is ignored

Note: Both pagination functions currently reset the underlying SplFileObject's internal line pointer.