1. Go to this page and download the library: Download kaliop/csv-parser 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/ */
kaliop / csv-parser example snippets
namespace App\Entity;
class Order
{
/**
* @var int
*/
public $id;
/**
* @var \DateTime
*/
public $date;
/**
* @var string
*/
public $clientName;
/**
* @var string
*/
public $address;
/**
* @var int
*/
public $postalCode;
/**
* @var string
*/
public $country;
/**
* @var float
*/
public $amount;
/**
* @var null|\DateTime
*/
public $shipDate = null;
}
namespace App\Core;
use App\CSV\OrderParser;
class ImportOrdersFromCSV
{
protected $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
public function import($csvFilePath)
{
$parser = new OrderParser($csvFilePath, ";");
$results = $parser->execute();
foreach ($results as $result) {
if ($result->isValid()) {
$this->em->persist($result->getEntity());
continue;
}
// log or do something with invalid entities
}
$this->em->flush();
}
}
namespace App\CSV;
use Kaliop\CsvParser\Result\ParserResult;
use App\Entity\Order;
class OrderParserResult extends ParserResult
{
public function finalize()
{
if (!$this->entity instanceof Order || !$this->entity->date) {
// do nothing on invalid entities
return;
}
$shipDate = clone $this->entity->date;
$shipDate->modify('+3 days');
$this->entity->shipDate = $shipDate;
}
}
csv
date;order_id;client_name;address;postal_code;country;amount
2019-04-15;678945;"Laurent Doe";"12 avenue PhpUnit";34000;France;12
2019-03-12;987564;"Ruh Doe";"15 rue du test";31001;France;50,53
2019-05-01;123456;"Julien Doe";"125 rue PHP";34440;France;69,12
2019-02-09;456123;"Gérard Doe";"15 blvd Bouchard";76000;France;789,10
2019-01-01;965478;"Jean-Luc Doe";"15 rue du test";34000;France;5,00
2019-05-01;126578;"Bernard Doe";"15 rue Symfony";75000;France;33,53
2019-05-01;216543;"Maël Doe";"Disneyland Paris";77000;France;1250,53
2019-05-01;987521;"Gros Doe";"15 rue de Behat";98520;France;50,98
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.