dbt / table
Objects to represent fixed-length tabular data
Installs: 868
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Type:project
Requires
- php: >=7.4
- ext-json: *
Requires (Dev)
- ext-pcov: *
- friendsofphp/php-cs-fixer: ^2.15
- larapack/dd: ^1.1
- phpunit/phpunit: ^9.0
- vimeo/psalm: ^4.7.3
README
A set of objects to represent tabular data with fixed-width rows. Values are represented with strings.
Installation
You can install the package via composer:
composer require dbt/table
Usage
Create a new table:
use Dbt\Table\Table; use Dbt\Table\Row; use Dbt\Table\Cell; /** * Standard construction. */ $table = new Table( new Row( new Cell('row 0 / cell 0'), new Cell('row 0 / cell 1') ), new Row( new Cell('row 1 / cell 0'), new Cell('row 1 / cell 1') ) ); /** * From array. */ $table = Table::fromArray([ ['row 0 / cell 0', 'row 0 / cell 1'], ['row 1 / cell 0', 'row 1 / cell 1'], ]); /* * Rows must have the same length. This will throw a LengthException: */ $table = Table::fromArray([ ['row 0, cell 0', 'row 0, cell 1'], ['row 1, cell 0'], // Too short! ]);
Tables and rows are list objects that can be iterated over and pushed to:
use Dbt\Table\Table; use Dbt\Table\Row; use Dbt\Table\Cell; $table = Table::fromArray([['row 0 / cell 0'], ['row 1 / cell 0']]); /** * @var int $rowIndex * @var \Dbt\Table\Row $row */ foreach ($table as $rowIndex => $row) { var_dump(count($table), count($row)); /** * @var int $cellIndex * @var \Dbt\Table\Cell $cell */ foreach ($row as $cellIndex => $cell) { var_dump($cell->value()); } } $table->push(new Row(new Cell('row 2 / cell 0')));
Cells can be created from scalars or arrays of scalars:
use \Dbt\Table\Cell; /** * This int will be cast to a string. */ $cell = Cell::make(1); /** * This array will be serialized to a JSON string. */ $cell = Cell::make(['string', 1, 9.9, true]);
Tables, Rows, and Cells are not intended to be modified after being set. Though you can push to a Table and a Row, you cannot modify existing values.
Etc.
Please see CONTRIBUTING for details. The MIT License (MIT). Please see License File for more information.