Skip to content

Commit 80358c9

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

File tree

4 files changed

+114
-31
lines changed

4 files changed

+114
-31
lines changed

README.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,33 @@
1313

1414
**search by a keyword in file**
1515

16+
```
17+
$fileHandler = new FileHandler();
1618
19+
$fileHandler->open(filename: 'movie.csv',mode:'r')->searchInCsvFile(keyword: 'Twilight',column:'Film');
20+
21+
```
1722

23+
**search by a keyword in file and return array**
1824

1925
```
2026
$fileHandler = new FileHandler();
2127
22-
$fileHandler->open(filename: 'movie.csv',mode:'r')->searchInCsvFile(keyword: 'Twilight');
28+
$fileHandler->open(filename: 'movie.csv',mode:'r')->searchInCsvFile(keyword: 'Zack and Miri Make a Porno',column:'Film', format: FileHandler::ARRAY_FORMAT);
2329
30+
// output
31+
32+
[
33+
'Film' => 'Zack and Miri Make a Porno',
34+
'Genre' => 'Romance',
35+
'Lead Studio' => 'The Weinstein Company',
36+
'Audience score %' => '70',
37+
'Profitability' => '1.747541667',
38+
'Rotten Tomatoes %' => '64',
39+
'Worldwide Gross' => '$41.94 ',
40+
'Year' => '2008'
41+
42+
];
2443
```
2544

