Skip to content

Commit add96ef

Browse files
committed
v0.2, better csv to json conversion, removed support for php 7.0
1 parent 6e31ee2 commit add96ef

File tree

5 files changed

+55
-32
lines changed

5 files changed

+55
-32
lines changed

.travis.yml

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
language: php
22

33
php:
4-
- 7.0
54
- 7.1
65
- 7.2
76

CHANGELOG.md

+10
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,15 @@
22

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

5+
## 0.2.0
6+
- Remove support for PHP 7.0
7+
- Better CSV to JSON conversion method.
8+
9+
## 0.1.1
10+
- Fix error with assoc. arrays (splat operator).
11+
12+
## 0.1.0
13+
- Fix property error in JSON to CSV conversion.
14+
515
## 0.0.1
616
- Initial release with basic conversion methods.

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
}
1313
],
1414
"require": {
15-
"php" : "~7.0",
15+
"php" : "~7.1",
1616
"ext-json": "*"
1717
},
1818
"require-dev": {
19-
"phpunit/phpunit" : "~5.0|~6.0"
19+
"phpunit/phpunit" : "~6.0|~7.0"
2020
},
2121
"autoload": {
2222
"psr-4": { "OzdemirBurak\\JsonCsv\\": "src" }

src/File/Csv.php

+39-18
Original file line numberDiff line numberDiff line change
@@ -30,40 +30,58 @@ public function convert(): string
3030
$splitKeys = array_map(function ($key) {
3131
return explode($this->conversion['join'], $key);
3232
}, $keys);
33+
return json_encode(array_map(function ($line) use ($splitKeys) {
34+
return $this->getJsonObject($line, $splitKeys);
35+
}, $data), $this->conversion['options']);
36+
}
3337

34-
$jsonObjects = array_map(function ($line) use ($splitKeys) {
35-
$values = $this->parseCsv($line);
36-
$jsonObject = [];
37-
for ($valueIndex = 0, $count = count($values); $valueIndex < $count; $valueIndex++) {
38-
if ($values[$valueIndex] == "") {
39-
continue;
40-
}
41-
$this->setJsonValue($splitKeys[$valueIndex], 0, $jsonObject, $values[$valueIndex]);
38+
/**
39+
* @param $line
40+
* @param $splitKeys
41+
* @param array $jsonObject
42+
*
43+
* @return array
44+
*/
45+
private function getJsonObject($line, $splitKeys, array $jsonObject = []): array
46+
{
47+
$values = $this->parseCsv($line);
48+
for ($valueIndex = 0, $count = \count($values); $valueIndex < $count; $valueIndex++) {
49+
if ($values[$valueIndex] === '') {
50+
continue;
4251
}
43-
return $jsonObject;
44-
}, $data);
45-
46-
return json_encode($jsonObjects, $this->conversion['options']);
52+
$this->setJsonValue($splitKeys[$valueIndex], 0, $jsonObject, $values[$valueIndex]);
53+
}
54+
return $jsonObject;
4755
}
4856

49-
private function setJsonValue($splitKey, $splitKeyIndex, &$jsonObject, $value)
57+
/**
58+
* @param $splitKey
59+
* @param $splitKeyIndex
60+
* @param $jsonObject
61+
* @param $value
62+
*/
63+
private function setJsonValue($splitKey, $splitKeyIndex, &$jsonObject, $value): void
5064
{
5165
$keyPart = $splitKey[$splitKeyIndex];
52-
53-
if (count($splitKey) > $splitKeyIndex+1) {
66+
if (\count($splitKey) > $splitKeyIndex + 1) {
5467
if (!array_key_exists($keyPart, $jsonObject)) {
5568
$jsonObject[$keyPart] = [];
5669
}
5770
$this->setJsonValue($splitKey, $splitKeyIndex+1, $jsonObject[$keyPart], $value);
5871
} else {
59-
if ($this->conversion['numbers'] == 'numbers' && is_numeric($value)) {
72+
if (is_numeric($value) && $this->conversion['numbers'] === 'numbers') {
6073
$value = 0 + $value;
6174
}
6275
$jsonObject[$keyPart] = $value;
6376
}
6477
}
6578

66-
private function parseCsv($line)
79+
/**
80+
* @param $line
81+
*
82+
* @return array
83+
*/
84+
private function parseCsv($line): array
6785
{
6886
return str_getcsv(
6987
$line,
@@ -73,7 +91,10 @@ private function parseCsv($line)
7391
);
7492
}
7593

76-
private function parseData()
94+
/**
95+
* @return array
96+
*/
97+
private function parseData(): array
7798
{
7899
$data = explode("\n", $this->data);
79100
if (end($data) === '') {

tests/JsonReverseTest.php

+4-11
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,13 @@ class JsonReverseTest extends TestCase
1616
private function checkReverseConversion($file, $join = '_')
1717
{
1818
$pathCsvOut = $this->path($file . '.out', 'csv');
19-
2019
$jsonConverter = $this->initJson($file);
2120
$jsonConverter->setConversionKey('join', $join);
2221
$jsonConverter->convertAndSave($pathCsvOut);
23-
24-
$pathJsonOut = $this->path($file . '.out', 'json');
25-
2622
$csvConverter = $this->initCsv($file . '.out');
2723
$csvConverter->setConversionKey('join', $join);
2824
$csvConverter->setConversionKey('numbers', 'numbers');
29-
$csvConverter->convertAndSave($pathJsonOut);
30-
25+
$csvConverter->convertAndSave($pathJsonOut = $this->path($file . '.out', 'json'));
3126
try {
3227
$this->assertJsonFileEqualsJsonFile($this->path($file, 'json'), $pathJsonOut);
3328
} finally {
@@ -63,14 +58,12 @@ public function testProperties()
6358
}
6459

6560
/**
66-
* @group json-conversion-test
61+
* @TODO resolve issues which cause this test to fail
62+
* @group json-conversion-failing-test
6763
*/
68-
/**
69-
* @TODO resolve issues which cause this test to fail
70-
*
7164
public function testStats()
7265
{
66+
$this->markTestSkipped();
7367
$this->checkReverseConversion('stats');
7468
}
75-
*/
7669
}

0 commit comments

Comments
 (0)