Skip to content

Commit f98edd5

Browse files
committed
initial commit
0 parents  commit f98edd5

17 files changed

+626
-0
lines changed

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
; This file is for unifying the coding style for different editors and IDEs.
2+
; More information at http://editorconfig.org
3+
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
indent_size = 4
9+
indent_style = space
10+
end_of_line = lf
11+
insert_final_newline = true
12+
trim_trailing_whitespace = true
13+
14+
[*.md]
15+
trim_trailing_whitespace = false

.gitattributes

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Path-based git attributes
2+
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html
3+
4+
# Ignore all test and documentation with "export-ignore".
5+
/.gitattributes export-ignore
6+
/.gitignore export-ignore
7+
/.travis.yml export-ignore
8+
/phpunit.xml.dist export-ignore
9+
/.scrutinizer.yml export-ignore
10+
/tests export-ignore
11+
/.editorconfig export-ignore

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
build
2+
composer.lock
3+
vendor

.travis.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
language: php
2+
3+
php:
4+
- 7.0
5+
- 7.1
6+
- 7.2
7+
8+
sudo: false
9+
10+
before_script:
11+
- travis_retry composer self-update
12+
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source
13+
14+
script:
15+
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover
16+
17+
after_script:
18+
- if [[ $TRAVIS_PHP_VERSION != 'hhvm' && $TRAVIS_PHP_VERSION != '7.0' ]]; then php vendor/bin/ocular code-coverage:upload --format=php-clover coverage.clover; fi

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Changelog
2+
3+
All Notable changes to `json-csv` will be documented in this file.
4+
5+
## 0.0.1
6+
- Initial release with basic conversion methods.

CONTRIBUTING.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Contributing
2+
3+
Contributions are **welcome** and will be fully **credited**.
4+
5+
We accept contributions via Pull Requests on [Github](https://github.com/ozdemirburak/json-csv).
6+
7+
8+
## Pull Requests
9+
10+
- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer).
11+
12+
- **Add tests!** - Your patch won't be accepted if it doesn't have tests.
13+
14+
- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.
15+
16+
- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option.
17+
18+
- **Create feature branches** - Don't ask us to pull from your master branch.
19+
20+
- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.
21+
22+
- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting.
23+
24+
25+
## Running Tests
26+
27+
``` bash
28+
$ composer test
29+
```
30+
31+
32+
**Happy coding**!

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# The MIT License (MIT)
2+
3+
Copyright (c) 2018 Burak Özdemir <[email protected]>
4+
5+
> Permission is hereby granted, free of charge, to any person obtaining a copy
6+
> of this software and associated documentation files (the "Software"), to deal
7+
> in the Software without restriction, including without limitation the rights
8+
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
> copies of the Software, and to permit persons to whom the Software is
10+
> furnished to do so, subject to the following conditions:
11+
>
12+
> The above copyright notice and this permission notice shall be included in
13+
> all copies or substantial portions of the Software.
14+
>
15+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
> THE SOFTWARE.

