Skip to content

Commit cd9334c

Browse files
committed
check if the file is a valid csv
Signed-off-by: rahul <[email protected]>
1 parent eff9156 commit cd9334c

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

src/FileHandler.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ class FileHandler
1414

1515
private array $files = [];
1616

17+
/**
18+
* @throws FileNotFoundException
19+
*/
1720
public function open(
1821
string $filename,
1922
string $mode = "r+",
@@ -32,6 +35,9 @@ public function open(
3235
}
3336

3437

38+
/**
39+
* @throws CouldNotWriteFileException
40+
*/
3541
public function write(string $data, ?int $length = null): void
3642
{
3743
foreach ($this->files as $file) {
@@ -44,6 +50,9 @@ public function write(string $data, ?int $length = null): void
4450
}
4551
}
4652

53+
/**
54+
* @throws FileNotClosedException
55+
*/
4756
public function close(): void
4857
{
4958
foreach ($this->files as $file) {
@@ -63,6 +72,9 @@ public function toArray(): array
6372
return iterator_to_array($this->getRows());
6473
}
6574

75+
/**
76+
* @throws InvalidFileException
77+
*/
6678
private function getRows(): Generator
6779
{
6880
if (count($this->files) > 1) {
@@ -71,14 +83,27 @@ private function getRows(): Generator
7183

7284
$file = $this->files[0];
7385
$headers = fgetcsv($file);
86+
87+
$this->isValidCsvFileFormat($headers);
88+
89+
$isEmptyFile = true;
7490
while (($row = fgetcsv($file)) !== false) {
91+
$isEmptyFile = false;
92+
$this->isValidCsvFileFormat($row);
7593
$item = array_combine($headers, $row);
7694
yield $item;
7795
}
7896
fclose($file);
97+
98+
if ($isEmptyFile) {
99+
throw new InvalidFileException('invalid file format');
100+
}
79101
}
80102

81103

104+
/**
105+
* @throws InvalidFileException
106+
*/
82107
private function search(string $keyword, string $column, string|null $format): bool|array
83108
{
84109
foreach ($this->getRows() as $row) {
@@ -88,4 +113,14 @@ private function search(string $keyword, string $column, string|null $format): b
88113
}
89114
return false;
90115
}
116+
117+
/**
118+
* @throws InvalidFileException
119+
*/
120+
private function isValidCsvFileFormat(array|false $row): void
121+
{
122+
if (!$row || count($row) <= 1) {
123+
throw new InvalidFileException('invalid file format');
124+
}
125+
}
91126
}

tests/unit/FileHandlerTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PHPUnit\Framework\TestCase;
99
use rcsofttech85\FileHandler\Exception\CouldNotWriteFileException;
1010
use rcsofttech85\FileHandler\Exception\FileNotFoundException;
11+
use rcsofttech85\FileHandler\Exception\InvalidFileException;
1112
use rcsofttech85\FileHandler\FileHandler;
1213
use TypeError;
1314

@@ -131,6 +132,23 @@ public function toArrayMethodReturnsValidArray()
131132
$this->assertEquals($expected, $data[0]);
132133
}
133134

135+
#[Test]
136+
#[DataProvider('fileProvider')]
137+
public function throwErrorIfFileFormatIsInvalid(string $file)
138+
{
139+
$this->expectException(InvalidFileException::class);
140+
$this->expectExceptionMessage('invalid file format');
141+
142+
try {
143+
$this->fileHandler->open($file)->searchInCsvFile(
144+
keyword: 'Twilight',
145+
column: 'Summit'
146+
);
147+
} finally {
148+
unlink($file);
149+
}
150+
}
151+
134152
#[Test]
135153
public function searchByKeywordAndReturnArray()
136154
{
@@ -169,4 +187,20 @@ public static function provideMovieNames(): iterable
169187
yield ["Leap Year"];
170188
yield ["Twilight"];
171189
}
190+
191+
public static function fileProvider(): iterable
192+
{
193+
$file1 = 'file1.txt';
194+
$file2 = 'file2.txt';
195+
$file3 = 'file3.txt';
196+
197+
file_put_contents($file1, "film,year");
198+
file_put_contents($file2, "film\nyear");
199+
file_put_contents($file3, "Film");
200+
201+
202+
yield [$file1];
203+
yield [$file2];
204+
yield [$file3];
205+
}
172206
}

0 commit comments

Comments
 (0)