9
9
* This class represents a temporary XFDF file that can be used to fill a PDF
10
10
* form with valid unicode characters.
11
11
*
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
+ *
12
48
* @author Tomas Holy <[email protected] >
13
49
* @author Michael Härtl <[email protected] >
14
50
* @license http://www.opensource.org/licenses/MIT
@@ -20,20 +56,23 @@ class XfdfFile extends File
20
56
<?xml version="1.0" encoding="UTF-8"?>
21
57
<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
22
58
<fields>
59
+
23
60
FDF ;
24
61
25
62
// XFDF file footer
26
63
const XFDF_FOOTER = <<<FDF
27
64
</fields>
28
65
</xfdf>
66
+
29
67
FDF ;
30
68
31
69
/**
32
70
* Constructor
33
71
*
72
+ *
34
73
* @param array $data the form data as name => value
35
74
* @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
37
76
* 'php_tmpfile_' is used.
38
77
* @param string|null $directory directory where the file should be
39
78
* created. Autodetected if not provided.
@@ -142,11 +181,18 @@ protected function writeFields($fp, $fields)
142
181
foreach ($ fields as $ key => $ value ) {
143
182
$ key = $ this ->xmlEncode ($ key );
144
183
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
+ }
147
193
} else {
148
- $ value = $ this -> xmlEncode ( $ value );
149
- fwrite ($ fp , " <value> $ value</value> \n" );
194
+ // String keys: nested/hierarchical fields
195
+ $ this -> writeFields ($ fp , $ value );
150
196
}
151
197
fwrite ($ fp , "</field> \n" );
152
198
}
0 commit comments