@@ -40,18 +40,49 @@ public function encode($value, $depth, array $options, callable $encode)
40
40
implode (', ' , $ this ->getPairs ($ value , '' , $ options ['array.omit ' ], $ encode )),
41
41
$ options ['array.short ' ]
42
42
);
43
+ } elseif ($ options ['array.align ' ]) {
44
+ return $ this ->buildArray ($ this ->getAlignedPairs ($ value , $ encode ), $ depth , $ options );
43
45
}
44
46
45
- if (is_string ($ lines = $ this ->getLines ($ value , $ options , $ encode ))) {
46
- return $ lines ;
47
+ return $ this ->getFormattedArray ($ value , $ depth , $ options , $ encode );
48
+ }
49
+
50
+ private function getFormattedArray (array $ array , $ depth , $ options , $ encode )
51
+ {
52
+ $ lines = $ this ->getPairs ($ array , ' ' , $ options ['array.omit ' ], $ encode , $ omitted );
53
+
54
+ if ($ omitted && $ options ['array.inline ' ] !== false ) {
55
+ $ output = $ this ->getInlineArray ($ lines , $ options );
56
+
57
+ if ($ output !== false ) {
58
+ return $ output ;
59
+ }
60
+ }
61
+
62
+ return $ this ->buildArray ($ lines , $ depth , $ options );
63
+ }
64
+
65
+ private function getInlineArray ($ lines , $ options )
66
+ {
67
+ $ output = $ this ->wrap (implode (', ' , $ lines ), $ options ['array.short ' ]);
68
+
69
+ if (preg_match ('/[\r\n\t]/ ' , $ output )) {
70
+ return false ;
71
+ } elseif ($ options ['array.inline ' ] !== true && strlen ($ output ) > (int ) $ options ['array.inline ' ]) {
72
+ return false ;
47
73
}
48
74
75
+ return $ output ;
76
+ }
77
+
78
+ private function buildArray (array $ lines , $ depth , array $ options )
79
+ {
49
80
$ indent = $ this ->buildIndent ($ options ['array.base ' ], $ options ['array.indent ' ], $ depth + 1 );
50
81
$ last = $ this ->buildIndent ($ options ['array.base ' ], $ options ['array.indent ' ], $ depth );
51
82
$ eol = $ options ['array.eol ' ] === false ? PHP_EOL : (string ) $ options ['array.eol ' ];
52
83
53
84
return $ this ->wrap (
54
- $ eol . $ indent . implode (', ' . $ eol .$ indent , $ lines ) . ' , ' . $ eol . $ last ,
85
+ sprintf ( ' %s%s%s,%1$s%s ' , $ eol, $ indent, implode (', ' . $ eol . $ indent , $ lines ), $ last) ,
55
86
$ options ['array.short ' ]
56
87
);
57
88
}
@@ -85,38 +116,10 @@ private function buildIndent($base, $indent, $depth)
85
116
}
86
117
87
118
/**
88
- * Returns the code representation for the array values and keys.
89
- * @param array $array Array to convert into code
90
- * @param array $options List of encoder options
91
- * @param callable $encode Callback used to encode values
92
- * @return string|string[] Array encoded as strings or simple array as a string
93
- */
94
- private function getLines (array $ array , array $ options , callable $ encode )
95
- {
96
- if ($ options ['array.align ' ]) {
97
- return $ this ->getAlignedPairs ($ array , $ encode );
98
- }
99
-
100
- $ lines = $ this ->getPairs ($ array , ' ' , $ options ['array.omit ' ], $ encode , $ inline );
101
-
102
- if ($ inline && $ options ['array.inline ' ] !== false ) {
103
- $ output = $ this ->wrap (implode (', ' , $ lines ), $ options ['array.short ' ]);
104
-
105
- if (!preg_match ('/[\r\n\t]/ ' , $ output ) &&
106
- ($ options ['array.inline ' ] === true || strlen ($ output ) <= (int ) $ options ['array.inline ' ])
107
- ) {
108
- return $ output ;
109
- }
110
- }
111
-
112
- return $ lines ;
113
- }
114
-
115
- /**
116
- * Returns the array as aligned key and value pairs.
119
+ * Returns each encoded key and value pair with aligned assignment operators.
117
120
* @param array $array Array to convert into code
118
121
* @param callable $encode Callback used to encode values
119
- * @return string[] List of array key and value pairs as strings
122
+ * @return string[] Each of key and value pair encoded as php
120
123
*/
121
124
private function getAlignedPairs ($ array , callable $ encode )
122
125
{
@@ -139,27 +142,27 @@ private function getAlignedPairs($array, callable $encode)
139
142
}
140
143
141
144
/**
142
- * Encodes the array as code pairs according to given parameters
145
+ * Returns each key and value pair encoded as array assignment.
143
146
* @param array $array Array to convert into code
144
147
* @param string $space Whitespace between array assignment operator
145
148
* @param boolean $omit True to omit unnecessary keys, false to not
146
149
* @param callable $encode Callback used to encode values
147
- * @param boolean $inline Set to true, if all the keys were omitted, false otherwise *
148
- * @return string[] List of array key and value pairs as strings
150
+ * @param boolean $omitted Set to true, if all the keys were omitted, false otherwise
151
+ * @return string[] Each of key and value pair encoded as php
149
152
*/
150
- private function getPairs ($ array , $ space , $ omit , callable $ encode , & $ inline = true )
153
+ private function getPairs ($ array , $ space , $ omit , callable $ encode , & $ omitted = true )
151
154
{
152
155
$ pairs = [];
153
156
$ nextIndex = 0 ;
154
- $ inline = true ;
157
+ $ omitted = true ;
155
158
$ format = '%s ' . $ space . '=> ' . $ space . '%s ' ;
156
159
157
160
foreach ($ array as $ key => $ value ) {
158
161
if ($ key === $ nextIndex && $ omit ) {
159
162
$ pairs [] = $ encode ($ value , 1 );
160
163
} else {
161
164
$ pairs [] = sprintf ($ format , $ encode ($ key , 1 ), $ encode ($ value , 1 ));
162
- $ inline = false ;
165
+ $ omitted = false ;
163
166
}
164
167
165
168
if (is_int ($ key ) && $ key >= $ nextIndex ) {
0 commit comments