@@ -38,53 +38,27 @@ public function toArray(
3838 private function validateFile (string $ filename ): array
3939 {
4040 $ filename = $ this ->validateFileName ($ filename );
41- $ jsonContents = $ this ->getFileContents ($ filename );
42- $ contents = $ this ->parseJson ($ jsonContents );
43- if (!$ contents ) {
44- throw new FileHandlerException ('could not parse json ' );
45- }
46- $ this ->validateJsonData ($ contents );
47- return $ contents ;
41+ return $ this ->getValidJsonData ($ filename );
4842 }
4943
5044
5145 /**
5246 * @param string $filename
53- * @return string
47+ * @return array<int,array< string,string>>
5448 * @throws FileHandlerException
5549 */
56- private function getFileContents (string $ filename ): string
50+ public function getValidJsonData (string $ filename ): array
5751 {
5852 $ jsonContents = file_get_contents ($ filename );
5953 if (!$ jsonContents ) {
6054 throw new FileHandlerException ("{$ filename } is not valid " );
6155 }
62- return $ jsonContents ;
63- }
6456
65- /**
66- * @param string $jsonData
67- * @return array<int,array<string,string>>|false
68- */
69- private function parseJson (string $ jsonData ): array |false
70- {
71- $ data = json_decode ($ jsonData , true );
72- if (json_last_error () !== JSON_ERROR_NONE || !is_array ($ data )) {
73- return false ;
57+ $ data = json_decode ($ jsonContents , true );
58+ if (json_last_error () !== JSON_ERROR_NONE || !is_array ($ data [0 ])) {
59+ throw new FileHandlerException ('could not decode json ' );
7460 }
75- return $ data ;
76- }
7761
78- /**
79- * @param array<int,array<string,string>>|false $data
80- * @return void
81- * @throws FileHandlerException
82- */
83- private function validateJsonData (array |false $ data ): void
84- {
85- if (empty ($ data ) || !is_array ($ data [0 ])) {
86- throw new FileHandlerException (json_last_error_msg ());
87- }
8862
8963 $ firstArrayKeys = array_keys ($ data [0 ]);
9064
@@ -95,16 +69,30 @@ private function validateJsonData(array|false $data): void
9569 throw new FileHandlerException ('Inconsistent JSON data ' );
9670 }
9771 }
72+
73+ return $ data ;
9874 }
9975
10076 /**
101- * @param array<int,array<string,string>> $contents
102- * @param array<int<0,max>,int> $indices
77+ * @param string $filename
78+ * @param array<int,string> $headers
79+ * @param array<string>|false $hideColumns
10380 * @param int|false $limit
10481 * @return Generator
82+ * @throws FileHandlerException
10583 */
106- private function getProcessedContent (array $ contents , array $ indices , int |false $ limit = false ): Generator
84+ public function getRows (
85+ string $ filename ,
86+ array &$ headers ,
87+ array |false $ hideColumns = false ,
88+ int |false $ limit = false
89+ ): Generator
10790 {
91+ $ contents = $ this ->validateFile ($ filename );
92+
93+ $ headers = array_keys ($ contents [0 ]);
94+ $ indices = is_array ($ hideColumns ) ? $ this ->setColumnsToHide ($ headers , $ hideColumns ) : [];
95+
10896 $ count = 0 ;
10997 $ shouldLimit = is_int ($ limit );
11098
@@ -122,26 +110,4 @@ private function getProcessedContent(array $contents, array $indices, int|false
122110 }
123111 }
124112 }
125-
126- /**
127- * @param string $filename
128- * @param array<string> $headers
129- * @param array<string,string>|false $hideColumns
130- * @param int|false $limit
131- * @return Generator
132- * @throws FileHandlerException
133- */
134- public function getRows (
135- string $ filename ,
136- array &$ headers ,
137- array |false $ hideColumns = false ,
138- int |false $ limit = false
139- ): Generator {
140- $ contents = $ this ->validateFile ($ filename );
141-
142- $ headers = array_keys ($ contents [0 ]);
143- $ indices = is_array ($ hideColumns ) ? $ this ->setColumnsToHide ($ headers , $ hideColumns ) : [];
144-
145- return $ this ->getProcessedContent ($ contents , $ indices , $ limit );
146- }
147113}
0 commit comments