Skip to content

Commit 1b496c6

Browse files
authored
handling json file (#32)
Signed-off-by: rahul <rcsofttech85@gmail.com>
1 parent 46cb339 commit 1b496c6

File tree

8 files changed

+256
-3
lines changed

8 files changed

+256
-3
lines changed

bin/view-csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ $command = (new SingleCommandApplication())
4848
return Command::FAILURE;
4949
}
5050

51-
$headers = array_keys($data[0]);
51+
$headers = array_keys(reset($data));
5252
$io->title($csvFile);
5353
$table = $io->createTable();
5454
$table->setHeaders($headers);

bin/view-json

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
use rcsofttech85\FileHandler\DI\ServiceContainer;
5+
use rcsofttech85\FileHandler\Exception\FileHandlerException;
6+
use rcsofttech85\FileHandler\JsonFileHandler;
7+
use Symfony\Component\Console\Command\Command;
8+
use Symfony\Component\Console\Input\InputArgument;
9+
use Symfony\Component\Console\Input\InputInterface;
10+
use Symfony\Component\Console\Input\InputOption;
11+
use Symfony\Component\Console\Output\OutputInterface;
12+
use Symfony\Component\Console\SingleCommandApplication;
13+
use Symfony\Component\Console\Style\SymfonyStyle;
14+
15+
require 'vendor/autoload.php';
16+
17+
$command = (new SingleCommandApplication())
18+
->addArgument('jsonFile', InputArgument::REQUIRED, 'json file name')
19+
->addOption('hide-column', null, InputOption::VALUE_REQUIRED, 'Columns to hide (comma-separated)')
20+
->addOption('limit', null, InputOption::VALUE_REQUIRED, 'limit number of records')
21+
->setCode(function (InputInterface $input, OutputInterface $output): int {
22+
$io = new SymfonyStyle($input, $output);
23+
$jsonFile = $input->getArgument('jsonFile');
24+
25+
if (!file_exists($jsonFile)) {
26+
$io->error("{$jsonFile} does not exists");
27+
return Command::FAILURE;
28+
}
29+
30+
$serviceContainer = new ServiceContainer();
31+
/** @var JsonFileHandler $jsonFileHandler */
32+
$jsonFileHandler = $serviceContainer->getContainerBuilder()->get('json_file_handler');
33+
34+
try {
35+
$data = $jsonFileHandler->toArray($jsonFile);
36+
} catch (FileHandlerException) {
37+
$io->error('invalid json file');
38+
return Command::FAILURE;
39+
}
40+
41+
$headers = array_keys(reset($data));
42+
$io->title($jsonFile);
43+
$table = $io->createTable();
44+
$table->setHeaders($headers);
45+
$table->setRows($data);
46+
$table->render();
47+
$io->newLine();
48+
49+
return Command::SUCCESS;
50+
})->run();

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
},
3535
"bin": [
3636
"bin/file-diff",
37-
"bin/view-csv"
37+
"bin/view-csv",
38+
"bin/view-json"
3839
]
3940
}

src/JsonFileHandler.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace rcsofttech85\FileHandler;
4+
5+
use Generator;
6+
use rcsofttech85\FileHandler\Exception\FileHandlerException;
7+
8+
readonly class JsonFileHandler
9+
{
10+
/**
11+
* @return array<int,array<string,string>>
12+
* @throws FileHandlerException
13+
*/
14+
public function toArray(string $filename): array
15+
{
16+
return iterator_to_array($this->getRows($filename));
17+
}
18+
19+
/**
20+
* @return Generator
21+
* @throws FileHandlerException
22+
*/
23+
private function getRows(string $filename): Generator
24+
{
25+
if (!file_exists($filename)) {
26+
throw new FileHandlerException('file not found');
27+
}
28+
$jsonContents = file_get_contents($filename);
29+
30+
if (!$jsonContents) {
31+
throw new FileHandlerException("{$filename} is not valid");
32+
}
33+
34+
$contents = json_decode($jsonContents, true);
35+
if (!$contents || json_last_error() !== JSON_ERROR_NONE) {
36+
throw new FileHandlerException(json_last_error_msg());
37+
}
38+
39+
foreach ($contents as $content) {
40+
yield $content;
41+
}
42+
}
43+
}

src/config/services.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@ services:
2323
file_hash:
2424
class: 'rcsofttech85\FileHandler\FileHashChecker'
2525
arguments: [ '%filename%','@csv_file_handler' ]
26+
27+
json_file_handler:
28+
class: 'rcsofttech85\FileHandler\JsonFileHandler'
29+

tests/Integration/ViewCsvCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function viewCsvFileDisplayInformationCorrectly(string $file): void
6363
}
6464

