All reader handlers use an internal reader to read through a file and a series of sanitizers to apply to the raw values returned by these internal reader. All internal readers (should) support both a path to a file and a protocol wrapper to a file.
The csv reader handler internally uses fgetcsv to read through a file line by line. It can be configured to use the right csv control characters: "delimiter, enclosure and escape".
The sanitizers this handler receives are applied on each of the array values that is returned by the inner reader.
use DMT\Import\Reader\Handlers\CsvReaderHandler;
use DMT\Import\Reader\Handlers\Sanitizers\TrimSanitizer;
$csvReaderHandler = new CsvReaderHandler($innerReader, ['delimiter' => ';'], new TrimSanitizer()); The reader handler to read json files uses a JsonReader as inner reader. It expects a Pointer with a "dotted path" to point to the right elements to iterate from.
- . (a single dot) - points to first object in an array of objects.
- root.elements - points to the elements object array of the root.
use DMT\Import\Reader\Handlers\Pointers\JsonPathPointer;
use DMT\Import\Reader\Handlers\JsonReaderHandler;
use pcrov\JsonReader\JsonReader;
$innerReader = new JsonReader();
$innerReader->open($jsonFile);
$jsonReaderHandler = new JsonReaderHandler($innerReader, new JsonPathPointer($path));When the path is left empty the complete contents of the file is returned whilst reading.
NOTE: The paths are always determined from the root of the file to import and should always point to an (array of) object(s)
This handler to read through XML uses the build-in XMLReader. It requires a Pointer to determine which elements to return during reading. This pointer is configured with an xpath-like structure of node names starting from the root of the xml.
- /root/element - points to the first element in root.
use DMT\Import\Reader\Handlers\Pointers\XmlPathPointer;
use DMT\Import\Reader\Handlers\XmlReaderHandler;
use DMT\XmlParser\Parser;
use DMT\XmlParser\Source\FileParser;
use DMT\XmlParser\Tokenizer;
$innerReader = new Parser(new Tokenizer(new FileParser($xmlFile), $fileEncoding, $tokenizerOptions));
$jsonReaderHandler = new XmlReaderHandler($innerReader, new XmlPathPointer($path));The handler factory can be used to construct a reader for a file type with certain options, as documented in the reader builder configuration.
use DMT\Import\Reader\Handlers\CsvReaderHandler;
$factory->createReaderHandler(CsvReaderHandler::class, $file, $options = ['delimiter' => ';']); To enable creating a custom handler via the handler factory a handler factory must be registered for it.
use DMT\Import\Reader\Handlers\Factories\HandlerFactoryInterface;
class CustomHandlerFactory implements HandlerFactoryInterface {}
$factory->addInitializeHandlerFactory(CustomReaderHandler::class, new CustomHandlerFactory());