{"id":36279459,"url":"https://github.com/ksassnowski/csv-schema","last_synced_at":"2026-01-11T09:02:12.289Z","repository":{"id":62540458,"uuid":"66401049","full_name":"ksassnowski/csv-schema","owner":"ksassnowski","description":"Parse a CSV file into PHP objects based on a schema.","archived":false,"fork":false,"pushed_at":"2025-02-12T07:54:17.000Z","size":24,"stargazers_count":25,"open_issues_count":2,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-25T14:14:34.631Z","etag":null,"topics":["csv","parser","schema"],"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/ksassnowski.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-08-23T20:31:52.000Z","updated_at":"2025-08-22T09:51:33.000Z","dependencies_parsed_at":"2025-02-12T08:34:17.248Z","dependency_job_id":"b0954e56-3b55-45a6-a972-7fdb50da9339","html_url":"https://github.com/ksassnowski/csv-schema","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/ksassnowski/csv-schema","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ksassnowski%2Fcsv-schema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ksassnowski%2Fcsv-schema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ksassnowski%2Fcsv-schema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ksassnowski%2Fcsv-schema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ksassnowski","download_url":"https://codeload.github.com/ksassnowski/csv-schema/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ksassnowski%2Fcsv-schema/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28298875,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-11T08:21:30.231Z","status":"ssl_error","status_checked_at":"2026-01-11T08:21:26.882Z","response_time":60,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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","schema"],"created_at":"2026-01-11T09:02:11.618Z","updated_at":"2026-01-11T09:02:12.283Z","avatar_url":"https://github.com/ksassnowski.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CSV Schema Parser\n\n[![Build Status](https://travis-ci.org/ksassnowski/csv-schema.svg?branch=master)](https://travis-ci.org/ksassnowski/csv-schema)\n[![Code Climate](https://codeclimate.com/github/ksassnowski/csv-schema/badges/gpa.svg)](https://codeclimate.com/github/ksassnowski/csv-schema)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/ksassnowski/csv-schema/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/ksassnowski/csv-schema/?branch=master)\n[![SensioLabsInsight](https://insight.sensiolabs.com/projects/efc81c31-f930-4d96-8c90-6104d500788a/mini.png)](https://insight.sensiolabs.com/projects/efc81c31-f930-4d96-8c90-6104d500788a)\n[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/ksassnowski/csv-schema/master/LICENSE.md)\n[![Current Release](https://img.shields.io/badge/packagist-0.3.0-blue.svg)](https://img.shields.io/badge/release-0.4.1-blue.svg)\n\nHave you ever wanted to have something like an ORM but for CSV files? No? Well now you can!\n\nIntroducing **CSV Schema Parser**. The number one way to turn your boring old CSV files into kick-ass PHP objects.\nAnd as if that wasn't amazing enough, it also casts your data to the correct data type! How cool is that? Not very cool you say? Well I disagree!\n\n## Installation\n\n```bash\ncomposer require sassnowski/csv-schema\n```\n\n## Usage\n\nFirst we have to define the schema of the CSV file we're about to parse. The schema is an associative array where the keys\ndefine what properties will be named on the resulting object. The values specify the data type of that column.\nThe schema is ordered, meaning the first entry in the schema will correspond to the first column in the CSV and so on.\n\n```php\n\u003c?php\n\n$config = [\n    'schema' =\u003e [\n        'first_name' =\u003e 'string',\n        'last_name' =\u003e 'string',\n        'age' =\u003e 'integer',\n        'coolness_factor' =\u003e 'float',\n    ]\n];\n```\n\nAfter we defined the schema, we can instantiate a new parser from it.\n\n```php\n\u003c?php\n\n// continuing from above..\n\n$parser = new Sassnowski\\CsvSchema\\Parser($config);\n```\n\n### Reading from a string\n\nThe parser provides a `fromString` method that will turn any CSV string into an array of objects. The objects will be structured according to our schema.\n\n```php\n\u003c?php\n\npublic Parser::fromString(string $input): array\n```\n\n#### Example\n\n```php\n\u003c?php\n\n// Using our parser from above..\n\n$input = \"Kai,Sassnowski,26,0.3\\nJohn,Doe,38,7.8\";\n\n$rows = $parser-\u003efromString($input);\n\nvar_dump($rows[0]-\u003efirstname);\n// string(3) \"Kai\"\n\nvar_dump($rows[0]-\u003eage);\n// int(26)\n\nvar_dump($rows[0]-\u003ecoolness_factor);\n// float(0.3)\n```\n\n### Reading from a file\n\nMore often than not you will parse your CSV from a file. For this the `Parser` provides the `fromFile` method.\n\n```php\n\u003c?php\n\npublic Parser::fromFile(string $filename): array\n```\n\n#### Example\n\n```php\n\u003c?php\n\n// Using our parser from above..\n\n// Assuming our file contains the same data as the string example.\n$rows = $parser-\u003efromFile('somefile.csv');\n\nvar_dump($rows[1]-\u003efirstname);\n// string(4) \"John\"\n\nvar_dump($rows[1]-\u003ecoolness_factor);\n// float(7.8)\n```\n\n## Configuration\n\nThe configuration array provides a way to overwrite the default settings of the parser. You can set the `delimiter`, the `enclosure` character, the `escape` character and the `encoding` of the input file.\n\n```php\n\u003c?php\n\n$config = [\n    'delimiter' =\u003e ',',\n    'enclosure' =\u003e '\"',\n    'escape' =\u003e '\\\\',\n    'skipTitle' =\u003e false,\n    'encoding' =\u003e 'UTF-8',\n    'schema' =\u003e [ /* Your schema */ ],\n];\n```\n\n### Available Column Types\n\nYou can parse a column to `string`, `float`, `integer` and `array`.\n\n#### Parsing arrays\n\nAssuming you have multiple values in a column (sometimes we cannot choose our data...) you might want to parse that column into an array instead.\nYou can do this by specifying `array:\u003cdelimiter\u003e` as the column type in your schema.\n\n```php\n\u003c?php\n\n$config = [\n    'schema' =\u003e [\n        'name' =\u003e 'string',\n        'friends' =\u003e 'array:|'\n    ],\n];\n\n$input = 'Kai Sassnowski,Lots|and|lots|of|friends';\n\n$parser = new Sassnowski\\CsvSchema\\Parser($config);\n\n$rows = $parser-\u003efromString($input);\n\nvar_dump($rows[0])-\u003efriends);\n\n// array(5) {\n//    [0]=\u003e\n//    string(4) \"Lots\"\n//    [1]=\u003e\n//    string(3) \"and\"\n//    [2]=\u003e\n//    string(4) \"lots\"\n//    [3]=\u003e\n//    string(2) \"of\"\n//    [4]=\u003e\n//    string(7) \"friends\"\n// }\n```\n\n## Adding custom types\n\nSometimes you might want a bit more control than the built-in types give you. For instance, you might want to query your database\nbased on an integer in one of your columns and return a model instead. You can easily register\narbitrarily complex types by using the static `registerType` method on the `Parser` class.\n\n```php\n\u003c?php\n\npublic static registerType(string $type, callable $callback)\n```\n\nThe provided callback receives the column's value and should return its parsed representation.\n\n```php\n\u003c?php\n\nParser::registerType('foo', function ($value) {\n    return $value.'foo';\n});\n\n$config = [\n    'schema' =\u003e [\n        'custom' =\u003e 'foo'\n    ]\n];\n\n$parser = new \\Sassnowski\\CsvSchema\\Parser($config);\n\n$rows = $parser-\u003efromString(\"hello\\nworld\");\n\nvar_dump($rows[0]-\u003ecustom);\n// string(8) \"hellofoo\"\n\nvar_dump($rows[1]-\u003ecustom);\n// string(8) \"worldfoo\"\n```\n\n### Adding parameters to custom types\n\nYou can add additional parameters to your types by specifying them in the following format in your schema `\u003ctype\u003e:\u003cparameter\u003e`.\nIn this case, the callback function gets passed a second parameter containing the parameter specified in the schema.\nThis allows you to reuse your types instead of defining all sorts of variations of the same type (like querying the database, but using a different table/model).\n\n```php\n\u003c?php\n\nParser::registerType('model', function ($value, $table) {\n    // Pseudo code, assuming some library.\n    return DB::table($table)-\u003efindById($value);\n});\n\n$config = [\n    'schema' =\u003e [\n        'author' =\u003e 'model:authors',\n        'uploaded_by' =\u003e 'model:users',\n    ]\n];\n\n$input = \"5,13\";\n\n$parser = new \\Sassnowski\\CsvSchema\\Parser($config);\n\n$rows = $parser-\u003efromString($input);\n\nvar_dump($rows[0]-\u003eauthor);\n// object(Author)#1 (15) {\n//    [\"id\"]=\u003e\n//    int(5)\n//    ...\n// }\n\nvar_dump($rows[1]-\u003euploaded_by);\n// object(User)#1 (12) {\n//    [\"id\"]=\u003e\n//    int(13)\n//    ...\n// }\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fksassnowski%2Fcsv-schema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fksassnowski%2Fcsv-schema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fksassnowski%2Fcsv-schema/lists"}