This package is abandoned and no longer maintained. No replacement package was suggested.

File functionality.

0.4.0 2017-03-20 11:16 UTC

This package is not auto-updated.

Last update: 2022-08-20 15:48:55 UTC


README

To install the BlackwoodSeven file library in your project using Composer:

composer require blackwoodseven/file

Usage: CsvFileObject

use BlackwoodSeven\File\CsvFileObject;

$csvFile = new CsvFileObject('my-data.csv');
$data = iterator_to_array($csvFile);

$fields = $csvFile->getFields();
// or
$fields = array_keys(reset($data));

Usage: AtomicTempFileObject

use BlackwoodSeven\File\AtomicTempFileObject;

$newFile = new AtomicTempFileObject('my-output.csv');
$newFile->putcsv($fields);

foreach ($data as $row) {
    $newFile->putcsv($row);
}

$newfile->persistOnClose();
unset($newfile);

Usage: AtomicTempFileObjects

use BlackwoodSeven\File\CsvFileObject;
use BlackwoodSeven\File\AtomicTempFileObjects;

$inputFile = new CsvFileObject('my-input.csv');
$outputFiles = new AtomicTempFileObjects();

// Split a csv file into multiple files.
$outputFiles->splitCsvFile($inputFile, function (&$row) {
    // $row is by reference and can be modified.
    $row = mapTheRowAsIWantItToBe($row);

    // Return filename to use for the specific row.
    return 'my-output.' . $row['date'] . '.csv';
});

$outputFiles->persistOnClose();
unset($outputFiles);
use BlackwoodSeven\File\CsvFileObject;
use BlackwoodSeven\File\AtomicTempFileObjects;

$inputFile = new \SplFileObject('my-input.csv');
$outputFiles = new AtomicTempFileObjects();

// Split a file into two files containing odd and even lines.
$outputFiles->process($inputFile, function ($line, $lineNum, $input, $output) {
    $no = ($lineNum % 2);
    $fileName = 'my-output.' . ($no ? 'even' : 'odd') . '.txt';
    $file = $output->isFileOpen($fileName) ? $output->getFile($fileName) : $output->openFile($fileName);
    $file->fwrite($line);
});

$outputFiles->persistOnClose();
unset($outputFiles);