Skip to content

Commit 84c3103

Browse files
committed
convert a file to an array
Signed-off-by: rahul <[email protected]>
1 parent 1948adc commit 84c3103

File tree

4 files changed

+66
-9
lines changed

4 files changed

+66
-9
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,25 @@ fileHandler->write(data: "hello world");
3737
$fileHandler->close();
3838
3939
```
40+
41+
**converting file to an array**
42+
43+
```
44+
$fileHandler = new FileHandler();
45+
46+
$data = $fileHandler->open(filename: 'movie.csv',mode:'r')->toArray();
47+
48+
// output
49+
$data[0] = [
50+
'Film' => 'Zack and Miri Make a Porno',
51+
'Genre' => 'Romance',
52+
'Lead Studio' => 'The Weinstein Company',
53+
'Audience score %' => '70',
54+
'Profitability' => '1.747541667',
55+
'Rotten Tomatoes %' => '64',
56+
'Worldwide Gross' => '$41.94 ',
57+
'Year' => '2008'
58+
59+
];
60+
61+
```

invalid.csv

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
rahul,chavan
2-
hello
1+
rahul

src/FileHandler.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,40 @@ public function searchInCsvFile(string $keyword, int $offset = 0): bool
5757
return $this->search($keyword, $offset);
5858
}
5959

60+
public function toArray(): array
61+
{
62+
if (count($this->files) > 2) {
63+
throw new InvalidFileException("multiple files not allowed");
64+
}
65+
66+
$headers = fgetcsv($this->files[0]);
67+
68+
$data = [];
69+
while (($row = fgetcsv($this->files[0])) !== false) {
70+
if (count($row) < 2 || !is_array($row)) {
71+
throw new InvalidFileException('not a valid csv file');
72+
}
73+
$item = array_combine($headers, $row);
74+
$data[] = $item;
75+
}
76+
77+
return $data;
78+
}
79+
6080
private function getRows(): Generator
6181
{
6282
foreach ($this->files as $file) {
6383
while (($row = fgetcsv($file)) !== false) {
6484
if (count($row) < 2 || !is_array($row)) {
65-
throw new InvalidFileException();
85+
throw new InvalidFileException('not a valid csv file');
6686
}
6787
yield $row;
6888
}
6989
fclose($file);
7090
}
7191
}
7292

93+
7394
private function search(string $keyword, int $offset): bool
7495
{
7596
foreach ($this->getRows() as $row) {

tests/FileHandlerTest.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ protected function tearDown(): void
3232

3333

3434
#[Test]
35-
#[TestDox("file was written successfully!")]
3635
public function file_successfully_written()
3736
{
3837
$this->fileHandler->open(filename: 'file');
@@ -43,7 +42,6 @@ public function file_successfully_written()
4342
}
4443

4544
#[Test]
46-
#[TestDox("should throw an exception if file is not found")]
4745
public function should_throw_exception_if_file_is_not_Found()
4846
{
4947
$this->expectException(FileNotFoundException::class);
@@ -52,7 +50,6 @@ public function should_throw_exception_if_file_is_not_Found()
5250
}
5351

5452
#[Test]
55-
#[TestDox("should throw an exception if file is not writable")]
5653
public function should_throw_exception_if_file_is_not_writable()
5754
{
5855
$this->fileHandler->open(filename: 'file', mode: 'r');
@@ -63,7 +60,6 @@ public function should_throw_exception_if_file_is_not_writable()
6360
}
6461

6562
#[Test]
66-
#[TestDox("multiple files can be written simultaneously")]
6763
public function multiple_file_can_be_written_simultaneously()
6864
{
6965
$this->fileHandler->open(filename: 'file');
@@ -81,7 +77,6 @@ public function multiple_file_can_be_written_simultaneously()
8177

8278

8379
#[Test]
84-
#[TestDox("checks if a movie exists in a collection by a name")]
8580
public function file_is_closed_properly()
8681
{
8782
$this->fileHandler->open(filename: 'file');
@@ -114,10 +109,30 @@ public function studio_is_found_for_exact_name_match(string $keyword)
114109
public function should_throw_exception_if_not_valid_csv()
115110
{
116111
$this->expectException(InvalidFileException::class);
117-
$this->expectExceptionMessage("invalid file format");
112+
$this->expectExceptionMessage("not a valid csv file");
118113
$this->fileHandler->open(filename: 'invalid.csv')->searchInCsvFile(keyword: 'hello');
119114
}
120115

116+
#[Test]
117+
public function to_array_method_returns_valid_array()
118+
{
119+
$data = $this->fileHandler->open(filename: 'movie.csv')->toArray();
120+
121+
$expected = [
122+
'Film' => 'Zack and Miri Make a Porno',
123+
'Genre' => 'Romance',
124+
'Lead Studio' => 'The Weinstein Company',
125+
'Audience score %' => '70',
126+
'Profitability' => '1.747541667',
127+
'Rotten Tomatoes %' => '64',
128+
'Worldwide Gross' => '$41.94 ',
129+
'Year' => '2008'
130+
131+
];
132+
133+
$this->assertEquals($expected, $data[0]);
134+
}
135+
121136
public static function provide_studio_names(): iterable
122137
{
123138
yield ["Fox"];

0 commit comments

Comments
 (0)