Skip to content

view json file with hide column and limit option #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions bin/view-json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ $command = (new SingleCommandApplication())
->setCode(function (InputInterface $input, OutputInterface $output): int {
$io = new SymfonyStyle($input, $output);
$jsonFile = $input->getArgument('jsonFile');
$limit = $input->getOption('limit');
$hiddenColumns = $input->getOption('hide-column');
$hiddenColumns = $hiddenColumns ? explode(',', $hiddenColumns) : false;

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

if (isset($limit) && !is_numeric($limit)) {
$io->error("{$limit} is not numeric");
return Command::FAILURE;
}

$limit = $limit ? (int)$limit : false;
$headers = [];
try {
$data = $jsonFileHandler->toArray($jsonFile);
$data = $jsonFileHandler->toArray(
filename: $jsonFile,
headers: $headers,
hideColumns: $hiddenColumns,
limit: $limit
);
} catch (FileHandlerException) {
$io->error('invalid json file');
return Command::FAILURE;
}

$headers = array_keys(reset($data));

$io->title($jsonFile);
$table = $io->createTable();
$table->setHeaders($headers);
Expand Down
39 changes: 3 additions & 36 deletions src/CsvFileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

use Generator;
use rcsofttech85\FileHandler\Exception\FileHandlerException;
use rcsofttech85\FileHandler\Utilities\RowColumnHelper;

class CsvFileHandler
{
use RowColumnHelper;

public function __construct(
private readonly TempFileHandler $tempFileHandler
) {
Expand Down Expand Up @@ -147,26 +150,6 @@ private function isValidCsvFileFormat(array $row): bool
return true;
}

/**
* @param array<string> $headers
* @param array<string> $hideColumns
* @return array<int<0, max>,int>
*/
private function setColumnsToHide(array &$headers, array $hideColumns): array
{
$indices = [];
if (!empty($hideColumns)) {
foreach ($hideColumns as $hideColumn) {
$index = array_search($hideColumn, $headers);
if ($index !== false) {
$indices[] = (int)$index;
unset($headers[$index]);
}
}
$headers = array_values($headers);
}
return $indices;
}

/**
* @param string $filename
Expand Down Expand Up @@ -222,22 +205,6 @@ private function getRows(string $filename, array|false $hideColumns = false, int
}


/**
* @param array<int,string> $row
* @param array<int<0, max>, int> $indices
* @return void
*/
private function removeElementByIndex(array &$row, array $indices): void
{
foreach ($indices as $index) {
if (isset($row[$index])) {
unset($row[$index]);
}
}

$row = array_values($row);
}

/**
* @param array<string> $row
* @param string $keyword
Expand Down
43 changes: 37 additions & 6 deletions src/JsonFileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,43 @@

use Generator;
use rcsofttech85\FileHandler\Exception\FileHandlerException;
use rcsofttech85\FileHandler\Utilities\RowColumnHelper;

readonly class JsonFileHandler
{
use RowColumnHelper;

/**
* @return array<int,array<string,string>>
* @param string $filename
* @param array<string> $headers
* @param array<string>|false $hideColumns
* @param int|false $limit
* @return array<string,string>
* @throws FileHandlerException
*/
public function toArray(string $filename): array
{
return iterator_to_array($this->getRows($filename));
public function toArray(
string $filename,
array &$headers = [],
array|false $hideColumns = false,
int|false $limit = false
): array {
return iterator_to_array($this->getRows($filename, $headers, $hideColumns, $limit));
}

/**
* @param string $filename
* @param array<string> $headers
* @param array<string>|false $hideColumns
* @param int|false $limit
* @return Generator
* @throws FileHandlerException
*/
private function getRows(string $filename): Generator
{
private function getRows(
string $filename,
array &$headers,
array|false $hideColumns = false,
int|false $limit = false
): Generator {
if (!file_exists($filename)) {
throw new FileHandlerException('file not found');
}
Expand All @@ -36,8 +55,20 @@ private function getRows(string $filename): Generator
throw new FileHandlerException(json_last_error_msg());
}

$count = 0;
$headers = array_keys(reset($contents));
$indices = is_array($hideColumns) ? $this->setColumnsToHide($headers, $hideColumns) : [];
foreach ($contents as $content) {
if (!empty($indices)) {
$content = array_values($content);
$this->removeElementByIndex($content, $indices);
}
yield $content;
$count++;

if (is_int($limit) && $limit <= $count) {
break;
}
}
}
}
43 changes: 43 additions & 0 deletions src/Utilities/RowColumnHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace rcsofttech85\FileHandler\Utilities;

trait RowColumnHelper
{
/**
* @param array<string> $headers
* @param array<string> $hideColumns
* @return array<int<0, max>,int>
*/
private function setColumnsToHide(array &$headers, array $hideColumns): array
{
$indices = [];
if (!empty($hideColumns)) {
foreach ($hideColumns as $hideColumn) {
$index = array_search($hideColumn, $headers);
if ($index !== false) {
$indices[] = (int)$index;
unset($headers[$index]);
}
}
$headers = array_values($headers);
}
return $indices;
}

/**
* @param array<int,string> $row
* @param array<int<0, max>, int> $indices
* @return void
*/
private function removeElementByIndex(array &$row, array $indices): void
{
foreach ($indices as $index) {
if (isset($row[$index])) {
unset($row[$index]);
}
}

$row = array_values($row);
}
}
1 change: 1 addition & 0 deletions tests/unit/JsonFileHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public static function bookListProvider(): iterable
{
yield
[

[
[
'title' => 'The Catcher in the Rye',
Expand Down