99 * This class represents a temporary XFDF file that can be used to fill a PDF
1010 * form with valid unicode characters.
1111 *
12+ * Form data must be passed to the constructor as an array in this form:
13+ *
14+ * ```
15+ * [
16+ * // Field name => field value
17+ * 'Firstname' => 'John',
18+ *
19+ * // Hierarchical/nested fields in dot notation
20+ * 'Address.Street' => 'Some Street',
21+ * 'Address.City' => 'Any City',
22+ *
23+ * // Multi value fields
24+ * 'Pets' => ['Cat', 'Mouse'],
25+ * ]
26+ * ```
27+ *
28+ * This will result in the following XML structure (header/footer omitted):
29+ *
30+ * ```
31+ * <field name="Firstname">
32+ * <Value>John</Value>
33+ * </field>
34+ * <field name="Address">
35+ * <field name="Street">
36+ * <Value>Some Street</Value>
37+ * </field>
38+ * <field name="City">
39+ * <Value>Any City</Value>
40+ * </field>
41+ * </field>
42+ * <field name="Pets">
43+ * <Value>Cat</Value>
44+ * <Value>Mouse</Value>
45+ * </field>
46+ * ```
47+ *
1248 * @author Tomas Holy <[email protected] > 1349 * @author Michael Härtl <[email protected] > 1450 * @license http://www.opensource.org/licenses/MIT
@@ -20,20 +56,23 @@ class XfdfFile extends File
2056<?xml version="1.0" encoding="UTF-8"?>
2157<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
2258<fields>
59+
2360FDF ;
2461
2562 // XFDF file footer
2663 const XFDF_FOOTER = <<<FDF
2764</fields>
2865</xfdf>
66+
2967FDF ;
3068
3169 /**
3270 * Constructor
3371 *
72+ *
3473 * @param array $data the form data as name => value
3574 * @param string|null $suffix the optional suffix for the tmp file
36- * @param string|null $suffix the optional prefix for the tmp file. If null
75+ * @param string|null $prefix the optional prefix for the tmp file. If null
3776 * 'php_tmpfile_' is used.
3877 * @param string|null $directory directory where the file should be
3978 * created. Autodetected if not provided.
@@ -142,11 +181,18 @@ protected function writeFields($fp, $fields)
142181 foreach ($ fields as $ key => $ value ) {
143182 $ key = $ this ->xmlEncode ($ key );
144183 fwrite ($ fp , "<field name= \"$ key \"> \n" );
145- if (is_array ($ value )) {
146- $ this ->writeFields ($ fp , $ value );
184+ if (!is_array ($ value )) {
185+ $ value = array ($ value );
186+ }
187+ if (isset ($ value [0 ])) {
188+ // Numeric keys: single or multi-value field
189+ foreach ($ value as $ val ) {
190+ $ val = $ this ->xmlEncode ($ val );
191+ fwrite ($ fp , "<value> $ val</value> \n" );
192+ }
147193 } else {
148- $ value = $ this -> xmlEncode ( $ value );
149- fwrite ($ fp , " <value> $ value</value> \n" );
194+ // String keys: nested/hierarchical fields
195+ $ this -> writeFields ($ fp , $ value );
150196 }
151197 fwrite ($ fp , "</field> \n" );
152198 }
0 commit comments