Skip to content

Commit b1f59a7

Browse files
committed
v1.0.0 - PHP 8.1+, edge case handling, bug fixes
1 parent 01f998f commit b1f59a7

21 files changed

Lines changed: 1045 additions & 624 deletions

‎.editorconfig‎

Lines changed: 0 additions & 15 deletions
This file was deleted.

‎.gitattributes‎

Lines changed: 0 additions & 11 deletions
This file was deleted.

‎.github/workflows/tests.yml‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,24 @@ jobs:
77
strategy:
88
fail-fast: true
99
matrix:
10-
php: [ 8.0, 8.1, 8.2, 8.3, 8.4 ]
11-
dependency-version: [ prefer-lowest, prefer-stable ]
10+
php: [8.1, 8.2, 8.3, 8.4, 8.5]
11+
dependency-version: [prefer-lowest, prefer-stable]
1212

13-
name: P${{ matrix.php }} - ${{ matrix.dependency-version }}
13+
name: PHP ${{ matrix.php }} - ${{ matrix.dependency-version }}
1414

1515
steps:
1616
- name: Checkout code
17-
uses: actions/checkout@v2
17+
uses: actions/checkout@v4
1818

1919
- name: Setup PHP
2020
uses: shivammathur/setup-php@v2
2121
with:
2222
php-version: ${{ matrix.php }}
23-
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick
23+
extensions: json
2424
coverage: none
2525

2626
- name: Install dependencies
27-
run: |
28-
composer install --no-progress --no-suggest --prefer-dist --optimize-autoloader
27+
run: composer install --no-progress --prefer-dist --optimize-autoloader
28+
2929
- name: Execute tests
3030
run: vendor/bin/phpunit
31-
- run: echo " ${{ job.status }}."

‎.gitignore‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
build
1+
.claude/
2+
.idea/
3+
.vscode/
4+
/build/
5+
/vendor/
6+
.phpunit.cache
7+
.phpunit.result.cache
28
composer.lock
3-
vendor
4-
*.cache
9+
CLAUDE.MD

‎.travis.yml‎

Lines changed: 0 additions & 18 deletions
This file was deleted.

‎CHANGELOG.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22

33
All Notable changes to `json-csv` will be documented in this file.
44

5+
## 1.0.0
6+
7+
- Minimum PHP version is now 8.1, and updated PHPUnit to version 10/11.
8+
- Modernized codebase with PHP 8.1+ features (typed properties, union types, `static` return type).
9+
- Replaced `numbers` option with boolean `numeric` option for CSV to JSON conversion.
10+
- Improved handling of edge cases (empty arrays, nested structures, arrays of scalars).
11+
- Added support for CSV fields containing newlines within quoted values.
12+
- Better handling of different line endings (Unix, Windows, old Mac).
13+
- Added comprehensive test suite with 85+ tests covering edge cases.
14+
- Removed Travis CI and updated GitHub Actions workflow.
15+
516
## 0.4.0
617
- Remove support for PHP 7.2.
718
- Added PHP 8.0 support.

‎CONTRIBUTING.md‎

Lines changed: 0 additions & 32 deletions
This file was deleted.

‎LICENSE.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# The MIT License (MIT)
22

3-
Copyright (c) 2023 Burak Özdemir
3+
Copyright (c) 2025 Burak Özdemir
44

55
> Permission is hereby granted, free of charge, to any person obtaining a copy
66
> of this software and associated documentation files (the "Software"), to deal

‎README.md‎

Lines changed: 59 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![Latest Version on Packagist][ico-version]][link-packagist]
44
[![Software License][ico-license]](LICENSE.md)
5-
[![Build Status][ico-travis]][link-travis]
5+
[![Build Status][ico-github-actions]][link-github-actions]
66
[![Total Downloads][ico-downloads]][link-downloads]
77

88
The most basic CSV to JSON and JSON to CSV converter library in PHP without any dependencies.
@@ -11,96 +11,108 @@ The most basic CSV to JSON and JSON to CSV converter library in PHP without any
1111

1212
Via Composer
1313

