Skip to content

Commit

Permalink
v0.2, better csv to json conversion, removed support for php 7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ozdemirburak committed Oct 31, 2018
1 parent 6e31ee2 commit add96ef
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 32 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: php

php:
- 7.0
- 7.1
- 7.2

Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,15 @@

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

## 0.2.0
- Remove support for PHP 7.0
- Better CSV to JSON conversion method.

## 0.1.1
- Fix error with assoc. arrays (splat operator).

## 0.1.0
- Fix property error in JSON to CSV conversion.

## 0.0.1
- Initial release with basic conversion methods.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
}
],
"require": {
"php" : "~7.0",
"php" : "~7.1",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit" : "~5.0|~6.0"
"phpunit/phpunit" : "~6.0|~7.0"
},
"autoload": {
"psr-4": { "OzdemirBurak\\JsonCsv\\": "src" }
Expand Down
57 changes: 39 additions & 18 deletions src/File/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,40 +30,58 @@ public function convert(): string
$splitKeys = array_map(function ($key) {
return explode($this->conversion['join'], $key);
}, $keys);
return json_encode(array_map(function ($line) use ($splitKeys) {
return $this->getJsonObject($line, $splitKeys);
}, $data), $this->conversion['options']);
}

$jsonObjects = array_map(function ($line) use ($splitKeys) {
$values = $this->parseCsv($line);
$jsonObject = [];
for ($valueIndex = 0, $count = count($values); $valueIndex < $count; $valueIndex++) {
if ($values[$valueIndex] == "") {
continue;
}
$this->setJsonValue($splitKeys[$valueIndex], 0, $jsonObject, $values[$valueIndex]);
/**
* @param $line
* @param $splitKeys
* @param array $jsonObject
*
* @return array
*/
private function getJsonObject($line, $splitKeys, array $jsonObject = []): array
{
$values = $this->parseCsv($line);
for ($valueIndex = 0, $count = \count($values); $valueIndex < $count; $valueIndex++) {
if ($values[$valueIndex] === '') {
continue;
}
return $jsonObject;
}, $data);

return json_encode($jsonObjects, $this->conversion['options']);
$this->setJsonValue($splitKeys[$valueIndex], 0, $jsonObject, $values[$valueIndex]);
}
return $jsonObject;
}

private function setJsonValue($splitKey, $splitKeyIndex, &$jsonObject, $value)
/**
* @param $splitKey
* @param $splitKeyIndex
* @param $jsonObject
* @param $value
*/
private function setJsonValue($splitKey, $splitKeyIndex, &$jsonObject, $value): void
{
$keyPart = $splitKey[$splitKeyIndex];

if (count($splitKey) > $splitKeyIndex+1) {
if (\count($splitKey) > $splitKeyIndex + 1) {
if (!array_key_exists($keyPart, $jsonObject)) {
$jsonObject[$keyPart] = [];
}
$this->setJsonValue($splitKey, $splitKeyIndex+1, $jsonObject[$keyPart], $value);
} else {
if ($this->conversion['numbers'] == 'numbers' && is_numeric($value)) {
if (is_numeric($value) && $this->conversion['numbers'] === 'numbers') {
$value = 0 + $value;
}
$jsonObject[$keyPart] = $value;
}
}

private function parseCsv($line)
/**
* @param $line
*
* @return array
*/
private function parseCsv($line): array
{
return str_getcsv(
$line,
Expand All @@ -73,7 +91,10 @@ private function parseCsv($line)
);
}

private function parseData()
/**
* @return array
*/
private function parseData(): array
{
$data = explode("\n", $this->data);
if (end($data) === '') {
Expand Down
15 changes: 4 additions & 11 deletions tests/JsonReverseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,13 @@ class JsonReverseTest extends TestCase
private function checkReverseConversion($file, $join = '_')
{
$pathCsvOut = $this->path($file . '.out', 'csv');

$jsonConverter = $this->initJson($file);
$jsonConverter->setConversionKey('join', $join);
$jsonConverter->convertAndSave($pathCsvOut);

$pathJsonOut = $this->path($file . '.out', 'json');

$csvConverter = $this->initCsv($file . '.out');
$csvConverter->setConversionKey('join', $join);
$csvConverter->setConversionKey('numbers', 'numbers');
$csvConverter->convertAndSave($pathJsonOut);

$csvConverter->convertAndSave($pathJsonOut = $this->path($file . '.out', 'json'));
try {
$this->assertJsonFileEqualsJsonFile($this->path($file, 'json'), $pathJsonOut);
} finally {
Expand Down Expand Up @@ -63,14 +58,12 @@ public function testProperties()
}

/**
* @group json-conversion-test
* @TODO resolve issues which cause this test to fail
* @group json-conversion-failing-test
*/
/**
* @TODO resolve issues which cause this test to fail
*
public function testStats()
{
$this->markTestSkipped();
$this->checkReverseConversion('stats');
}
*/
}

0 comments on commit add96ef

Please sign in to comment.