@@ -30,40 +30,58 @@ public function convert(): string
3030 $ splitKeys = array_map (function ($ key ) {
3131 return explode ($ this ->conversion ['join ' ], $ key );
3232 }, $ keys );
33+ return json_encode (array_map (function ($ line ) use ($ splitKeys ) {
34+ return $ this ->getJsonObject ($ line , $ splitKeys );
35+ }, $ data ), $ this ->conversion ['options ' ]);
36+ }
3337
34- $ jsonObjects = array_map (function ($ line ) use ($ splitKeys ) {
35- $ values = $ this ->parseCsv ($ line );
36- $ jsonObject = [];
37- for ($ valueIndex = 0 , $ count = count ($ values ); $ valueIndex < $ count ; $ valueIndex ++) {
38- if ($ values [$ valueIndex ] == "" ) {
39- continue ;
40- }
41- $ this ->setJsonValue ($ splitKeys [$ valueIndex ], 0 , $ jsonObject , $ values [$ valueIndex ]);
38+ /**
39+ * @param $line
40+ * @param $splitKeys
41+ * @param array $jsonObject
42+ *
43+ * @return array
44+ */
45+ private function getJsonObject ($ line , $ splitKeys , array $ jsonObject = []): array
46+ {
47+ $ values = $ this ->parseCsv ($ line );
48+ for ($ valueIndex = 0 , $ count = \count ($ values ); $ valueIndex < $ count ; $ valueIndex ++) {
49+ if ($ values [$ valueIndex ] === '' ) {
50+ continue ;
4251 }
43- return $ jsonObject ;
44- }, $ data );
45-
46- return json_encode ($ jsonObjects , $ this ->conversion ['options ' ]);
52+ $ this ->setJsonValue ($ splitKeys [$ valueIndex ], 0 , $ jsonObject , $ values [$ valueIndex ]);
53+ }
54+ return $ jsonObject ;
4755 }
4856
49- private function setJsonValue ($ splitKey , $ splitKeyIndex , &$ jsonObject , $ value )
57+ /**
58+ * @param $splitKey
59+ * @param $splitKeyIndex
60+ * @param $jsonObject
61+ * @param $value
62+ */
63+ private function setJsonValue ($ splitKey , $ splitKeyIndex , &$ jsonObject , $ value ): void
5064 {
5165 $ keyPart = $ splitKey [$ splitKeyIndex ];
52-
53- if (count ($ splitKey ) > $ splitKeyIndex +1 ) {
66+ if (\count ($ splitKey ) > $ splitKeyIndex + 1 ) {
5467 if (!array_key_exists ($ keyPart , $ jsonObject )) {
5568 $ jsonObject [$ keyPart ] = [];
5669 }
5770 $ this ->setJsonValue ($ splitKey , $ splitKeyIndex +1 , $ jsonObject [$ keyPart ], $ value );
5871 } else {
59- if ($ this ->conversion ['numbers ' ] == 'numbers ' && is_numeric ( $ value ) ) {
72+ if (is_numeric ( $ value ) && $ this ->conversion ['numbers ' ] === 'numbers ' ) {
6073 $ value = 0 + $ value ;
6174 }
6275 $ jsonObject [$ keyPart ] = $ value ;
6376 }
6477 }
6578
66- private function parseCsv ($ line )
79+ /**
80+ * @param $line
81+ *
82+ * @return array
83+ */
84+ private function parseCsv ($ line ): array
6785 {
6886 return str_getcsv (
6987 $ line ,
@@ -73,7 +91,10 @@ private function parseCsv($line)
7391 );
7492 }
7593
76- private function parseData ()
94+ /**
95+ * @return array
96+ */
97+ private function parseData (): array
7798 {
7899 $ data = explode ("\n" , $ this ->data );
79100 if (end ($ data ) === '' ) {
0 commit comments