{"id":15014381,"url":"https://github.com/sebastianbergmann/csv-parser","last_synced_at":"2025-04-08T03:09:40.013Z","repository":{"id":145162526,"uuid":"617438454","full_name":"sebastianbergmann/csv-parser","owner":"sebastianbergmann","description":"Library for type-safe parsing of CSV files","archived":false,"fork":false,"pushed_at":"2025-03-16T07:58:05.000Z","size":21670,"stargazers_count":41,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-31T18:19:28.959Z","etag":null,"topics":["csv-parser"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sebastianbergmann.png","metadata":{"files":{"readme":"README.md","changelog":"ChangeLog.md","contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"sebastianbergmann"}},"created_at":"2023-03-22T11:53:27.000Z","updated_at":"2025-03-16T07:58:09.000Z","dependencies_parsed_at":null,"dependency_job_id":"494771ee-1f72-4bc2-af4c-91eef5a8afb7","html_url":"https://github.com/sebastianbergmann/csv-parser","commit_stats":{"total_commits":119,"total_committers":3,"mean_commits":"39.666666666666664","dds":0.01680672268907568,"last_synced_commit":"955c647892f4b7ad6b3ac5ec77e57f8d0488f9af"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebastianbergmann%2Fcsv-parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebastianbergmann%2Fcsv-parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebastianbergmann%2Fcsv-parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebastianbergmann%2Fcsv-parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sebastianbergmann","download_url":"https://codeload.github.com/sebastianbergmann/csv-parser/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247767234,"owners_count":20992547,"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-parser"],"created_at":"2024-09-24T19:45:33.039Z","updated_at":"2025-04-08T03:09:39.987Z","avatar_url":"https://github.com/sebastianbergmann.png","language":"PHP","funding_links":["https://github.com/sponsors/sebastianbergmann"],"categories":[],"sub_categories":[],"readme":"[![No Maintenance Intended](https://unmaintained.tech/badge.svg)](https://unmaintained.tech/)\n[![Latest Stable Version](https://poser.pugx.org/sebastian/csv-parser/v)](https://packagist.org/packages/sebastian/csv-parser)\n[![CI Status](https://github.com/sebastianbergmann/csv-parser/workflows/CI/badge.svg)](https://github.com/sebastianbergmann/csv-parser/actions)\n[![codecov](https://codecov.io/gh/sebastianbergmann/csv-parser/branch/main/graph/badge.svg)](https://codecov.io/gh/sebastianbergmann/csv-parser)\n\n# sebastian/csv-parser\n\nLibrary for type-safe parsing of CSV files.\n\n## Installation\n\nYou can add this library as a local, per-project dependency to your project using [Composer](https://getcomposer.org/):\n\n```\ncomposer require sebastian/csv-parser\n```\n\nIf you only need this library during development, for instance to run your project's test suite, then you should add it as a development-time dependency:\n\n```\ncomposer require --dev sebastian/csv-parser\n```\n\n## Usage\n\n#### `example.csv`\n\n```csv\n1,2,3,1,0,2023-03-24\n```\n\n```php\n\u003c?php declare(strict_types=1);\nuse SebastianBergmann\\CsvParser\\Parser;\nuse SebastianBergmann\\CsvParser\\Schema;\nuse SebastianBergmann\\CsvParser\\FieldDefinition;\nuse SebastianBergmann\\CsvParser\\Type;\nuse SebastianBergmann\\CsvParser\\ObjectMapper;\n\n$schema = Schema::from(\n    FieldDefinition::from(1, 'a', Type::integer()),\n    FieldDefinition::from(2, 'b', Type::float()),\n    FieldDefinition::from(3, 'c', Type::string()),\n    FieldDefinition::from(4, 'd', Type::boolean()),\n    FieldDefinition::from(5, 'e', Type::boolean()),\n    FieldDefinition::from(6, 'f', Type::object(\n        new class implements ObjectMapper\n        {\n            public function map(string $value): DateTimeImmutable\n            {\n                return new DateTimeImmutable($value);\n            }\n        }\n    )),\n);\n\n$parser = new Parser;\n\nforeach ($parser-\u003eparse('example.csv', $schema) as $record) {\n    var_dump($record);\n}\n```\n\nThe example above shows how to use a `Schema` object to configure how a string from a line of comma-separated values is parsed into an array element:\n\n* `1` refers to the position of the string in the line of comma-separated values\n* `'a'` specifies the key for the value in the associative array that is generated for each record\n* `Type::integer()` specifies the type that should be used to store the value in the associative array that is generated for each record\n\nThe following types are available:\n\n* boolean (`Type::boolean()`; uses `(bool)` type cast)\n* integer (`Type::integer()`; uses `(int)` type cast)\n* float (`Type::float()`; uses `(float)` type cast)\n* string (`Type::string()`)\n* object (`Type::object($mapper)`; `$mapper` is an object that implements the `SebastianBergmann\\CsvParser\\ObjectMapper` interface)\n\nThe `Parser::parse()` method requires two arguments:\n\n* The first argument, `$filename`, is the path to the CSV file that should be parsed\n* The second argument, `$schema`, is the `Schema` object we discussed above\n\nRunning the example shown above prints the output shown below:\n\n```\narray(3) {\n  [\"a\"]=\u003e\n  int(1)\n  [\"b\"]=\u003e\n  float(2)\n  [\"c\"]=\u003e\n  string(1) \"3\"\n  [\"d\"]=\u003e\n  bool(true)\n  [\"e\"]=\u003e\n  bool(false)\n  [\"f\"]=\u003e\n  object(DateTimeImmutable)#1 (3) {\n    [\"date\"]=\u003e\n    string(26) \"2023-03-24 00:00:00.000000\"\n    [\"timezone_type\"]=\u003e\n    int(3)\n    [\"timezone\"]=\u003e\n    string(13) \"Europe/Berlin\"\n  }\n}\n```\n\nThe `$parser-\u003eignoreFirstLine()` method can be used to configure the parser to ignore the first line of the CSV file.\n\nThe `$parser-\u003esetSeparator()` method can be used to configure the parser to use a separator different from the default `,`.\n\nThe `$parser-\u003esetEnclosure()` method can be used to configure the parser to use an enclosure different from the default `\"`.\n\nThe `$parser-\u003esetEscape()` method can be used to configure the parser to use an escape different from the default `\"`.\n\nPlease note that this is a [low maintenance project](https://github.com/sebastianbergmann/csv-parser/blob/main/.github/CONTRIBUTING.md#low-maintenance-project).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsebastianbergmann%2Fcsv-parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsebastianbergmann%2Fcsv-parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsebastianbergmann%2Fcsv-parser/lists"}