@@ -50,13 +50,14 @@ private function getRows(
50
50
throw new FileHandlerException ("{$ filename } is not valid " );
51
51
}
52
52
53
- $ contents = json_decode ( $ jsonContents , true );
54
- if (!$ contents || json_last_error () !== JSON_ERROR_NONE ) {
53
+
54
+ if (!$ contents = $ this -> isValidJson ( $ jsonContents ) ) {
55
55
throw new FileHandlerException (json_last_error_msg ());
56
56
}
57
57
58
+
58
59
$ count = 0 ;
59
- $ headers = array_keys (reset ( $ contents) );
60
+ $ headers = array_keys ($ contents[ 0 ] );
60
61
$ indices = is_array ($ hideColumns ) ? $ this ->setColumnsToHide ($ headers , $ hideColumns ) : [];
61
62
foreach ($ contents as $ content ) {
62
63
if (!empty ($ indices )) {
@@ -71,4 +72,38 @@ private function getRows(
71
72
}
72
73
}
73
74
}
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
+ }
74
109
}
0 commit comments