14-
``` bash
15-
$ composer require ozdemirburak/json-csv
14+
```bash
15+
composer require ozdemirburak/json-csv
1616
```
1717

18+
Note: Requires PHP 8.1 or higher with JSON extension
19+
1820
## Usage
1921

2022
### JSON to CSV Converter
2123

22-
``` php
24+
```php
2325
use OzdemirBurak\JsonCsv\File\Json;
2426

2527
// JSON to CSV
26-
$json = new Json(__DIR__ . '/above.json');
28+
$json = new Json(__DIR__ . '/file.json');
2729
// To convert JSON to CSV string
2830
$csvString = $json->convert();
2931
// To set a conversion option then convert JSON to CSV and save
3032
$json->setConversionKey('utf8_encoding', true);
31-
$json->convertAndSave(__DIR__ . '/above.csv');
33+
$json->convertAndSave(__DIR__ . '/file.csv');
3234
// To convert JSON to CSV and force download on browser
3335
$json->convertAndDownload();
3436
```
3537

3638
You can also convert directly from a JSON string using the `fromString` method.
3739

38-
``` php
39-
$csvString = (new Json())->fromString('{"name": "Buddha", "age": 80}')->convert();
40+
```php
41+
$csvString = (new Json())->fromString('{"name": "John", "age": 30}')->convert();
4042
```
4143

42-
Assume that the input JSON is something like below.
44+
Assume that the input JSON is something like below.
4345

4446
```json
4547
[
4648
{
4749
"name": {
48-
"common": "Turkey",
49-
"official": "Republic of Turkey",
50-
"native": "T\u00fcrkiye"
50+
"common": "Türkiye",
51+
"official": "Republic of Türkiye",
52+
"native": "Türkiye"
5153
},
5254
"area": 783562,
5355
"latlng": [39, 35]
5456
},
5557
{
5658
"name": {
57-
"common": "Israel",
58-
"official": "State of Israel",
59-
"native": "\u05d9\u05e9\u05e8\u05d0\u05dc"
59+
"common": "Japan",
60+
"official": "Japan",
61+
"native": "日本"
6062
},
61-
"area": 20770,
62-
"latlng": [31.30, 34.45]
63+
"area": 377975,
64+
"latlng": [36, 138]
6365
}
6466
]
6567
```
6668

6769
After the conversion, the resulting CSV data will look like below.
6870

69-
**name\_common**|**name\_official**|**name\_native**|**area**|**latlng\_0**|**latlng\_1**
70-
:-----:|:-----:|:-----:|:-----:|:-----:|:-----:
71-
Turkey|Republic of Turkey|Türkiye|783562|39|35
72-
Israel|State of Israel|ישראל|20770|31.3|34.45
71+
| name_common | name_official | name_native | area | latlng_0 | latlng_1 |
72+
|--------------|----------------------|--------------|------------|----------|-----------|
73+
| Türkiye | Republic of Türkiye | Türkiye | 783562 | 39 | 35 |
74+
| Japan | Japan | 日本 | 377975 | 36 | 138 |
75+
76+
#### JSON Conversion Options
7377

78+
| Option | Type | Default | Description |
79+
|-----------------|--------|-----------|--------------------------------------|
80+
| `delimiter` | string | `,` | CSV field delimiter |
81+
| `enclosure` | string | `"` | CSV field enclosure character |
82+
| `escape` | string | `\` | CSV escape character |
83+
| `join` | string | `_` | Character used to join nested keys |
84+
| `null` | mixed | `null` | Value to use for null/missing fields |
85+
| `utf8_encoding` | bool | `false` | Add UTF-8 BOM to output |
7486

7587
### CSV to JSON Converter
7688

77-
``` php
89+
```php
7890
use OzdemirBurak\JsonCsv\File\Csv;
7991

8092
// CSV to JSON
81-
$csv = new Csv(__DIR__ . '/below.csv');
93+
$csv = new Csv(__DIR__ . '/file.csv');
8294
$csv->setConversionKey('options', JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
8395
// To convert CSV to JSON string
8496
$jsonString = $csv->convert();
8597
// To convert CSV to JSON and save
86-
$csv->convertAndSave(__DIR__ . '/below.json');
98+
$csv->convertAndSave(__DIR__ . '/file.json');
8799
// To convert CSV to JSON and force download on browser
88100
$csv->convertAndDownload();
89101
```
90102

