averay / csv
0.2.2
2025-05-27 03:08 UTC
Requires
- php: >=8.4
- league/csv: ^9.23
- symfony/string: ^7.2
- symfony/translation-contracts: ^3.5
Requires (Dev)
- phpunit/phpunit: ^12.1
- roave/security-advisories: dev-latest
- vimeo/psalm: ^6.11
README
Extensions to the League CSV library to support declaring CSV schemas used for reading & writing.
Installation
composer install averay/csv
Schemas
A schema can be defined in a standalone class implementing Averay\Csv\Schema\SchemaInterface
, or directly in an array on a Reader
or Writer
instance.
<?php use Averay\Csv\Reader; use Averay\Csv\Schema\Types; $reader = Reader::createFromPath('/path/to/csv'); $reader->setSchema([ 'id' => new Types\StringType(), 'label' => new Types\StringType(nullable: true), 'count' => new Types\IntType(), ]);
Records retrieved from or written to a CSV will be validated against the schema. Schema keys will be used as headers for the CSV.
Additional Processing
A slugHeaders()
method is provided on the Reader
class, which after calling will apply a slug transformation to headers. This allows CSVs to be authored with title-cased headers but referenced by simplified slugged representations.