@@ -30,40 +30,58 @@ public function convert(): string
30
30
$ splitKeys = array_map (function ($ key ) {
31
31
return explode ($ this ->conversion ['join ' ], $ key );
32
32
}, $ keys );
33
+ return json_encode (array_map (function ($ line ) use ($ splitKeys ) {
34
+ return $ this ->getJsonObject ($ line , $ splitKeys );
35
+ }, $ data ), $ this ->conversion ['options ' ]);
36
+ }
33
37
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 ;
42
51
}
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 ;
47
55
}
48
56
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
50
64
{
51
65
$ keyPart = $ splitKey [$ splitKeyIndex ];
52
-
53
- if (count ($ splitKey ) > $ splitKeyIndex +1 ) {
66
+ if (\count ($ splitKey ) > $ splitKeyIndex + 1 ) {
54
67
if (!array_key_exists ($ keyPart , $ jsonObject )) {
55
68
$ jsonObject [$ keyPart ] = [];
56
69
}
57
70
$ this ->setJsonValue ($ splitKey , $ splitKeyIndex +1 , $ jsonObject [$ keyPart ], $ value );
58
71
} else {
59
- if ($ this ->conversion ['numbers ' ] == 'numbers ' && is_numeric ( $ value ) ) {
72
+ if (is_numeric ( $ value ) && $ this ->conversion ['numbers ' ] === 'numbers ' ) {
60
73
$ value = 0 + $ value ;
61
74
}
62
75
$ jsonObject [$ keyPart ] = $ value ;
63
76
}
64
77
}
65
78
66
- private function parseCsv ($ line )
79
+ /**
80
+ * @param $line
81
+ *
82
+ * @return array
83
+ */
84
+ private function parseCsv ($ line ): array
67
85
{
68
86
return str_getcsv (
69
87
$ line ,
@@ -73,7 +91,10 @@ private function parseCsv($line)
73
91
);
74
92
}
75
93
76
- private function parseData ()
94
+ /**
95
+ * @return array
96
+ */
97
+ private function parseData (): array
77
98
{
78
99
$ data = explode ("\n" , $ this ->data );
79
100
if (end ($ data ) === '' ) {
0 commit comments