Skip to content

set limit option to restrict total number of records #29

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 20, 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
15 changes: 11 additions & 4 deletions bin/view-csv
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,32 @@ use Symfony\Component\Console\Style\SymfonyStyle;
$command = (new SingleCommandApplication())
->addArgument('csvFile', InputArgument::REQUIRED, 'csv file name')
->addOption('hide-column', null, InputOption::VALUE_REQUIRED, 'Columns to hide (comma-separated)')
->addOption('limit', null, InputOption::VALUE_REQUIRED, 'limit number of records')
->setCode(function (InputInterface $input, OutputInterface $output): int {
$io = new SymfonyStyle($input, $output);
$csvFile = $input->getArgument('csvFile');

$hiddenColumns = $input->getOption('hide-column');

$hiddenColumnsArray = explode(',', $hiddenColumns);
$limit = $input->getOption('limit');

if (!file_exists($csvFile)) {
$io->error("{$csvFile} does not exists");
return Command::FAILURE;
}

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

$hiddenColumns = $hiddenColumns ? explode(',', $hiddenColumns) : false;
$limit = $limit ? (int)$limit : false;

$serviceContainer = new ServiceContainer();
/** @var CsvFileHandler $csvFileHandler */
$csvFileHandler = $serviceContainer->getContainerBuilder()->get('csv_file_handler');

try {
$data = $csvFileHandler->toArray($csvFile, $hiddenColumnsArray);
$data = $csvFileHandler->toArray($csvFile, $hiddenColumns, $limit);
} catch (FileHandlerException) {
$io->error('invalid csv file');
return Command::FAILURE;
Expand Down
52 changes: 34 additions & 18 deletions src/CsvFileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ public function toJson(string $filename): string|false
* @return array<int,array<string,string>>
* @throws FileHandlerException
*/
public function toArray(string $filename, array|false $hideColumns = false): array
public function toArray(string $filename, array|false $hideColumns = false, int|false $limit = false): array
{
if (!file_exists($filename)) {
throw new FileHandlerException('file not found');
}

return iterator_to_array($this->getRows($filename, $hideColumns));
return iterator_to_array($this->getRows($filename, $hideColumns, $limit));
}

public function findAndReplaceInCsv(
Expand Down Expand Up @@ -148,22 +148,22 @@ private function isValidCsvFileFormat(array $row): bool
}

/**
* @param array<string> $row
* @param array<string> $headers
* @param array<string> $hideColumns
* @return array<int<0, max>,int|string>
* @return array<int<0, max>,int>
*/
private function setColumnsToHide(array &$row, array $hideColumns): array
private function setColumnsToHide(array &$headers, array $hideColumns): array
{
$indices = [];
if (!empty($hideColumns)) {
foreach ($hideColumns as $hideColumn) {
$index = array_search($hideColumn, $row);
$index = array_search($hideColumn, $headers);
if ($index !== false) {
$indices[] = $index;
unset($row[$index]);
$indices[] = (int)$index;
unset($headers[$index]);
}
}
$row = array_values($row);
$headers = array_values($headers);
}
return $indices;
}
Expand All @@ -174,7 +174,7 @@ private function setColumnsToHide(array &$row, array $hideColumns): array
* @return Generator
* @throws FileHandlerException
*/
private function getRows(string $filename, array|false $hideColumns = false): Generator
private function getRows(string $filename, array|false $hideColumns = false, int|false $limit = false): Generator
{
$csvFile = fopen($filename, 'r');
if (!$csvFile) {
Expand All @@ -191,6 +191,7 @@ private function getRows(string $filename, array|false $hideColumns = false): Ge


$isEmptyFile = true;
$count = 0;
try {
while (($row = fgetcsv($csvFile)) !== false) {
$isEmptyFile = false;
Expand All @@ -199,18 +200,17 @@ private function getRows(string $filename, array|false $hideColumns = false): Ge
}

if (!empty($indices)) {
foreach ($indices as $index) {
if (isset($row[$index])) {
unset($row[$index]);
}
}

$row = array_values($row);
$this->removeElementByIndex($row, $indices);
}

$item = array_combine($headers, $row);

$item = array_combine($headers, $row);
yield $item;
$count++;

if (is_int($limit) && $limit <= $count) {
break;
}
}
} finally {
fclose($csvFile);
Expand All @@ -222,6 +222,22 @@ private function getRows(string $filename, array|false $hideColumns = false): Ge
}
}

/**
* @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
13 changes: 12 additions & 1 deletion tests/Integration/ViewCsvCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,20 @@ public function viewCsvFileDisplayInformationCorrectly(string $file): void
unlink($file);
}

#[Test]
public function IfLimitIsSetToNonNumericCommandShouldFail(): void
{
$command = "php bin/view-csv movie.csv --limit hello";
exec($command, $output, $exitCode);
$actualOutput = implode("\n", $output);

$this->assertSame(1, $exitCode);
$this->assertStringContainsString("hello is not numeric", $actualOutput);
}

#[Test]
#[DataProvider('InvalidFileProvider')]
public function throwExceptionIfFileIsInvalid(string $file): void
public function commandShouldReturnErrorIfFileIsInvalid(string $file): void
{
$command = "php bin/view-csv {$file}";
exec($command, $output, $exitCode);
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/CsvFileHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,17 @@ public function toArrayMethodWithHideColumnsOptionReturnsValidArray(array $colum
$this->assertEquals($expected, $data[0]);
}

#[Test]
#[DataProvider('limitDataProvider')]
public function toArrayMethodShouldRestrictNumberOfRecordsWhenLimitIsSet(int $limit): void
{
$data = $this->csvFileHandler->toArray("movie.csv", ["Year"], $limit);

$count = count($data);

$this->assertSame($count, $limit);
}

#[Test]
public function searchByKeywordAndReturnArray(): void
{
Expand Down Expand Up @@ -267,4 +278,14 @@ public static function columnsToHideDataProvider(): iterable
yield [$hideSingleColumn, $expected1];
yield [$hideMultipleColumns, $expected2];
}

/**
* max limit for the test file is 3
* @return iterable<array<int>>
*/
public static function limitDataProvider(): iterable
{
yield [1];
yield [2];
}
}