@@ -99,26 +99,45 @@ public function __construct($data, $suffix = null, $prefix = null, $directory =
99
99
}
100
100
101
101
/**
102
- * Parses an array of key/value data that may contain keys in dot notation.
102
+ * Parses an array of key/value data into a nested array structure.
103
+ *
104
+ * The data may use keys in dot notation (#55). Values can also be arrays in
105
+ * case of multi value fields (#148). To make both distinguishable in the
106
+ * result array keys that represent field names are prefixed with `_`. This
107
+ * also allows for numeric field names (#260).
103
108
*
104
109
* For example an array like this:
105
110
*
106
111
* ```
107
112
* [
108
113
* 'a' => 'value a',
109
- * 'b.a' => 'value b.a',
110
- * 'b.b' => 'value b.b',
114
+ * 'b.x' => 'value b.x',
115
+ * 'b.y' => 'value b.y',
116
+ *
117
+ * 'c.0' => 'val c.0',
118
+ * 'c.1' => 'val c.1',
119
+ *
120
+ * 'd' => ['m1', 'm2'],
111
121
* ]
112
122
* ```
113
123
*
114
124
* Will become:
115
125
*
116
126
* ```
117
127
* [
118
- * 'a' => 'value a',
119
- * 'b' => [
120
- * 'a' => 'value b.a',
121
- * 'b' => 'value b.a',
128
+ * '_a' => 'value a',
129
+ * '_b' => [
130
+ * '_x' => 'value b.x',
131
+ * '_y' => 'value b.y',
132
+ * ],
133
+ * '_c' => [
134
+ * '_0' => 'value c.0',
135
+ * '_1' => 'value c.1',
136
+ * ],
137
+ * '_d' => [
138
+ * // notice the missing underscore in the keys
139
+ * 0 => 'm1',
140
+ * 1 => 'm2',
122
141
* ],
123
142
* ]
124
143
*
@@ -136,19 +155,19 @@ protected function parseData($data, $encoding)
136
155
$ key = mb_convert_encoding ($ key , 'UTF-8 ' , $ encoding );
137
156
$ value = mb_convert_encoding ($ value , 'UTF-8 ' , $ encoding );
138
157
}
139
- $ keyParts = explode ('. ' , $ key );
140
- $ lastPart = array_pop ($ keyParts );
141
- if (count ($ keyParts ) === 0 ) {
142
- $ result [$ lastPart ] = $ value ;
158
+ if (strpos ($ key , '. ' ) === false ) {
159
+ $ result ['_ ' . $ key ] = $ value ;
143
160
} else {
144
161
$ target = &$ result ;
162
+ $ keyParts = explode ('. ' , $ key );
163
+ $ lastPart = array_pop ($ keyParts );
145
164
foreach ($ keyParts as $ part ) {
146
- if (!isset ($ target [$ part ])) {
147
- $ target [$ part ] = array ();
165
+ if (!isset ($ target [' _ ' . $ part ])) {
166
+ $ target [' _ ' . $ part ] = array ();
148
167
}
149
- $ target = &$ target [$ part ];
168
+ $ target = &$ target [' _ ' . $ part ];
150
169
}
151
- $ target [$ lastPart ] = $ value ;
170
+ $ target [' _ ' . $ lastPart ] = $ value ;
152
171
}
153
172
}
154
173
return $ result ;
@@ -173,13 +192,13 @@ protected function writeXml($fields)
173
192
* Write the fields to the given filepointer
174
193
*
175
194
* @param int $fp
176
- * @param mixed[] $fields an array of field values. A value can also be
177
- * another array in which case a nested field is written .
195
+ * @param mixed[] $fields an array of field values as returned by
196
+ * `parseData()` .
178
197
*/
179
198
protected function writeFields ($ fp , $ fields )
180
199
{
181
200
foreach ($ fields as $ key => $ value ) {
182
- $ key = $ this ->xmlEncode ($ key );
201
+ $ key = $ this ->xmlEncode (substr ( $ key, 1 ) );
183
202
fwrite ($ fp , "<field name= \"$ key \"> \n" );
184
203
if (!is_array ($ value )) {
185
204
$ value = array ($ value );
0 commit comments