PHP code example of tii / csv-state-parser

1. Go to this page and download the library: Download tii/csv-state-parser 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/ */

    

tii / csv-state-parser example snippets


/**
 * @extends \Tii\CsvStateParser\CsvStateParser<array<int, int>> 
 */
class SumParser extends \Tii\CsvStateParser\CsvStateParser
{

    protected array $sums = [];

    protected function result(): array
    {
        return $this->sums;
    }
    
    protected function stateStart(array $row): void
    {
        if ($row[0] === 'START') {
            $this->state('list');
        }
    }
    
    protected function stateList(array $row): void
    {
        if ($row[0] === 'END') {
            $this->done();
            return;
        }
        
        $this->items[] = array_reduce($row, fn($sum, $number) => $sum + $number, 0);
    }

}

$parser = new SumParser(separator: ',');
$list = $parser->parse('filename.csv');