2645
**Write multiple file simultaneously:**
@@ -37,3 +56,25 @@ fileHandler->write(data: "hello world");
3756
$fileHandler->close();
3857
3958
```
59+
60+
**converting file to an array**
61+
62+
```
63+
$fileHandler = new FileHandler();
64+
65+
$data = $fileHandler->open(filename: 'movie.csv',mode:'r')->toArray();
66+
67+
// output
68+
$data[0] = [
69+
'Film' => 'Zack and Miri Make a Porno',
70+
'Genre' => 'Romance',
71+
'Lead Studio' => 'The Weinstein Company',
72+
'Audience score %' => '70',
73+
'Profitability' => '1.747541667',
74+
'Rotten Tomatoes %' => '64',
75+
'Worldwide Gross' => '$41.94 ',
76+
'Year' => '2008'
77+
78+
];
79+
80+
```

invalid.csv

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/FileHandler.php

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
class FileHandler
1212
{
13+
const ARRAY_FORMAT = 'array';
1314

1415
private array $files = [];
1516

@@ -52,29 +53,37 @@ public function close(): void
5253
}
5354
}
5455

55-
public function searchInCsvFile(string $keyword, int $offset = 0): bool
56+
public function searchInCsvFile(string $keyword, string $column, string|null $format = null): bool
5657
{
57-
return $this->search($keyword, $offset);
58+
return $this->search($keyword, $column, $format);
59+
}
60+
61+
public function toArray(): array
62+
{
63+
return iterator_to_array($this->getRows());
5864
}
5965

6066
private function getRows(): Generator
6167
{
62-
foreach ($this->files as $file) {
63-
while (($row = fgetcsv($file)) !== false) {
64-
if (count($row) < 2 || !is_array($row)) {
65-
throw new InvalidFileException();
66-
}
67-
yield $row;
68-
}
69-
fclose($file);
68+
if (count($this->files) > 1) {
69+
throw new InvalidFileException("multiple files not allowed");
70+
}
71+
72+
$file = $this->files[0];
73+
$headers = fgetcsv($file);
74+
while (($row = fgetcsv($file)) !== false) {
75+
$item = array_combine($headers, $row);
76+
yield $item;
7077
}
78+
fclose($file);
7179
}
7280

73-
private function search(string $keyword, int $offset): bool
81+
82+
private function search(string $keyword, string $column, string|null $format): bool
7483
{
7584
foreach ($this->getRows() as $row) {
76-
if ($keyword === $row[$offset]) {
77-
return true;
85+
if ($keyword === $row[$column]) {
86+
return ($format === self::ARRAY_FORMAT) ? $row[$column] : true;
7887
}
7988
}
8089
return false;

tests/FileHandlerTest.php

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use PHPUnit\Framework\TestCase;
77
use rcsofttech85\FileHandler\Exception\CouldNotWriteFileException;
88
use rcsofttech85\FileHandler\Exception\FileNotFoundException;
9-
use rcsofttech85\FileHandler\Exception\InvalidFileException;
109
use rcsofttech85\FileHandler\FileHandler;
1110

1211
class FileHandlerTest extends TestCase
@@ -32,7 +31,6 @@ protected function tearDown(): void
3231

3332

3433
#[Test]
35-
#[TestDox("file was written successfully!")]
3634
public function file_successfully_written()
3735
{
3836
$this->fileHandler->open(filename: 'file');
@@ -43,7 +41,6 @@ public function file_successfully_written()
4341
}
4442

4543
#[Test]
46-
#[TestDox("should throw an exception if file is not found")]
4744
public function should_throw_exception_if_file_is_not_Found()
4845
{
4946
$this->expectException(FileNotFoundException::class);
@@ -52,7 +49,6 @@ public function should_throw_exception_if_file_is_not_Found()
5249
}
5350

5451
#[Test]
55-
#[TestDox("should throw an exception if file is not writable")]
5652
public function should_throw_exception_if_file_is_not_writable()
5753
{
5854
$this->fileHandler->open(filename: 'file', mode: 'r');
@@ -63,7 +59,6 @@ public function should_throw_exception_if_file_is_not_writable()
6359
}
6460

6561
#[Test]
66-
#[TestDox("multiple files can be written simultaneously")]
6762
public function multiple_file_can_be_written_simultaneously()
6863
{
6964
$this->fileHandler->open(filename: 'file');
@@ -81,7 +76,6 @@ public function multiple_file_can_be_written_simultaneously()
8176

8277

8378
#[Test]
84-
#[TestDox("checks if a movie exists in a collection by a name")]
8579
public function file_is_closed_properly()
8680
{
8781
$this->fileHandler->open(filename: 'file');
@@ -94,28 +88,69 @@ public function file_is_closed_properly()
9488

9589
#[Test]
9690
#[DataProvider('provide_movie_names')]
97-
#[TestDox('Movie with name $keyword exists in collection.')]
98-
public function movie_is_found_for_exact_name_match(string $keyword)
91+
#[TestDox('search result with name $keyword exists in file.')]
92+
public function result_found_for_exact_name_match(string $keyword)
9993
{
100-
$isMovieAvailable = $this->fileHandler->open(filename: 'movie.csv')->searchInCsvFile(keyword: $keyword);
94+
$isMovieAvailable = $this->fileHandler->open(filename: 'movie.csv')->searchInCsvFile(
95+
keyword: $keyword,
96+
column: 'Film'
97+
);
10198
$this->assertTrue($isMovieAvailable);
10299
}
103100

104101
#[Test]
105102
#[DataProvider('provide_studio_names')]
106-
#[TestDox('Studio with name $keyword exists in collection.')]
103+
#[TestDox('search result with name $keyword exists in file.')]
107104
public function studio_is_found_for_exact_name_match(string $keyword)
108105
{
109-
$isStudioFound = $this->fileHandler->open(filename: 'movie.csv')->searchInCsvFile(keyword: $keyword, offset: 2);
106+
$isStudioFound = $this->fileHandler->open(filename: 'movie.csv')->searchInCsvFile(
107+
keyword: $keyword,
108+
column: 'Lead Studio'
109+
);
110110
$this->assertTrue($isStudioFound);
111111
}
112112

113113
#[Test]
114-
public function should_throw_exception_if_not_valid_csv()
114+
public function to_array_method_returns_valid_array()
115115
{
116-
$this->expectException(InvalidFileException::class);
117-
$this->expectExceptionMessage("invalid file format");
118-
$this->fileHandler->open(filename: 'invalid.csv')->searchInCsvFile(keyword: 'hello');
116+
$data = $this->fileHandler->open(filename: 'movie.csv')->toArray();
117+
118+
$expected = [
119+
'Film' => 'Zack and Miri Make a Porno',
120+
'Genre' => 'Romance',
121+
'Lead Studio' => 'The Weinstein Company',
122+
'Audience score %' => '70',
123+
'Profitability' => '1.747541667',
124+
'Rotten Tomatoes %' => '64',
125+
'Worldwide Gross' => '$41.94 ',
126+
'Year' => '2008'
127+
128+
];
129+
130+
$this->assertEquals($expected, $data[0]);
131+
}
132+
133+
public function search_by_keyword_and_return_array()
134+
{
135+
$expected = [
136+
'Film' => 'Zack and Miri Make a Porno',
137+
'Genre' => 'Romance',
138+
'Lead Studio' => 'The Weinstein Company',
139+
'Audience score %' => '70',
140+
'Profitability' => '1.747541667',
141+
'Rotten Tomatoes %' => '64',
142+
'Worldwide Gross' => '$41.94 ',
143+
'Year' => '2008'
144+
145+
];
146+
147+
$data = $this->fileHandler->open(filename: 'movie.csv')->searchInCsvFile(
148+
keyword: 'Zack and Miri Make a Porno',
149+
column: 'Film',
150+
format: FileHandler::ARRAY_FORMAT
151+
);
152+
153+
$this->assertEquals($expected, $data);
119154
}
120155

121156
public static function provide_studio_names(): iterable

0 commit comments

Comments
 (0)