Skip to content

Commit 73eeddd

Browse files
authored
hide column feature
Signed-off-by: rahul <[email protected]>
1 parent a60876b commit 73eeddd

File tree

3 files changed

+102
-14
lines changed

3 files changed

+102
-14
lines changed

bin/view-csv

+13-10
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,25 @@ require 'vendor/autoload.php';
55

66
use rcsofttech85\FileHandler\CsvFileHandler;
77
use rcsofttech85\FileHandler\DI\ServiceContainer;
8+
use rcsofttech85\FileHandler\Exception\FileHandlerException;
89
use Symfony\Component\Console\Command\Command;
910
use Symfony\Component\Console\Input\InputArgument;
1011
use Symfony\Component\Console\Input\InputInterface;
12+
use Symfony\Component\Console\Input\InputOption;
1113
use Symfony\Component\Console\Output\OutputInterface;
1214
use Symfony\Component\Console\SingleCommandApplication;
1315
use Symfony\Component\Console\Style\SymfonyStyle;
1416

1517
$command = (new SingleCommandApplication())
1618
->addArgument('csvFile', InputArgument::REQUIRED, 'csv file name')
19+
->addOption('hide-column', null, InputOption::VALUE_REQUIRED, 'Columns to hide (comma-separated)')
1720
->setCode(function (InputInterface $input, OutputInterface $output): int {
1821
$io = new SymfonyStyle($input, $output);
1922
$csvFile = $input->getArgument('csvFile');
2023

24+
$hiddenColumns = $input->getOption('hide-column');
25+
26+
$hiddenColumnsArray = explode(',', $hiddenColumns);
2127

2228
if (!file_exists($csvFile)) {
2329
$io->error("{$csvFile} does not exists");
@@ -28,23 +34,20 @@ $command = (new SingleCommandApplication())
2834
/** @var CsvFileHandler $csvFileHandler */
2935
$csvFileHandler = $serviceContainer->getContainerBuilder()->get('csv_file_handler');
3036

31-
$io->title($csvFile);
32-
$table = $io->createTable();
33-
$headers = $csvFileHandler->extractHeader($csvFile);
34-
35-
if (!$headers) {
37+
try {
38+
$data = $csvFileHandler->toArray($csvFile, $hiddenColumnsArray);
39+
} catch (FileHandlerException) {
3640
$io->error('invalid csv file');
3741
return Command::FAILURE;
3842
}
3943

40-
44+
$headers = array_keys($data[0]);
45+
$io->title($csvFile);
46+
$table = $io->createTable();
4147
$table->setHeaders($headers);
42-
$table->setRows($csvFileHandler->toArray($csvFile));
43-
48+
$table->setRows($data);
4449
$table->render();
45-
4650
$io->newLine();
4751

48-
4952
return Command::SUCCESS;
5053
})->run();

src/CsvFileHandler.php

+42-4
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,17 @@ public function toJson(string $filename): string|false
4949

5050
/**
5151
* @param string $filename
52+
* @param array<string> $hideColumns
5253
* @return array<int,array<string,string>>
5354
* @throws FileHandlerException
5455
*/
55-
public function toArray(string $filename): array
56+
public function toArray(string $filename, array|false $hideColumns = false): array
5657
{
5758
if (!file_exists($filename)) {
5859
throw new FileHandlerException('file not found');
5960
}
6061

61-
return iterator_to_array($this->getRows($filename));
62+
return iterator_to_array($this->getRows($filename, $hideColumns));
6263
}
6364

6465
public function findAndReplaceInCsv(
@@ -107,7 +108,7 @@ public function findAndReplaceInCsv(
107108
* @return array<string>|false
108109
*/
109110

110-
public function extractHeader(mixed $file): array|false
111+
private function extractHeader(mixed $file): array|false
111112
{
112113
$headers = [];
113114
if (is_resource($file)) {
@@ -146,12 +147,34 @@ private function isValidCsvFileFormat(array $row): bool
146147
return true;
147148
}
148149

150+
/**
151+
* @param array<string> $row
152+
* @param array<string> $hideColumns
153+
* @return array<int<0, max>,int|string>
154+
*/
155+
private function setColumnsToHide(array &$row, array $hideColumns): array
156+
{
157+
$indices = [];
158+
if (!empty($hideColumns)) {
159+
foreach ($hideColumns as $hideColumn) {
160+
$index = array_search($hideColumn, $row);
161+
if ($index !== false) {
162+
$indices[] = $index;
163+
unset($row[$index]);
164+
}
165+
}
166+
$row = array_values($row);
167+
}
168+
return $indices;
169+
}
170+
149171
/**
150172
* @param string $filename
173+
* @param array<string>|false $hideColumns
151174
* @return Generator
152175
* @throws FileHandlerException
153176
*/
154-
private function getRows(string $filename): Generator
177+
private function getRows(string $filename, array|false $hideColumns = false): Generator
155178
{
156179
$csvFile = fopen($filename, 'r');
157180
if (!$csvFile) {
@@ -162,6 +185,10 @@ private function getRows(string $filename): Generator
162185
throw new FileHandlerException('could not extract header');
163186
}
164187

188+
if (is_array($hideColumns)) {
189+
$indices = $this->setColumnsToHide($headers, $hideColumns);
190+
}
191+
165192

166193
$isEmptyFile = true;
167194
try {
@@ -170,6 +197,17 @@ private function getRows(string $filename): Generator
170197
if (!$this->isValidCsvFileFormat($row)) {
171198
throw new FileHandlerException('invalid csv file format');
172199
}
200+
201+
if (!empty($indices)) {
202+
foreach ($indices as $index) {
203+
if (isset($row[$index])) {
204+
unset($row[$index]);
205+
}
206+
}
207+
208+
$row = array_values($row);
209+
}
210+
173211
$item = array_combine($headers, $row);
174212

175213
yield $item;

tests/unit/CsvFileHandlerTest.php

+47
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,20 @@ public function toArrayMethodReturnsValidArray(): void
111111
$this->assertEquals($expected, $data[0]);
112112
}
113113

114+
/**
115+
* @param array<string> $columnsToHide
116+
* @param array<string,string> $expected
117+
* @return void
118+
* @throws FileHandlerException
119+
*/
120+
#[Test]
121+
#[DataProvider('columnsToHideDataProvider')]
122+
public function toArrayMethodWithHideColumnsOptionReturnsValidArray(array $columnsToHide, array $expected): void
123+
{
124+
$data = $this->csvFileHandler->toArray("movie.csv", $columnsToHide);
125+
$this->assertEquals($expected, $data[0]);
126+
}
127+
114128
#[Test]
115129
public function searchByKeywordAndReturnArray(): void
116130
{
@@ -220,4 +234,37 @@ public static function wrongColumnNameProvider(): iterable
220234
yield ["wrong"];
221235
yield ["honey bee"];
222236
}
237+
238+
/**
239+
* @return iterable<array<array<string>>>
240+
*/
241+
public static function columnsToHideDataProvider(): iterable
242+
{
243+
$hideSingleColumn = ["Film"];
244+
$expected1 = [
245+
'Genre' => 'Romance',
246+
'Lead Studio' => 'The Weinstein Company',
247+
'Audience score %' => '70',
248+
'Profitability' => '1.747541667',
249+
'Rotten Tomatoes %' => '64',
250+
'Worldwide Gross' => '$41.94 ',
251+
'Year' => '2008'
252+
253+
];
254+
255+
$hideMultipleColumns = ["Film", "Profitability", "Year"];
256+
$expected2 = [
257+
'Genre' => 'Romance',
258+
'Lead Studio' => 'The Weinstein Company',
259+
'Audience score %' => '70',
260+
'Rotten Tomatoes %' => '64',
261+
'Worldwide Gross' => '$41.94 ',
262+
263+
264+
];
265+
266+
267+
yield [$hideSingleColumn, $expected1];
268+
yield [$hideMultipleColumns, $expected2];
269+
}
223270
}

0 commit comments

Comments
 (0)