91103
You can also convert directly from a CSV string using the `fromString` method.
92104

93-
``` php
94-
$jsonString = (new Csv())->fromString('[{"name":"Buddha","age":"80"}]')->convert();
105+
```php
106+
$jsonString = (new Csv())->fromString("name,age\nJohn,30\n")->convert();
95107
```
96108

97-
Assume that the input CSV file is something like below.
109+
Assume that the input CSV file is something like below.
98110

99-
**SepalLength**|**SepalWidth**|**PetalLength**|**PetalWidth**|**Name**
100-
:-----:|:-----:|:-----:|:-----:|:-----:
101-
5.1|3.5|1.4|0.2|Iris-setosa
102-
7.0|3.2|4.7|1.4|Iris-versicolor
103-
6.3|3.3|6.0|2.5|Iris-virginica
111+
| SepalLength | SepalWidth | PetalLength | PetalWidth | Name |
112+
|-------------|-------------|--------------|------------|-----------------|
113+
| 5.1 | 3.5 | 1.4 | 0.2 | Iris-setosa |
114+
| 7.0 | 3.2 | 4.7 | 1.4 | Iris-versicolor |
115+
| 6.3 | 3.3 | 6.0 | 2.5 | Iris-virginica |
104116

105117
After the conversion, the resulting JSON data will look like below.
106118

@@ -130,20 +142,27 @@ After the conversion, the resulting JSON data will look like below.
130142
]
131143
```
132144

145+
#### CSV Conversion Options
146+
147+
| Option | Type | Default | Description |
148+
|-------------|--------|-----------|--------------------------------------------------------|
149+
| `delimiter` | string | `,` | CSV field delimiter |
150+
| `enclosure` | string | `"` | CSV field enclosure character |
151+
| `escape` | string | `\` | CSV escape character |
152+
| `join` | string | `_` | Character used to split nested keys |
153+
| `numeric` | bool | `false` | Set to `true` to convert numeric strings to numbers |
154+
| `options` | int | `0` | JSON encode flags (e.g., `JSON_PRETTY_PRINT`) |
155+
133156
## Change log
134157

135158
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
136159

137160
## Testing
138161

139-
``` bash
140-
$ composer test
162+
```bash
163+
composer test
141164
```
142165

143-
## Known Issues
144-
145-
Currently, there are not any issues that are known.
146-
147166
## Contributing
148167

149168
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
@@ -159,11 +178,11 @@ The MIT License (MIT). Please see [License File](LICENSE.md) for more informatio
159178

160179
[ico-version]: https://img.shields.io/packagist/v/ozdemirburak/json-csv.svg?style=flat-square
161180
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
162-
[ico-travis]: https://img.shields.io/travis/ozdemirburak/json-csv/master.svg?style=flat-square
181+
[ico-github-actions]: https://img.shields.io/github/actions/workflow/status/ozdemirburak/json-csv/tests.yml?branch=master&style=flat-square
163182
[ico-downloads]: https://img.shields.io/packagist/dt/ozdemirburak/json-csv.svg?style=flat-square
164183

165184
[link-packagist]: https://packagist.org/packages/ozdemirburak/json-csv
166-
[link-travis]: https://travis-ci.org/ozdemirburak/json-csv
185+
[link-github-actions]: https://github.com/ozdemirburak/json-csv/actions/workflows/tests.yml
167186
[link-downloads]: https://packagist.org/packages/ozdemirburak/json-csv
168187
[link-author]: https://github.com/ozdemirburak
169188
[link-contributors]: ../../contributors

‎composer.json‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
}
1212
],
1313
"require": {
14-
"php": "^7.3|^8.0",
14+
"php": "^8.1",
1515
"ext-json": "*"
1616
},
1717
"require-dev": {
18-
"phpunit/phpunit" : "~8.0|~9.0",
19-
"squizlabs/php_codesniffer": "~3.5"
18+
"phpunit/phpunit": "^10.0|^11.0",
19+
"squizlabs/php_codesniffer": "^3.7"
2020
},
2121
"autoload": {
2222
"psr-4": { "OzdemirBurak\\JsonCsv\\": "src" }

0 commit comments

Comments
 (0)