Skip to content

Commit d51a095

Browse files
committed
search a keyword in csv file
Signed-off-by: rahul <rcsofttech85@gmail.com>
1 parent b5a9819 commit d51a095

File tree

5 files changed

+155
-4
lines changed

5 files changed

+155
-4
lines changed

invalid.csv

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

movie.csv

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
Film,Genre,Lead Studio,Audience score %,Profitability,Rotten Tomatoes %,Worldwide Gross,Year
2+
Zack and Miri Make a Porno,Romance,The Weinstein Company,70,1.747541667,64,$41.94 ,2008
3+
Youth in Revolt,Comedy,The Weinstein Company,52,1.09,68,$19.62 ,2010
4+
You Will Meet a Tall Dark Stranger,Comedy,Independent,35,1.211818182,43,$26.66 ,2010
5+
When in Rome,Comedy,Disney,44,0,15,$43.04 ,2010
6+
What Happens in Vegas,Comedy,Fox,72,6.267647029,28,$219.37 ,2008
7+
Water For Elephants,Drama,20th Century Fox,72,3.081421053,60,$117.09 ,2011
8+
WALL-E,Animation,Disney,89,2.896019067,96,$521.28 ,2008
9+
Waitress,Romance,Independent,67,11.0897415,89,$22.18 ,2007
10+
Waiting For Forever,Romance,Independent,53,0.005,6,$0.03 ,2011
11+
Valentine's Day,Comedy,Warner Bros.,54,4.184038462,17,$217.57 ,2010
12+
Tyler Perry's Why Did I get Married,Romance,Independent,47,3.7241924,46,$55.86 ,2007
13+
Twilight: Breaking Dawn,Romance,Independent,68,6.383363636,26,$702.17 ,2011
14+
Twilight,Romance,Summit,82,10.18002703,49,$376.66 ,2008
15+
The Ugly Truth,Comedy,Independent,68,5.402631579,14,$205.30 ,2009
16+
The Twilight Saga: New Moon,Drama,Summit,78,14.1964,27,$709.82 ,2009
17+
The Time Traveler's Wife,Drama,Paramount,65,2.598205128,38,$101.33 ,2009
18+
The Proposal,Comedy,Disney,74,7.8675,43,$314.70 ,2009
19+
The Invention of Lying,Comedy,Warner Bros.,47,1.751351351,56,$32.40 ,2009
20+
The Heartbreak Kid,Comedy,Paramount,41,2.129444167,30,$127.77 ,2007
21+
The Duchess,Drama,Paramount,68,3.207850222,60,$43.31 ,2008
22+
The Curious Case of Benjamin Button,Fantasy,Warner Bros.,81,1.78394375,73,$285.43 ,2008
23+
The Back-up Plan,Comedy,CBS,47,2.202571429,20,$77.09 ,2010
24+
Tangled,Animation,Disney,88,1.365692308,89,$355.01 ,2010
25+
Something Borrowed,Romance,Independent,48,1.719514286,15,$60.18 ,2011
26+
She's Out of My League,Comedy,Paramount,60,2.4405,57,$48.81 ,2010
27+
Sex and the City Two,Comedy,Warner Bros.,49,2.8835,15,$288.35 ,2010
28+
Sex and the City 2,Comedy,Warner Bros.,49,2.8835,15,$288.35 ,2010
29+
Sex and the City,Comedy,Warner Bros.,81,7.221795791,49,$415.25 ,2008
30+
Remember Me,Drama,Summit,70,3.49125,28,$55.86 ,2010
31+
Rachel Getting Married,Drama,Independent,61,1.384166667,85,$16.61 ,2008
32+
Penelope,Comedy,Summit,74,1.382799733,52,$20.74 ,2008
33+
P.S. I Love You,Romance,Independent,82,5.103116833,21,$153.09 ,2007
34+
Over Her Dead Body,Comedy,New Line,47,2.071,15,$20.71 ,2008
35+
Our Family Wedding,Comedy,Independent,49,0,14,$21.37 ,2010
36+
One Day,Romance,Independent,54,3.682733333,37,$55.24 ,2011
37+
Not Easily Broken,Drama,Independent,66,2.14,34,$10.70 ,2009
38+
No Reservations,Comedy,Warner Bros.,64,3.307180357,39,$92.60 ,2007
39+
Nick and Norah's Infinite Playlist,Comedy,Sony,67,3.3527293,73,$33.53 ,2008
40+
New Year's Eve,Romance,Warner Bros.,48,2.536428571,8,$142.04 ,2011
41+
My Week with Marilyn,Drama,The Weinstein Company,84,0.8258,83,$8.26 ,2011
42+
Music and Lyrics,Romance,Warner Bros.,70,3.64741055,63,$145.90 ,2007
43+
Monte Carlo,Romance,20th Century Fox,50,1.9832,38,$39.66 ,2011
44+
Miss Pettigrew Lives for a Day,Comedy,Independent,70,0.2528949,78,$15.17 ,2008
45+
Midnight in Paris,Romence,Sony,84,8.744705882,93,$148.66 ,2011
46+
Marley and Me,Comedy,Fox,77,3.746781818,63,$206.07 ,2008
47+
Mamma Mia!,Comedy,Universal,76,9.234453864,53,$609.47 ,2008
48+
Mamma Mia!,Comedy,Universal,76,9.234453864,53,$609.47 ,2008
49+
Made of Honor,Comdy,Sony,61,2.64906835,13,$105.96 ,2008
50+
Love Happens,Drama,Universal,40,2.004444444,18,$36.08 ,2009
51+
Love & Other Drugs,Comedy,Fox,55,1.817666667,48,$54.53 ,2010
52+
Life as We Know It,Comedy,Independent,62,2.530526316,28,$96.16 ,2010
53+
License to Wed,Comedy,Warner Bros.,55,1.9802064,8,$69.31 ,2007
54+
Letters to Juliet,Comedy,Summit,62,2.639333333,40,$79.18 ,2010
55+
Leap Year,Comedy,Universal,49,1.715263158,21,$32.59 ,2010
56+
Knocked Up,Comedy,Universal,83,6.636401848,91,$219 ,2007
57+
Killers,Action,Lionsgate,45,1.245333333,11,$93.40 ,2010
58+
Just Wright,Comedy,Fox,58,1.797416667,45,$21.57 ,2010
59+
Jane Eyre,Romance,Universal,77,0,85,$30.15 ,2011
60+
It's Complicated,Comedy,Universal,63,2.642352941,56,$224.60 ,2009
61+
I Love You Phillip Morris,Comedy,Independent,57,1.34,71,$20.10 ,2010
62+
High School Musical 3: Senior Year,Comedy,Disney,76,22.91313646,65,$252.04 ,2008
63+
He's Just Not That Into You,Comedy,Warner Bros.,60,7.1536,42,$178.84 ,2009
64+
Good Luck Chuck,Comedy,Lionsgate,61,2.36768512,3,$59.19 ,2007
65+
Going the Distance,Comedy,Warner Bros.,56,1.3140625,53,$42.05 ,2010
66+
Gnomeo and Juliet,Animation,Disney,52,5.387972222,56,$193.97 ,2011
67+
Gnomeo and Juliet,Animation,Disney,52,5.387972222,56,$193.97 ,2011
68+
Ghosts of Girlfriends Past,Comedy,Warner Bros.,47,2.0444,27,$102.22 ,2009
69+
Four Christmases,Comedy,Warner Bros.,52,2.022925,26,$161.83 ,2008
70+
Fireproof,Drama,Independent,51,66.934,40,$33.47 ,2008
71+
Enchanted,Comedy,Disney,80,4.005737082,93,$340.49 ,2007
72+
Dear John,Drama,Sony,66,4.5988,29,$114.97 ,2010
73+
Beginners,Comedy,Independent,80,4.471875,84,$14.31 ,2011
74+
Across the Universe,romance,Independent,84,0.652603178,54,$29.37 ,2007
75+
A Serious Man,Drama,Universal,64,4.382857143,89,$30.68 ,2009
76+
A Dangerous Method,Drama,Independent,89,0.44864475,79,$8.97 ,2011
77+
27 Dresses,Comedy,Fox,71,5.3436218,40,$160.31 ,2008
78+
(500) Days of Summer,comedy,Fox,81,8.096,87,$60.72 ,2009
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace rcsofttech85\FileHandler\Exception;
4+
5+
use Exception;
6+
use Throwable;
7+
8+
class InvalidFileException extends Exception
9+
{
10+
11+
public function __construct($message = "invalid file format", $code = 0, Throwable $previous = null)
12+
{
13+
parent::__construct($message, $code, $previous);
14+
}
15+
}

