diff --git a/bin/view-json b/bin/view-json index 372fd0f..6af6b94 100755 --- a/bin/view-json +++ b/bin/view-json @@ -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"); @@ -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); diff --git a/src/CsvFileHandler.php b/src/CsvFileHandler.php index a7a6cd4..cd7c55e 100644 --- a/src/CsvFileHandler.php +++ b/src/CsvFileHandler.php @@ -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 ) { @@ -147,26 +150,6 @@ private function isValidCsvFileFormat(array $row): bool return true; } - /** - * @param array $headers - * @param array $hideColumns - * @return array,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 @@ -222,22 +205,6 @@ private function getRows(string $filename, array|false $hideColumns = false, int } - /** - * @param array $row - * @param array, 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 $row * @param string $keyword diff --git a/src/JsonFileHandler.php b/src/JsonFileHandler.php index 38290eb..f6aaad2 100644 --- a/src/JsonFileHandler.php +++ b/src/JsonFileHandler.php @@ -4,24 +4,43 @@ use Generator; use rcsofttech85\FileHandler\Exception\FileHandlerException; +use rcsofttech85\FileHandler\Utilities\RowColumnHelper; readonly class JsonFileHandler { + use RowColumnHelper; + /** - * @return array> + * @param string $filename + * @param array $headers + * @param array|false $hideColumns + * @param int|false $limit + * @return array * @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 $headers + * @param array|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'); } @@ -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; + } } } } diff --git a/src/Utilities/RowColumnHelper.php b/src/Utilities/RowColumnHelper.php new file mode 100644 index 0000000..96f392e --- /dev/null +++ b/src/Utilities/RowColumnHelper.php @@ -0,0 +1,43 @@ + $headers + * @param array $hideColumns + * @return array,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 $row + * @param array, 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); + } +} diff --git a/tests/unit/JsonFileHandlerTest.php b/tests/unit/JsonFileHandlerTest.php index 2584bc9..62584f0 100644 --- a/tests/unit/JsonFileHandlerTest.php +++ b/tests/unit/JsonFileHandlerTest.php @@ -50,6 +50,7 @@ public static function bookListProvider(): iterable { yield [ + [ [ 'title' => 'The Catcher in the Rye',