66use  rcsofttech85 \FileHandler \Exception \FileHandlerException ;
77use  rcsofttech85 \FileHandler \Utilities \RowColumnHelper ;
88
9- readonly   class  JsonFileHandler
9+ class  JsonFileHandler
1010{
1111    use  RowColumnHelper;
1212
1515     * @param array<string> $headers 
1616     * @param array<string>|false $hideColumns 
1717     * @param int|false $limit 
18-      * @return array<string,string> 
18+      * @return array<int,array< string,string> > 
1919     * @throws FileHandlerException 
2020     */ 
2121    public  function  toArray (
@@ -29,81 +29,128 @@ public function toArray(
2929
3030    /** 
3131     * @param string $filename 
32-      * @param array<string> $headers 
33-      * @param array<string>|false $hideColumns 
34-      * @param int|false $limit 
35-      * @return Generator 
32+      * @return array<int,array<string,string>> 
3633     * @throws FileHandlerException 
3734     */ 
38-     private  function  getRows (
39-         string  $ filename ,
40-         array  &$ headers ,
41-         array |false  $ hideColumns  = false ,
42-         int |false  $ limit  = false 
43-     ): Generator   {
35+ 
36+     private  function  validateFile (string  $ filename ): array 
37+     {
38+         $ this  ->checkFileExistence ($ filename );
39+         $ jsonContents  = $ this  ->getFileContents ($ filename );
40+         $ contents  = $ this  ->parseJson ($ jsonContents );
41+         if  (!$ contents ) {
42+             throw  new  FileHandlerException ('could not parse json ' );
43+         }
44+         $ this  ->validateJsonData ($ contents );
45+         return  $ contents ;
46+     }
47+ 
48+     /** 
49+      * @param string $filename 
50+      * @return void 
51+      * @throws FileHandlerException 
52+      */ 
53+     private  function  checkFileExistence (string  $ filename ): void 
54+     {
4455        if  (!file_exists ($ filename )) {
45-             throw  new  FileHandlerException ('file  not found ' );
56+             throw  new  FileHandlerException ('File  not found ' );
4657        }
47-          $ jsonContents  =  file_get_contents ( $ filename ); 
58+     } 
4859
60+     /** 
61+      * @param string $filename 
62+      * @return string 
63+      * @throws FileHandlerException 
64+      */ 
65+     private  function  getFileContents (string  $ filename ): string 
66+     {
67+         $ jsonContents  = file_get_contents ($ filename );
4968        if  (!$ jsonContents ) {
5069            throw  new  FileHandlerException ("{$ filename } is not valid " );
5170        }
71+         return  $ jsonContents ;
72+     }
5273
74+     /** 
75+      * @param string $jsonData 
76+      * @return array<int,array<string,string>>|false 
77+      */ 
78+     private  function  parseJson (string  $ jsonData ): array |false 
79+     {
80+         $ data  = json_decode ($ jsonData , true );
81+         if  (json_last_error () !== JSON_ERROR_NONE  || !is_array ($ data )) {
82+             return  false ;
83+         }
84+         return  $ data ;
85+     }
5386
54-         if  (!$ contents  = $ this  ->isValidJson ($ jsonContents )) {
87+     /** 
88+      * @param array<int,array<string,string>>|false $data 
89+      * @return void 
90+      * @throws FileHandlerException 
91+      */ 
92+     private  function  validateJsonData (array |false  $ data ): void 
93+     {
94+         if  (empty ($ data ) || !is_array ($ data [0 ])) {
5595            throw  new  FileHandlerException (json_last_error_msg ());
5696        }
5797
98+         $ firstArrayKeys  = array_keys ($ data [0 ]);
99+ 
100+         foreach  ($ data  as  $ item ) {
101+             $ currentArrayKeys  = array_keys ($ item );
58102
103+             if  ($ firstArrayKeys  !== $ currentArrayKeys ) {
104+                 throw  new  FileHandlerException ('Inconsistent JSON data ' );
105+             }
106+         }
107+     }
108+ 
109+     /** 
110+      * @param array<int,array<string,string>> $contents 
111+      * @param array<int<0,max>,int> $indices 
112+      * @param int|false $limit 
113+      * @return Generator 
114+      */ 
115+     private  function  getProcessedContent (array  $ contents , array  $ indices , int |false  $ limit  = false ): Generator 
116+     {
59117        $ count  = 0 ;
60-         $ headers   = array_keys ( $ contents [ 0 ] );
61-          $ indices  =  is_array ( $ hideColumns ) ?  $ this -> setColumnsToHide ( $ headers ,  $ hideColumns ) : []; 
118+         $ shouldLimit   = is_int ( $ limit  );
119+ 
62120        foreach  ($ contents  as  $ content ) {
63121            if  (!empty ($ indices )) {
64122                $ content  = array_values ($ content );
65123                $ this  ->removeElementByIndex ($ content , $ indices );
66124            }
125+ 
67126            yield  $ content ;
68127            $ count ++;
69128
70-             if  (is_int ( $ limit )  && $ limit  < = $ count  ) {
129+             if  ($ shouldLimit   && $ count  > = $ limit  ) {
71130                break ;
72131            }
73132        }
74133    }
75134
76135    /** 
77-      * @param string $jsonData 
78-      * @return array<int,array<string,string>>|false 
136+      * @param string $filename 
137+      * @param array<string> $headers 
138+      * @param array<string,string>|false $hideColumns 
139+      * @param int|false $limit 
140+      * @return Generator 
141+      * @throws FileHandlerException 
79142     */ 
80-     private  function  isValidJson (string  $ jsonData ): array |false 
81-     {
82-         $ data  = json_decode ($ jsonData , true );
83- 
84-         if  (json_last_error () !== JSON_ERROR_NONE ) {
85-             return  false ;
86-         }
87- 
88- 
89-         if  (!is_array ($ data )) {
90-             return  false ;
91-         }
92- 
93-         if  (!isset ($ data [0 ]) || !is_array ($ data [0 ])) {
94-             return  false ;
95-         }
96- 
97-         $ firstArrayKeys  = array_keys ($ data [0 ]);
98- 
99-         foreach  ($ data  as  $ item ) {
100-             $ currentArrayKeys  = array_keys ($ item );
143+     public  function  getRows (
144+         string  $ filename ,
145+         array  &$ headers ,
146+         array |false  $ hideColumns  = false ,
147+         int |false  $ limit  = false 
148+     ): Generator   {
149+         $ contents  = $ this  ->validateFile ($ filename );
101150
102-             if  ($ firstArrayKeys  !== $ currentArrayKeys ) {
103-                 return  false ;
104-             }
105-         }
151+         $ headers  = array_keys ($ contents [0 ]);
152+         $ indices  = is_array ($ hideColumns ) ? $ this  ->setColumnsToHide ($ headers , $ hideColumns ) : [];
106153
107-         return  $ data  ;
154+         return  $ this -> getProcessedContent ( $ contents ,  $ indices ,  $ limit ) ;
108155    }
109156}
0 commit comments