{"id":19162232,"url":"https://github.com/kaliop/csv-parser","last_synced_at":"2025-12-31T14:32:45.377Z","repository":{"id":57004119,"uuid":"188010400","full_name":"kaliop/csv-parser","owner":"kaliop","description":"PHP CSV Parser. OOP-style, Customizable and Testable","archived":false,"fork":false,"pushed_at":"2019-05-27T12:11:02.000Z","size":24,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-02-22T22:29:15.564Z","etag":null,"topics":["csv","csv-parser","excel","library","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kaliop.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-05-22T09:51:04.000Z","updated_at":"2019-05-27T12:06:54.000Z","dependencies_parsed_at":"2022-08-21T13:50:45.082Z","dependency_job_id":null,"html_url":"https://github.com/kaliop/csv-parser","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/kaliop/csv-parser","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaliop%2Fcsv-parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaliop%2Fcsv-parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaliop%2Fcsv-parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaliop%2Fcsv-parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kaliop","download_url":"https://codeload.github.com/kaliop/csv-parser/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaliop%2Fcsv-parser/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263833641,"owners_count":23517387,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csv","csv-parser","excel","library","php"],"created_at":"2024-11-09T09:08:32.992Z","updated_at":"2025-12-31T14:32:45.348Z","avatar_url":"https://github.com/kaliop.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CSV Parser Library\n\nThis library allows you to define your CSV parsing in OOP style, allowing you to easily filter and test your imports. \nIt's been in production on various large clients with multiples complex imports for quite some time now.\n\nMain advantages are:\n- OOP-style: One *Parser* class per import = meaningful for developers working on the project.\n- Testable: A fixture csv and you can easily write tests for the import.\n- Fully Customizable: each parsed result is also a PHP class where the developer can add its own logic.\n- Lightly coupled: this package depends only on Symfony's Validator. Any database/persistence stuff is possible (see example below).\n\n\n## Installation\n\nInstall this package using composer:\n``` bash\ncomposer require kaliop/csv-parser\n```\n\n## Basic Usage\n\nYou'll need to describe the mapping between CSV columns and entity's properties.\nTo do so, you should start by creating a *Parser*, but first, let's take a look at our CSV:\n\n```csv\ndate;order_id;client_name;address;postal_code;country;amount\n2019-04-15;678945;\"Laurent Doe\";\"12 avenue PhpUnit\";34000;France;12\n2019-03-12;987564;\"Ruh Doe\";\"15 rue du test\";31001;France;50,53\n2019-05-01;123456;\"Julien Doe\";\"125 rue PHP\";34440;France;69,12\n2019-02-09;456123;\"Gérard Doe\";\"15 blvd Bouchard\";76000;France;789,10\n2019-01-01;965478;\"Jean-Luc Doe\";\"15 rue du test\";34000;France;5,00\n2019-05-01;126578;\"Bernard Doe\";\"15 rue Symfony\";75000;France;33,53\n2019-05-01;216543;\"Maël Doe\";\"Disneyland Paris\";77000;France;1250,53\n2019-05-01;987521;\"Gros Doe\";\"15 rue de Behat\";98520;France;50,98\n```\n\nour Entity looks like this:\n\n```php\n\u003c?php\n\nnamespace App\\Entity;\n\nclass Order\n{\n    /**\n     * @var int\n     */\n    public $id;\n\n    /**\n     * @var \\DateTime\n     */\n    public $date;\n\n    /**\n     * @var string\n     */\n    public $clientName;\n\n    /**\n     * @var string\n     */\n    public $address;\n\n    /**\n     * @var int\n     */\n    public $postalCode;\n\n    /**\n     * @var string\n     */\n    public $country;\n\n    /**\n     * @var float\n     */\n    public $amount;\n    \n    /**\n     * @var null|\\DateTime\n     */\n    public $shipDate = null;\n}\n```\n\nCreating the *Parser*:\n\n```php\n\u003c?php\n\nnamespace App\\CSV;\n\nuse App\\Entity\\Order;\nuse Kaliop\\CsvParser\\Parser\\AbstractParser;\nuse Kaliop\\CsvParser\\Parser\\ParserInterface;\nuse Kaliop\\CsvParser\\ColumnHelper;\n\nclass OrderParser extends AbstractParser implements ParserInterface\n{\n    public function getMappingDefinition()\n    {\n        return [\n            ColumnHelper::index('A') =\u003e [\n                'property'  =\u003e 'entity.date',\n                'filter'    =\u003e function($value) { return \\DateTime::createFromFormat('Y-m-d', $value); }\n            ],\n            ColumnHelper::index('B') =\u003e [\n                'property'  =\u003e 'entity.id',\n                'filter'    =\u003e function($value) { return \\intval($value); }\n            ],\n            ColumnHelper::index('C') =\u003e [\n                'property'  =\u003e 'entity.clientName',\n                'filter'    =\u003e function($value) { return \\trim($value); }\n            ],\n            ColumnHelper::index('D') =\u003e [\n                'property'  =\u003e 'entity.address',\n                'filter'    =\u003e function($value) { return \\trim($value); }\n            ],\n            ColumnHelper::index('E') =\u003e [\n                'property'  =\u003e 'entity.postalCode',\n                'filter'    =\u003e function($value) { return \\intval($value); }\n            ],\n            ColumnHelper::index('F') =\u003e [\n                'property'  =\u003e 'entity.country',\n                'filter'    =\u003e function($value) { return \\trim($value); }\n            ],\n            ColumnHelper::index('G') =\u003e [\n                'property'  =\u003e 'entity.amount',\n                'filter'    =\u003e function($value) { return \\floatval($value); }\n            ]\n        ];\n    }\n\n    public function getEntityClassName()\n    {\n        return Order::class;\n    }\n}\n```\n\nAnd now do the magic! In this example we persist imported entities in database. \n\n```php\n\u003c?php\n\nnamespace App\\Core;\n\nuse App\\CSV\\OrderParser;\n\nclass ImportOrdersFromCSV\n{\n    protected $em;\n    \n    public function __construct(EntityManagerInterface $em)\n    {\n        $this-\u003eem = $em;\n    }\n    \n    public function import($csvFilePath)\n    {\n        $parser = new OrderParser($csvFilePath, \";\");\n        $results = $parser-\u003eexecute();\n        \n        foreach ($results as $result) {\n            if ($result-\u003eisValid()) {\n                $this-\u003eem-\u003epersist($result-\u003egetEntity());\n                continue;\n            }\n            \n            // log or do something with invalid entities\n        }\n        \n        $this-\u003eem-\u003eflush();\n    }\n}\n```\n\n## Advanced Usage\n\nYou can decide to change the default ```ParserResult``` class to do you own logic once the Entity has been parsed.\nIn the following example we will add a ```$shipDate``` which will be set by our custom result class, in the ```finalize()``` method:\n\n\n```php\n\u003c?php\n\nnamespace App\\CSV;\n\nuse Kaliop\\CsvParser\\Result\\ParserResult;\nuse App\\Entity\\Order;\n\nclass OrderParserResult extends ParserResult\n{\n    public function finalize()\n    {\n        if (!$this-\u003eentity instanceof Order || !$this-\u003eentity-\u003edate) {\n            // do nothing on invalid entities\n            return;\n        }\n\n        $shipDate = clone $this-\u003eentity-\u003edate;\n        $shipDate-\u003emodify('+3 days');\n\n        $this-\u003eentity-\u003eshipDate = $shipDate;\n    }\n}\n```\n\nNow just tell your Parser to use this Result class and you're ready:\n\n```php\n\u003c?php\n// ... ImportOrdersFromCSV\n\n$parser-\u003esetResultClassName(App\\CSV\\OrderParserResult::class);\n```\n\n## Run PHPUnit Tests\n\n```bash\n./vendor/bin/phpunit \n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkaliop%2Fcsv-parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkaliop%2Fcsv-parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkaliop%2Fcsv-parser/lists"}