src/FileHandler.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace rcsofttech85\FileHandler;
44

5+
use Generator;
56
use rcsofttech85\FileHandler\Exception\CouldNotWriteFileException;
67
use rcsofttech85\FileHandler\Exception\FileNotClosedException;
78
use rcsofttech85\FileHandler\Exception\FileNotFoundException;
9+
use rcsofttech85\FileHandler\Exception\InvalidFileException;
810

911
class FileHandler
1012
{
@@ -50,4 +52,32 @@ public function close(): void
5052
}
5153
}
5254

55+
public function searchInCsvFile(string $keyword, int $offset = 0): bool
56+
{
57+
return $this->search($keyword, $offset);
58+
}
59+
60+
private function getRows(): Generator
61+
{
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);
70+
}
71+
}
72+
73+
private function search(string $keyword, int $offset): bool
74+
{
75+
foreach ($this->getRows() as $row) {
76+
if ($keyword === $row[$offset]) {
77+
return true;
78+
}
79+
}
80+
return false;
81+
}
82+
5383
}

tests/FileHandlerTest.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<?php
22

3+
use PHPUnit\Framework\Attributes\DataProvider;
34
use PHPUnit\Framework\Attributes\Test;
45
use PHPUnit\Framework\Attributes\TestDox;
56
use PHPUnit\Framework\TestCase;
67
use rcsofttech85\FileHandler\Exception\CouldNotWriteFileException;
78
use rcsofttech85\FileHandler\Exception\FileNotFoundException;
9+
use rcsofttech85\FileHandler\Exception\InvalidFileException;
810
use rcsofttech85\FileHandler\FileHandler;
911

