gekkone/table-data-accessors

A common interface for accessing table data. Contains implementations for csv and google spreadsheets

1.1.1 2024-01-24 10:11 UTC

This package is auto-updated.

Last update: 2024-04-24 10:53:27 UTC


README

A common interface for accessing table data on PHP. Contains implementations for csv and google spreadsheets

Details

An extended iterator interface is provided to access tabular data.

src/TableIteratorInterface.php line 7:11
interface TableIteratorInterface extends Iterator
{
    public function fetchRowCount(bool $skipEmpty = false): int;
    public function currentField(string $field, $default = null);
}

⚠️ Attention: The data of the first row is used as keys in the associative array returned for each of the rows

Code Examples

Simple read from csv file:

use Gekkone\TdaLib\Accessor\Csv;
use GuzzleHttp\Psr7\Stream;

$accessor = new Csv(
    Csv\TableOptions::new(new Stream(fopen(__DIR__ . '/filename.csv', 'r')))
);

foreach ($accessor as $row => $fields) {
    // $fields = ['columnHeader' => mixed, ...] or [(int) columnIndex => mixed, ...]
}

Simple read from Google Sheet

use Gekkone\TdaLib\Accessor\GoogleSheet;
use Google\Client;
use Google\Service\Sheets;

$googleCline = new Client([
    'scopes' => Sheets::SPREADSHEETS_READONLY
]);

// for read first sheet
$accessor = new GoogleSheet(
    GoogleSheet\TableOptions::new(new Sheets($googleCline), 'spreadsheetId')
);

// for read concreate sheet set sheetId, find it in url after '#gid=',
// example https://docs.google.com/spreadsheets/d/<spreadsheetID>/edit#gid=1737163713)
$accessor = new GoogleSheet(
    GoogleSheet\TableOptions::new(new Sheets($googleCline), 'spreadsheetId', 1737163713)
);

foreach ($accessor as $row => $fields) {
    // $fields = ['columnHeader' => mixed, ...] or [(int) columnIndex => mixed, ...]
}

Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License