README.md

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
# JSON to CSV and CSV to JSON Converter
2+
3+
[![Latest Version on Packagist][ico-version]][link-packagist]
4+
[![Software License][ico-license]](LICENSE.md)
5+
[![Build Status][ico-travis]][link-travis]
6+
[![Total Downloads][ico-downloads]][link-downloads]
7+
8+
The most basic CSV to JSON and JSON to CSV converter library in PHP without any dependencies.
9+
10+
## Install
11+
12+
Via Composer
13+
14+
``` bash
15+
$ composer require ozdemirburak/json-csv
16+
```
17+
18+
## Usage
19+
20+
### JSON to CSV Converter
21+
22+
``` php
23+
use OzdemirBurak\JsonCsv\File\Json;
24+
25+
// JSON to CSV
26+
$json = new Json(__DIR__ . '/above.json');
27+
// To convert JSON to CSV string
28+
$csvString = $json->convert();
29+
// To convert JSON to CSV and save
30+
$json->convertAndSave(__DIR__ . '/above.csv');
31+
// To convert JSON to CSV and force download on browser
32+
$json->convertAndDownload();
33+
```
34+
35+
Assume that the input JSON is something like below.
36+
37+
```json
38+
[
39+
{
40+
"name": {
41+
"common": "Turkey",
42+
"official": "Republic of Turkey",
43+
"native": "T\u00fcrkiye"
44+
},
45+
"area": 783562,
46+
"latlng": [39, 35]
47+
},
48+
{
49+
"name": {
50+
"common": "Israel",
51+
"official": "State of Israel",
52+
"native": "\u05d9\u05e9\u05e8\u05d0\u05dc"
53+
},
54+
"area": 20770,
55+
"latlng": [31.30, 34.45]
56+
}
57+
]
58+
```
59+
60+
After the conversion, the resulting CSV data will look like below.
61+
62+
| name_common,name_official,name_native,area,latlng_0,latlng_1 |
63+
|--------------------------------------------------------------|
64+
| Turkey,"Republic of Turkey",Türkiye,783562,39,35 |
65+
| Israel,"State of Israel",ישראל,20770,31.3,34.45 |
66+
67+
68+
### CSV to JSON Converter
69+
70+
``` php
71+
use OzdemirBurak\JsonCsv\File\Csv;
72+
73+
// CSV to JSON
74+
$csv = new Csv(__DIR__ . '/below.csv');
75+
$csv->setConversionKey('options', JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
76+
// To convert CSV to JSON string
77+
$jsonString = $csv->convert();
78+
// To convert CSV to JSON and save
79+
$csv->convertAndSave(__DIR__ . '/below.csv');
80+
// To convert CSV to JSON and force download on browser
81+
$csv->convertAndDownload();
82+
```
83+
84+
Assume that the input CSV file is something like below.
85+
86+
| SepalLength,SepalWidth,PetalLength,PetalWidth,Name |
87+
|----------------------------------------------------|
88+
| 5.1,3.5,1.4,0.2,Iris-setosa |
89+
| 7.0,3.2,4.7,1.4,Iris-versicolor |
90+
| 6.3,3.3,6.0,2.5,Iris-virginica |
91+
92+
93+
After the conversion, the resulting JSON data will look like below.
94+
95+
```json
96+
[
97+
{
98+
"SepalLength": "5.1",
99+
"SepalWidth": "3.5",
100+
"PetalLength": "1.4",
101+
"PetalWidth": "0.2",
102+
"Name": "Iris-setosa"
103+
},
104+
{
105+
"SepalLength": "7.0",
106+
"SepalWidth": "3.2",
107+
"PetalLength": "4.7",
108+
"PetalWidth": "1.4",
109+
"Name": "Iris-versicolor"
110+
},
111+
{
112+
"SepalLength": "6.3",
113+
"SepalWidth": "3.3",
114+
"PetalLength": "6.0",
115+
"PetalWidth": "2.5",
116+
"Name": "Iris-virginica"
117+
}
118+
]
119+
```
120+
121+
## Change log
122+
123+
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
124+
125+
## Testing
126+
127+
``` bash
128+
$ composer test
129+
```
130+
131+
## Known Issues
132+
133+
Currently, it is assumed that each object shares the same properties while converting JSON to CSV. So if one object has a property that the other one does not have, then it will be a major problem.
134+
135+
## Contributing
136+
137+
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
138+
139+
## Credits
140+
141+
- [Burak Özdemir][link-author]
142+
- [All Contributors][link-contributors]
143+
144+
## License
145+
146+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
147+
148+
[ico-version]: https://img.shields.io/packagist/v/ozdemirburak/json-csv.svg?style=flat-square
149+
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
150+
[ico-travis]: https://img.shields.io/travis/ozdemirburak/json-csv/master.svg?style=flat-square
151+
[ico-downloads]: https://img.shields.io/packagist/dt/ozdemirburak/json-csv.svg?style=flat-square
152+
153+
[link-packagist]: https://packagist.org/packages/ozdemirburak/json-csv
154+
[link-travis]: https://travis-ci.org/ozdemirburak/json-csv
155+
[link-downloads]: https://packagist.org/packages/ozdemirburak/json-csv
156+
[link-author]: https://github.com/ozdemirburak
157+
[link-contributors]: ../../contributors

composer.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "ozdemirburak/json-csv",
3+
"description": "JSON to CSV and CSV to JSON converters in PHP.",
4+
"keywords": ["json", "csv", "json to csv", "csv to json", "json2csv", "csv2json"],
5+
"homepage": "https://github.com/ozdemirburak/json-csv",
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Burak Özdemir",
10+
"email": "[email protected]",
11+
"homepage": "https://burakozdemir.co.uk"
12+
}
13+
],
14+
"require": {
15+
"php" : "~7.0"
16+
},
17+
"require-dev": {
18+
"phpunit/phpunit" : "~5.0|~6.0"
19+
},
20+
"autoload": {
21+
"psr-4": { "OzdemirBurak\\JsonCsv\\": "src" }
22+
},
23+
"autoload-dev": {
24+
"psr-4": { "OzdemirBurak\\JsonCsv\\Tests\\": "tests" }
25+
},
26+
"scripts": { "test": "vendor/bin/phpunit" }
27+
}

phpunit.xml.dist

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php" stderr="true" backupGlobals="false" backupStaticAttributes="false" colors="true" verbose="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false">
3+
<testsuites>
4+
<testsuite name="JsonCsv PHP Test Suite">
5+
<directory>tests</directory>
6+
</testsuite>
7+
</testsuites>
8+
<filter>
9+
<whitelist>
10+
<directory suffix=".php">src/</directory>
11+
</whitelist>
12+
</filter>
13+
<logging>
14+
<log type="tap" target="build/report.tap"/>
15+
<log type="junit" target="build/report.junit.xml"/>
16+
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
17+
<log type="coverage-text" target="build/coverage.txt"/>
18+
<log type="coverage-clover" target="build/logs/clover.xml"/>
19+
</logging>
20+
</phpunit>

0 commit comments

Comments
 (0)