6
6
use rcsofttech85 \FileHandler \Exception \FileHandlerException ;
7
7
use rcsofttech85 \FileHandler \Utilities \RowColumnHelper ;
8
8
9
- readonly class JsonFileHandler
9
+ class JsonFileHandler
10
10
{
11
11
use RowColumnHelper;
12
12
15
15
* @param array<string> $headers
16
16
* @param array<string>|false $hideColumns
17
17
* @param int|false $limit
18
- * @return array<string,string>
18
+ * @return array<int,array< string,string> >
19
19
* @throws FileHandlerException
20
20
*/
21
21
public function toArray (
@@ -29,81 +29,128 @@ public function toArray(
29
29
30
30
/**
31
31
* @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>>
36
33
* @throws FileHandlerException
37
34
*/
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
+ {
44
55
if (!file_exists ($ filename )) {
45
- throw new FileHandlerException ('file not found ' );
56
+ throw new FileHandlerException ('File not found ' );
46
57
}
47
- $ jsonContents = file_get_contents ( $ filename );
58
+ }
48
59
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 );
49
68
if (!$ jsonContents ) {
50
69
throw new FileHandlerException ("{$ filename } is not valid " );
51
70
}
71
+ return $ jsonContents ;
72
+ }
52
73
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
+ }
53
86
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 ])) {
55
95
throw new FileHandlerException (json_last_error_msg ());
56
96
}
57
97
98
+ $ firstArrayKeys = array_keys ($ data [0 ]);
99
+
100
+ foreach ($ data as $ item ) {
101
+ $ currentArrayKeys = array_keys ($ item );
58
102
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
+ {
59
117
$ count = 0 ;
60
- $ headers = array_keys ( $ contents [ 0 ] );
61
- $ indices = is_array ( $ hideColumns ) ? $ this -> setColumnsToHide ( $ headers , $ hideColumns ) : [];
118
+ $ shouldLimit = is_int ( $ limit );
119
+
62
120
foreach ($ contents as $ content ) {
63
121
if (!empty ($ indices )) {
64
122
$ content = array_values ($ content );
65
123
$ this ->removeElementByIndex ($ content , $ indices );
66
124
}
125
+
67
126
yield $ content ;
68
127
$ count ++;
69
128
70
- if (is_int ( $ limit ) && $ limit < = $ count ) {
129
+ if ($ shouldLimit && $ count > = $ limit ) {
71
130
break ;
72
131
}
73
132
}
74
133
}
75
134
76
135
/**
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
79
142
*/
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 );
101
150
102
- if ($ firstArrayKeys !== $ currentArrayKeys ) {
103
- return false ;
104
- }
105
- }
151
+ $ headers = array_keys ($ contents [0 ]);
152
+ $ indices = is_array ($ hideColumns ) ? $ this ->setColumnsToHide ($ headers , $ hideColumns ) : [];
106
153
107
- return $ data ;
154
+ return $ this -> getProcessedContent ( $ contents , $ indices , $ limit ) ;
108
155
}
109
156
}
0 commit comments