PHP code example of dmt-software / import-reader

1. Go to this page and download the library: Download dmt-software/import-reader library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dmt-software / import-reader example snippets


use DMT\Import\Reader\Decorators\DecoratorInterface;
use DMT\Import\Reader\Decorators\Handler\GenericHandlerDecorator;
use DMT\Import\Reader\Handlers\JsonReaderHandler;
use DMT\Import\Reader\Reader;
use DMT\Import\Reader\Handlers\Sanitizers\SanitizerInterface;
use pcrov\JsonReader\JsonReader;

$internalReader = new JsonReader();
$internalReader->open('/path/to/some.json');

/** @var DecoratorInterface[] $decorators */
/** @var SanitizerInterface[] $sanitizers */
$reader = new Reader(
    new JsonReaderHandler($internalReader, ...$sanitizers),
    new GenericHandlerDecorator(), 
    ...$decorators
);

use DMT\Import\Reader\Decorators\ToObjectDecorator;
use DMT\Import\Reader\Reader;

/** @var Reader $reader */
$reader->addDecorator(new ToObjectDecorator(Customer::class, ['id' => 'id', 'name' => 'fullName', ]));

foreach ($reader->read() as $customer) {
    // import customer;
}

use DMT\Import\Reader\Reader;

/** start on item 4 */ 
$skip = 3;
/** skip all objects that has no id */
$filter = function (object $current) { 
    return isset($current->id); 
}

foreach ($reader->read($skip, $filter) as $item) {
    // import item
}

use DMT\Import\Reader\Exceptions\UnreadableException;

try {
    $readerBuilder->build($file, $options = []);
} catch (UnreadableException $exception) {
    // file can not be processed
}

use DMT\Import\Reader\Exceptions\ReaderReadException;

try {
    foreach ($reader->read() as $n => $object) {
        // import object
    }
} catch (ReaderReadException $exception) {
    // execution stopped, after $n rows
}