PHP code example of vrok / import-export

1. Go to this page and download the library: Download vrok/import-export 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/ */

    

vrok / import-export example snippets


use Vrok\ImportExport\ExportableProperty;
use Vrok\ImportExport\ExportHelper;
use Vrok\ImportExport\ImportableProperty;
use Vrok\ImportExport\ImportHelper;

class Entity
{
    #[ExportableProperty]
    #[ImportableProperty]
    public int $id = 0;

    #[ExportableProperty]
    #[ImportableProperty]
    public ?\DateTimeImmutable $timestamp = null;
}

$entity = new Entity();
$entity->id = 1;
$entity->timestamp = new \DateTimeImmutable();

$helper = new ExportHelper();
$export = $helper->objectToArray($entity);
$exportList = $helper->collectionToArray([$entity]);

/*
  $export === [
    'id'        => 1,
    'timestamp' => '2022-03-23....',
  ]
*/

$helper = new ImportHelper();
$newInstance = $helper->objectFromArray($export, Entity::class);
$newInstances = $helper->collectionFromArray([$export], Entity::class);