ts / reader
Extensible string and file reading library.
This package has no released version yet, and little information is available.
README
Extensible string and file reading library.
Installation
Use Composer to install: composer require ts/reader:~2.1
Direct usage of an implementation
If you know exactly what you want to read, feel free to instantiate a reader implementation directly:
use TS\Reader\Implementation\Json; $jsonReader = new Json; // Setting a json string $jsonReader->setString('{ "key": "value" }'); // Setting a json file $jsonReader->setFile(/* path to .json file */); // Reading the data $data = $jsonReader->readAll();
Using the ReaderContainer
Instead of instantiating reader implementations directly you can use the reader container to create the reader for you after a little bit of bootstrapping:
use TS\Reader\ReaderContainer; $container = new ReaderContainer; // Registration $container->registerReader('TS\\Reader\\Implementation\\Json', ['json']); // Register further implementations... // Creation using a json string $reader = $container->createForString('json', '{ "key": "value" }'); // Creation using a json file $reader = $container->createForFile(/* path to .json file */); // Reading the data $data = $reader->readAll();
Using the event system
You can intercept or influence most parts of the mailer's lifecycle by utilizing your favorite event dispatcher. By default Symfony's EventDispatcher Component is used.
To utilize the full power of the EventDispatcher, you need to explicitly pass an instance when creating either the ReaderContainer or reader implementation:
use TS\Common\Event\EventDispatcher; use TS\Reader\Implementation\Json; use TS\Reader\ReaderContainer; // Instantiate or get the EventDispatcher instance from somewhere, like a service/DI container $dispatcher = new EventDispatcher; // Pass it to the reader implementation or container $jsonReader = new Json($dispatcher); $container = new ReaderContainer($dispatcher);
You're now able to listen in to the following events, which are described inside the TS\Reader\ReaderEvents
class:
- INIT: Dispatched right in the reader's constructor.
- READ: Dispatched after reading.
- READ_COMPLETE: Dispatched when everything has been read.
- READ_LINE: Dispatched when a line has been read.
Available implementations
Type Class name Csv TS\Reader\Implementation\Csv Ini TS\Reader\Implementation\Ini Json TS\Reader\Implementation\Json Txt TS\Reader\Implementation\Txt Xml TS\Reader\Implementation\Xml Yaml TS\Reader\Implementation\YamlExcel support is split off into a separate composer package ts/reader-excel
.