PHP code example of iterator-tools / pipeline

1. Go to this page and download the library: Download iterator-tools/pipeline library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

iterator-tools / pipeline example snippets




use IteratorTools\Source\Csv\CsvReader;
use IteratorTools\Source\Csv\CsvReaderOptions;
use function IteratorTools\Iterator\pipeline;
use function IteratorTools\Consumers\int_sum;

$options = CsvReaderOptions::defaults()->withDateColumn('created_at', 'Y-m-d H:i');

// First you need an iterable source from which you can create a stream
$csv = CsvReader::from('some-file.csv', $options)->readAssoc();

// Some helper object
$deadline = new DateTime('2022-03-01 00:00');

// Here we create a "pipeline" using source Iterator
$total = pipeline($csv)
    // First we tell that we want to filter all rows by date
    // (no iteration is happening at this moment)
    ->filter(function (array $row) use ($deadline) {
        return $row['created_at'] >= $deadline;
    })
    // Then we tell that after filtering we want mapping to integer values
    // (still, no iteration happens here)
    ->map(fn($row) => (int)$row['count'])
    // And here we want to consume all integers
    // and compute an integer sum
    // (here $csvReader is consumed; one row from file
    // at a time is filtered, mapped and then summed)
    // This line returns integer value.
    // result: 70
    ->consume(int_sum());
csv
id,count,created_at
1,30,"2022-01-01 12:00"
2,10,"2022-02-01 12:00"
3,50,"2022-03-01 12:00"
4,20,"2022-04-01 12:00"