1012
class FileHandlerTest extends TestCase
@@ -18,7 +20,6 @@ protected function setUp(): void
1820

1921
$this->fileHandler = new FileHandler();
2022
fopen(filename: "file", mode: "w");
21-
2223
}
2324

2425
protected function tearDown(): void
@@ -54,7 +55,7 @@ public function should_throw_exception_if_file_is_not_Found()
5455
#[TestDox("should throw an exception if file is not writable")]
5556
public function should_throw_exception_if_file_is_not_writable()
5657
{
57-
$this->fileHandler->open(filename: 'file',mode: 'r');
58+
$this->fileHandler->open(filename: 'file', mode: 'r');
5859

5960
$this->expectException(CouldNotWriteFileException::class);
6061
$this->expectExceptionMessage('Error writing to file');
@@ -67,7 +68,7 @@ public function multiple_file_can_be_written_simultaneously()
6768
{
6869
$this->fileHandler->open(filename: 'file');
6970

70-
$this->fileHandler->open(filename: 'file1',mode: 'w');
71+
$this->fileHandler->open(filename: 'file1', mode: 'w');
7172

7273
$this->fileHandler->write(data: "hello world");
7374

@@ -80,7 +81,7 @@ public function multiple_file_can_be_written_simultaneously()
8081

8182

8283
#[Test]
83-
#[TestDox("check if files are closed are properly")]
84+
#[TestDox("checks if a movie exists in a collection by a name")]
8485
public function file_is_closed_properly()
8586
{
8687
$this->fileHandler->open(filename: 'file');
@@ -91,4 +92,29 @@ public function file_is_closed_properly()
9192
$this->fileHandler->write(data: "fwrite(): supplied resource is not a valid stream resource");
9293
}
9394

95+
#[Test]
96+
#[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)
99+
{
100+
$isMovieAvailable = $this->fileHandler->open(filename: 'movie.csv')->searchInCsvFile(keyword: $keyword);
101+
$this->assertTrue($isMovieAvailable);
102+
}
103+
104+
#[Test]
105+
public function should_throw_exception_if_not_valid_csv()
106+
{
107+
$this->expectException(InvalidFileException::class);
108+
$this->expectExceptionMessage("invalid file format");
109+
$this->fileHandler->open(filename: 'invalid.csv')->searchInCsvFile(keyword: 'hello');
110+
}
111+
112+
113+
public static function provide_movie_names(): iterable
114+
{
115+
yield ["The Ugly Truth"];
116+
yield ["Leap Year"];
117+
yield ["Twilight"];
118+
}
119+
94120
}

0 commit comments

Comments
 (0)