Skip to content

Commit 37069c6

Browse files
authored
view json file with hide column and limit option (#33)
Signed-off-by: rahul <[email protected]>
1 parent 1b496c6 commit 37069c6

File tree

5 files changed

+101
-44
lines changed

5 files changed

+101
-44
lines changed

bin/view-json

+17-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ $command = (new SingleCommandApplication())
2121
->setCode(function (InputInterface $input, OutputInterface $output): int {
2222
$io = new SymfonyStyle($input, $output);
2323
$jsonFile = $input->getArgument('jsonFile');
24+
$limit = $input->getOption('limit');
25+
$hiddenColumns = $input->getOption('hide-column');
26+
$hiddenColumns = $hiddenColumns ? explode(',', $hiddenColumns) : false;
2427

2528
if (!file_exists($jsonFile)) {
2629
$io->error("{$jsonFile} does not exists");
@@ -31,14 +34,26 @@ $command = (new SingleCommandApplication())
3134
/** @var JsonFileHandler $jsonFileHandler */
3235
$jsonFileHandler = $serviceContainer->getContainerBuilder()->get('json_file_handler');
3336

37+
if (isset($limit) && !is_numeric($limit)) {
38+
$io->error("{$limit} is not numeric");
39+
return Command::FAILURE;
40+
}
41+
42+
$limit = $limit ? (int)$limit : false;
43+
$headers = [];
3444
try {
35-
$data = $jsonFileHandler->toArray($jsonFile);
45+
$data = $jsonFileHandler->toArray(
46+
filename: $jsonFile,
47+
headers: $headers,
48+
hideColumns: $hiddenColumns,
49+
limit: $limit
50+
);
3651
} catch (FileHandlerException) {
3752
$io->error('invalid json file');
3853
return Command::FAILURE;
3954
}
4055

41-
$headers = array_keys(reset($data));
56+
4257
$io->title($jsonFile);
4358
$table = $io->createTable();
4459
$table->setHeaders($headers);

src/CsvFileHandler.php

+3-36
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44

55
use Generator;
66
use rcsofttech85\FileHandler\Exception\FileHandlerException;
7+
use rcsofttech85\FileHandler\Utilities\RowColumnHelper;
78

89
class CsvFileHandler
910
{
11+
use RowColumnHelper;
12+
1013
public function __construct(
1114
private readonly TempFileHandler $tempFileHandler
1215
) {
@@ -147,26 +150,6 @@ private function isValidCsvFileFormat(array $row): bool
147150
return true;
148151
}
149152

150-
/**
151-
* @param array<string> $headers
152-
* @param array<string> $hideColumns
153-
* @return array<int<0, max>,int>
154-
*/
155-
private function setColumnsToHide(array &$headers, array $hideColumns): array
156-
{
157-
$indices = [];
158-
if (!empty($hideColumns)) {
159-
foreach ($hideColumns as $hideColumn) {
160-
$index = array_search($hideColumn, $headers);
161-
if ($index !== false) {
162-
$indices[] = (int)$index;
163-
unset($headers[$index]);
164-
}
165-
}
166-
$headers = array_values($headers);
167-
}
168-
return $indices;
169-
}
170153

171154
/**
172155
* @param string $filename
@@ -222,22 +205,6 @@ private function getRows(string $filename, array|false $hideColumns = false, int
222205
}
223206

224207

225-
/**
226-
* @param array<int,string> $row
227-
* @param array<int<0, max>, int> $indices
228-
* @return void
229-
*/
230-
private function removeElementByIndex(array &$row, array $indices): void
231-
{
232-
foreach ($indices as $index) {
233-
if (isset($row[$index])) {
234-
unset($row[$index]);
235-
}
236-
}
237-
238-
$row = array_values($row);
239-
}
240-
241208
/**
242209
* @param array<string> $row
243210
* @param string $keyword

src/JsonFileHandler.php

+37-6
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,43 @@
44

55
use Generator;
66
use rcsofttech85\FileHandler\Exception\FileHandlerException;
7+
use rcsofttech85\FileHandler\Utilities\RowColumnHelper;
78

89
readonly class JsonFileHandler
910
{
11+
use RowColumnHelper;
12+
1013
/**
11-
* @return array<int,array<string,string>>
14+
* @param string $filename
15+
* @param array<string> $headers
16+
* @param array<string>|false $hideColumns
17+
* @param int|false $limit
18+
* @return array<string,string>
1219
* @throws FileHandlerException
1320
*/
14-
public function toArray(string $filename): array
15-
{
16-
return iterator_to_array($this->getRows($filename));
21+
public function toArray(
22+
string $filename,
23+
array &$headers = [],
24+
array|false $hideColumns = false,
25+
int|false $limit = false
26+
): array {
27+
return iterator_to_array($this->getRows($filename, $headers, $hideColumns, $limit));
1728
}
1829

1930
/**
31+
* @param string $filename
32+
* @param array<string> $headers
33+
* @param array<string>|false $hideColumns
34+
* @param int|false $limit
2035
* @return Generator
2136
* @throws FileHandlerException
2237
*/
23-
private function getRows(string $filename): Generator
24-
{
38+
private function getRows(
39+
string $filename,
40+
array &$headers,
41+
array|false $hideColumns = false,
42+
int|false $limit = false
43+
): Generator {
2544
if (!file_exists($filename)) {
2645
throw new FileHandlerException('file not found');
2746
}
@@ -36,8 +55,20 @@ private function getRows(string $filename): Generator
3655
throw new FileHandlerException(json_last_error_msg());
3756
}
3857

58+
$count = 0;
59+
$headers = array_keys(reset($contents));
60+
$indices = is_array($hideColumns) ? $this->setColumnsToHide($headers, $hideColumns) : [];
3961
foreach ($contents as $content) {
62+
if (!empty($indices)) {
63+
$content = array_values($content);
64+
$this->removeElementByIndex($content, $indices);
65+
}
4066
yield $content;
67+
$count++;
68+
69+
if (is_int($limit) && $limit <= $count) {
70+
break;
71+
}
4172
}
4273
}
4374
}

src/Utilities/RowColumnHelper.php

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace rcsofttech85\FileHandler\Utilities;
4+
5+
trait RowColumnHelper
6+
{
7+
/**
8+
* @param array<string> $headers
9+
* @param array<string> $hideColumns
10+
* @return array<int<0, max>,int>
11+
*/
12+
private function setColumnsToHide(array &$headers, array $hideColumns): array
13+
{
14+
$indices = [];
15+
if (!empty($hideColumns)) {
16+
foreach ($hideColumns as $hideColumn) {
17+
$index = array_search($hideColumn, $headers);
18+
if ($index !== false) {
19+
$indices[] = (int)$index;
20+
unset($headers[$index]);
21+
}
22+
}
23+
$headers = array_values($headers);
24+
}
25+
return $indices;
26+
}
27+
28+
/**
29+
* @param array<int,string> $row
30+
* @param array<int<0, max>, int> $indices
31+
* @return void
32+
*/
33+
private function removeElementByIndex(array &$row, array $indices): void
34+
{
35+
foreach ($indices as $index) {
36+
if (isset($row[$index])) {
37+
unset($row[$index]);
38+
}
39+
}
40+
41+
$row = array_values($row);
42+
}
43+
}

tests/unit/JsonFileHandlerTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public static function bookListProvider(): iterable
5050
{
5151
yield
5252
[
53+
5354
[
5455
[
5556
'title' => 'The Catcher in the Rye',

0 commit comments

Comments
 (0)