44
55use Generator ;
66use rcsofttech85 \FileHandler \Exception \FileHandlerException ;
7+ use rcsofttech85 \FileHandler \Utilities \RowColumnHelper ;
78
89readonly class JsonFileHandler
910{
11+ use RowColumnHelper;
12+
1013 /**
11- * @return array<int,array<string,string>>
14+ * @param string $filename
15+ * @param array<string> $headers
16+ * @param array<string>|false $hideColumns
17+ * @param int|false $limit
18+ * @return array<string,string>
1219 * @throws FileHandlerException
1320 */
14- public function toArray (string $ filename ): array
15- {
16- return iterator_to_array ($ this ->getRows ($ filename ));
21+ public function toArray (
22+ string $ filename ,
23+ array &$ headers = [],
24+ array |false $ hideColumns = false ,
25+ int |false $ limit = false
26+ ): array {
27+ return iterator_to_array ($ this ->getRows ($ filename , $ headers , $ hideColumns , $ limit ));
1728 }
1829
1930 /**
31+ * @param string $filename
32+ * @param array<string> $headers
33+ * @param array<string>|false $hideColumns
34+ * @param int|false $limit
2035 * @return Generator
2136 * @throws FileHandlerException
2237 */
23- private function getRows (string $ filename ): Generator
24- {
38+ private function getRows (
39+ string $ filename ,
40+ array &$ headers ,
41+ array |false $ hideColumns = false ,
42+ int |false $ limit = false
43+ ): Generator {
2544 if (!file_exists ($ filename )) {
2645 throw new FileHandlerException ('file not found ' );
2746 }
@@ -36,8 +55,20 @@ private function getRows(string $filename): Generator
3655 throw new FileHandlerException (json_last_error_msg ());
3756 }
3857
58+ $ count = 0 ;
59+ $ headers = array_keys (reset ($ contents ));
60+ $ indices = is_array ($ hideColumns ) ? $ this ->setColumnsToHide ($ headers , $ hideColumns ) : [];
3961 foreach ($ contents as $ content ) {
62+ if (!empty ($ indices )) {
63+ $ content = array_values ($ content );
64+ $ this ->removeElementByIndex ($ content , $ indices );
65+ }
4066 yield $ content ;
67+ $ count ++;
68+
69+ if (is_int ($ limit ) && $ limit <= $ count ) {
70+ break ;
71+ }
4172 }
4273 }
4374}
0 commit comments