6565
#[Test]
66-
public function IfLimitIsSetToNonNumericCommandShouldFail(): void
66+
public function ifLimitIsSetToNonNumericCommandShouldFail(): void
6767
{
6868
$command = "php bin/view-csv movie.csv --limit hello";
6969
exec($command, $output, $exitCode);
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Integration;
4+
5+
use PHPUnit\Framework\Attributes\DataProvider;
6+
use PHPUnit\Framework\Attributes\Test;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class ViewJsonCommandTest extends TestCase
10+
{
11+
/**
12+
* @return iterable<array<string>>
13+
*/
14+
public static function fileProvider(): iterable
15+
{
16+
$file = "book.json";
17+
$csvData = '[
18+
{
19+
"title": "The Catcher in the Rye",
20+
"author": "J.D. Salinger",
21+
"published_year": 1951
22+
},
23+
{
24+
"title": "To Kill a Mockingbird",
25+
"author": "Harper Lee",
26+
"published_year": 1960
27+
},
28+
{
29+
"title": "1984",
30+
"author": "George Orwell",
31+
"published_year": 1949
32+
}
33+
]';
34+
file_put_contents($file, $csvData);
35+
36+
yield [$file];
37+
}
38+
39+
#[Test]
40+
#[DataProvider('fileProvider')]
41+
public function viewJsonFileDisplayInformationCorrectly(string $file): void
42+
{
43+
$command = "php bin/view-json {$file}";
44+
exec($command, $output, $exitCode);
45+
$actualOutput = implode("\n", $output);
46+
47+
$this->assertSame(0, $exitCode);
48+
$this->assertStringContainsString($file, $actualOutput);
49+
$this->assertStringContainsString('The Catcher in the Rye', $actualOutput);
50+
unlink($file);
51+
}
52+
}

tests/unit/JsonFileHandlerTest.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
namespace unit;
4+
5+
use PHPUnit\Framework\Attributes\DataProvider;
6+
use PHPUnit\Framework\Attributes\Test;
7+
use PHPUnit\Framework\TestCase;
8+
use rcsofttech85\FileHandler\Exception\FileHandlerException;
9+
use rcsofttech85\FileHandler\JsonFileHandler;
10+
11+
class JsonFileHandlerTest extends TestCase
12+
{
13+
private JsonFileHandler|null $jsonFileHandler;
14+
15+
public static function setUpBeforeClass(): void
16+
{
17+
parent::setUpBeforeClass();
18+
19+
$content = '[
20+
{
21+
"title": "The Catcher in the Rye",
22+
"author": "J.D. Salinger",
23+
"published_year": 1951
24+
},
25+
{
26+
"title": "To Kill a Mockingbird",
27+
"author": "Harper Lee",
28+
"published_year": 1960
29+
},
30+
{
31+
"title": "1984",
32+
"author": "George Orwell",
33+
"published_year": 1949
34+
}
35+
]
36+
';
37+
file_put_contents("book.json", $content);
38+
}
39+
40+
public static function tearDownAfterClass(): void
41+
{
42+
parent::tearDownAfterClass();
43+
unlink('book.json');
44+
}
45+
46+
/**
47+
* @return array<int,array<int, array<int, array<string, string>>>>
48+
*/
49+
public static function bookListProvider(): iterable
50+
{
51+
yield
52+
[
53+
[
54+
[
55+
'title' => 'The Catcher in the Rye',
56+
'author' => 'J.D. Salinger',
57+
'published_year' => '1951'
58+
],
59+
[
60+
'title' => 'To Kill a Mockingbird',
61+
'author' => 'Harper Lee',
62+
'published_year' => '1960'
63+
],
64+
[
65+
'title' => '1984',
66+
'author' => 'George Orwell',
67+
'published_year' => '1949'
68+
],
69+
70+
71+
]
72+
73+
];
74+
}
75+
76+
/**
77+
* @param array<int,array<string,string>> $book
78+
* @return void
79+
* @throws FileHandlerException
80+
*/
81+
#[Test]
82+
#[DataProvider('bookListProvider')]
83+
public function jsonFormatToArray(array $book): void
84+
{
85+
$data = $this->jsonFileHandler->toArray('book.json');
86+
87+
$this->assertSame(3, count($data));
88+
$this->assertEquals($data, $book);
89+
}
90+
91+
protected function setUp(): void
92+
{
93+
parent::setUp();
94+
$this->jsonFileHandler = new JsonFileHandler();
95+
}
96+
97+
protected function tearDown(): void
98+
{
99+
parent::tearDown();
100+
101+
$this->jsonFileHandler = null;
102+
}
103+
}

0 commit comments

Comments
 (0)