Skip to content

Commit

Permalink
Remove PHP 7.1 support, added utf-8 support for csv conversion #13
Browse files Browse the repository at this point in the history
  • Loading branch information
ozdemirburak committed Jan 29, 2020
1 parent 5037623 commit 92ef1d6
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
build
composer.lock
vendor
*.cache
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.1
- 7.2
- 7.3
- 7.4
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ use OzdemirBurak\JsonCsv\File\Json;
$json = new Json(__DIR__ . '/above.json');
// To convert JSON to CSV string
$csvString = $json->convert();
// To convert JSON to CSV and save
// To set a conversion option then convert JSON to CSV and save
$json->setConversionKey('utf8_encoding', true);
$json->convertAndSave(__DIR__ . '/above.csv');
// To convert JSON to CSV and force download on browser
$json->convertAndDownload();
Expand Down
12 changes: 9 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,23 @@
}
],
"require": {
"php" : "~7.1",
"php" : "~7.2",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit" : "~6.0|~7.0"
"phpunit/phpunit" : "~7.0|~8.0",
"squizlabs/php_codesniffer": "~3.5"
},
"autoload": {
"psr-4": { "OzdemirBurak\\JsonCsv\\": "src" }
},
"autoload-dev": {
"psr-4": { "OzdemirBurak\\JsonCsv\\Tests\\": "tests" }
},
"scripts": { "test": "vendor/bin/phpunit" }
"scripts": {
"test": "vendor/bin/phpunit",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage",
"check-style": "vendor/bin/phpcs -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests",
"fix-style": "vendor/bin/phpcbf -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests"
}
}
2 changes: 1 addition & 1 deletion src/AbstractFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ abstract class AbstractFile
*/
public function __construct($filepath)
{
list($this->filename, $this->data) = [pathinfo($filepath, PATHINFO_FILENAME), file_get_contents($filepath)];
[$this->filename, $this->data] = [pathinfo($filepath, PATHINFO_FILENAME), file_get_contents($filepath)];
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/File/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public function convert(): string
protected function toCsvString(array $data): string
{
$f = fopen('php://temp', 'wb');
if($this->conversion["utf8_encoding"]) {
fprintf($f, chr(0xEF).chr(0xBB).chr(0xBF));
if ($this->conversion['utf8_encoding']) {
fprintf($f, chr(0xEF) . chr(0xBB) . chr(0xBF));
}
$this->putCsv($f, array_keys(current($data)));
array_walk($data, function ($row) use (&$f) {
Expand Down
4 changes: 2 additions & 2 deletions tests/CsvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CsvTest extends TestCase
public function testFileReading()
{
$this->assertEquals('iris', ($csv = $this->initCsv())->getFilename());
$this->assertContains('6.3,3.3,6.0,2.5,Iris-virginica', $csv->getData());
$this->assertStringContainsString('6.3,3.3,6.0,2.5,Iris-virginica', $csv->getData());
}

/**
Expand Down Expand Up @@ -51,7 +51,7 @@ public function testConversionAndSave()
$this->initCsv()->convertAndSave($path);
$this->assertFileExists($path);
$json = '{"SL":"6.3","SW":"3.3","PL":"6.0","PW":"2.5","Name":"Iris-virginica"}';
$this->assertContains($json, file_get_contents($path));
$this->assertStringContainsString($json, file_get_contents($path));
unlink($path);
$this->assertFileNotExists($path);
}
Expand Down
13 changes: 11 additions & 2 deletions tests/JsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,16 @@ class JsonTest extends TestCase
public function testFileReading()
{
$this->assertEquals('countries', ($json = $this->initJson())->getFilename());
$this->assertContains('"common": "Turkey"', $json->getData());
$this->assertStringContainsString('"common": "Turkey"', $json->getData());
}

/**
* @group json-basic-test
*/
public function testSetter()
{
$conversion = $this->initJson()->setConversionKey('utf8_encoding', true);
$this->assertEquals(true, $conversion['utf8_encoding']);
}

/**
Expand Down Expand Up @@ -64,7 +73,7 @@ public function testConversionAndSave()
$path = $this->path('iris', 'countries');
$this->initJson()->convertAndSave($path);
$this->assertFileExists($path);
$this->assertContains("Turkey,\"Republic of Turkey\",Türkiye,783562,39,35\n", file_get_contents($path));
$this->assertStringContainsString("Turkey,\"Republic of Turkey\",Türkiye,783562,39,35\n", file_get_contents($path));
unlink($path);
$this->assertFileNotExists($path);
}
Expand Down

0 comments on commit 92ef1d6

Please sign in to comment.