@@ -12,6 +12,14 @@ public function __construct(
12
12
) {
13
13
}
14
14
15
+ /**
16
+ * @param string $filename
17
+ * @param string $keyword
18
+ * @param string $column
19
+ * @param string|null $format
20
+ * @return bool|array<string,string>
21
+ * @throws FileHandlerException
22
+ */
15
23
public function searchInCsvFile (
16
24
string $ filename ,
17
25
string $ keyword ,
@@ -32,13 +40,18 @@ public function searchInCsvFile(
32
40
}
33
41
34
42
35
- public function toJson (string $ filename ): string
43
+ public function toJson (string $ filename ): string | false
36
44
{
37
45
$ data = $ this ->toArray ($ filename );
38
46
39
47
return json_encode ($ data );
40
48
}
41
49
50
+ /**
51
+ * @param string $filename
52
+ * @return array<int,array<string,string>>
53
+ * @throws FileHandlerException
54
+ */
42
55
public function toArray (string $ filename ): array
43
56
{
44
57
if (!file_exists ($ filename )) {
@@ -61,6 +74,9 @@ public function findAndReplaceInCsv(
61
74
}
62
75
63
76
$ tempFilePath = $ this ->tempFileHandler ->createTempFileWithHeaders ($ headers );
77
+ if (!$ tempFilePath ) {
78
+ return false ;
79
+ }
64
80
65
81
66
82
try {
@@ -86,22 +102,24 @@ public function findAndReplaceInCsv(
86
102
return true ;
87
103
}
88
104
105
+ /**
106
+ * @param mixed $file
107
+ * @return array<string>|false
108
+ */
109
+
89
110
private function extractHeader (mixed $ file ): array |false
90
111
{
91
112
$ headers = [];
92
113
if (is_resource ($ file )) {
93
114
$ headers = fgetcsv ($ file );
94
115
}
95
116
if (is_string ($ file )) {
96
- if (!file_exists ($ file )) {
117
+ $ file = fopen ($ file , 'r ' );
118
+ if (!$ file ) {
97
119
return false ;
98
120
}
99
- try {
100
- $ file = fopen ($ file , 'r ' );
101
- $ headers = fgetcsv ($ file );
102
- } finally {
103
- fclose ($ file );
104
- }
121
+ $ headers = fgetcsv ($ file );
122
+ fclose ($ file );
105
123
}
106
124
107
125
if (!$ headers ) {
@@ -116,6 +134,10 @@ private function extractHeader(mixed $file): array|false
116
134
return $ headers ;
117
135
}
118
136
137
+ /**
138
+ * @param array<string> $row
139
+ * @return bool
140
+ */
119
141
private function isValidCsvFileFormat (array $ row ): bool
120
142
{
121
143
if (count ($ row ) <= 1 ) {
@@ -124,10 +146,21 @@ private function isValidCsvFileFormat(array $row): bool
124
146
return true ;
125
147
}
126
148
149
+ /**
150
+ * @param string $filename
151
+ * @return Generator
152
+ * @throws FileHandlerException
153
+ */
127
154
private function getRows (string $ filename ): Generator
128
155
{
129
156
$ csvFile = fopen ($ filename , 'r ' );
157
+ if (!$ csvFile ) {
158
+ throw new FileHandlerException ('file not found ' );
159
+ }
130
160
$ headers = $ this ->extractHeader ($ csvFile );
161
+ if (!is_array ($ headers )) {
162
+ throw new FileHandlerException ('could not extract header ' );
163
+ }
131
164
132
165
133
166
$ isEmptyFile = true ;
@@ -151,6 +184,12 @@ private function getRows(string $filename): Generator
151
184
}
152
185
}
153
186
187
+ /**
188
+ * @param array<string> $row
189
+ * @param string $keyword
190
+ * @param string $replace
191
+ * @return int
192
+ */
154
193
private function replaceKeywordInRow (array &$ row , string $ keyword , string $ replace ): int
155
194
{
156
195
$ count = 0 ;
@@ -164,6 +203,13 @@ private function replaceKeywordInRow(array &$row, string $keyword, string $repla
164
203
return $ count ;
165
204
}
166
205
206
+ /**
207
+ * @param array<string> $row
208
+ * @param string $column
209
+ * @param string $keyword
210
+ * @param string $replace
211
+ * @return int
212
+ */
167
213
private function replaceKeywordInColumn (array &$ row , string $ column , string $ keyword , string $ replace ): int
168
214
{
169
215
$ count = 0 ;
0 commit comments