@@ -50,13 +50,14 @@ private function getRows(
5050 throw new FileHandlerException ("{$ filename } is not valid " );
5151 }
5252
53- $ contents = json_decode ( $ jsonContents , true );
54- if (!$ contents || json_last_error () !== JSON_ERROR_NONE ) {
53+
54+ if (!$ contents = $ this -> isValidJson ( $ jsonContents ) ) {
5555 throw new FileHandlerException (json_last_error_msg ());
5656 }
5757
58+
5859 $ count = 0 ;
59- $ headers = array_keys (reset ( $ contents) );
60+ $ headers = array_keys ($ contents[ 0 ] );
6061 $ indices = is_array ($ hideColumns ) ? $ this ->setColumnsToHide ($ headers , $ hideColumns ) : [];
6162 foreach ($ contents as $ content ) {
6263 if (!empty ($ indices )) {
@@ -71,4 +72,38 @@ private function getRows(
7172 }
7273 }
7374 }
75+
76+ /**
77+ * @param string $jsonData
78+ * @return array<int,array<string,string>>|false
79+ */
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 );
101+
102+ if ($ firstArrayKeys !== $ currentArrayKeys ) {
103+ return false ;
104+ }
105+ }
106+
107+ return $ data ;
108+ }
74109}
0 commit comments