Skip to content

Commit 8a6fe16

Browse files
authored
Merge pull request #248 from mikehaertl/148-multi-value-fields
Issue #148 Support multi value fields
2 parents 01bb1d8 + 35b519f commit 8a6fe16

File tree

3 files changed

+74
-11
lines changed

3 files changed

+74
-11
lines changed

src/XfdfFile.php

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,42 @@
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+
2360
FDF;
2461

2562
// XFDF file footer
2663
const XFDF_FOOTER = <<<FDF
2764
</fields>
2865
</xfdf>
66+
2967
FDF;
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
}

tests/XfdfFileTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@ public function testXfdfFileCreation()
1616
'checkbox 2' => 0,
1717
'radio 1' => 2,
1818
'address.street' => 'some street',
19-
"umlauts-in-value" => "öäüÖÄÜ",
19+
"umlauts-in-value" => 'öäüÖÄÜ',
2020
'some.other.value' => 'val1',
2121
'some.other.value2' => 'val2',
22-
"öäüÖÄÜ" => "umlauts in key",
23-
"special-in-value" => "ۧ&()",
24-
"€ key" => "special in key",
22+
'öäüÖÄÜ' => "umlauts in key",
23+
'special-in-value' => 'ۧ&()',
24+
'€ key' => 'special in key',
25+
'multivalue1' => array('val1', 'val2'),
26+
'nested.dummy' => 'valX',
27+
'nested.multivalue2' => array('val3', 'val4'),
2528
);
2629

2730
$oXfdfFile = new XfdfFile($data, null, null, __DIR__);

tests/files/XfdfFileTest.xfdf

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
3-
<fields><field name="name">
3+
<fields>
4+
<field name="name">
45
<value>Jürgen čárka čČćĆđĐ мирано</value>
56
</field>
67
<field name="email">
@@ -45,5 +46,18 @@
4546
<field name="€ key">
4647
<value>special in key</value>
4748
</field>
49+
<field name="multivalue1">
50+
<value>val1</value>
51+
<value>val2</value>
52+
</field>
53+
<field name="nested">
54+
<field name="dummy">
55+
<value>valX</value>
56+
</field>
57+
<field name="multivalue2">
58+
<value>val3</value>
59+
<value>val4</value>
60+
</field>
61+
</field>
4862
</fields>
49-
</xfdf>
63+
</xfdf>

0 commit comments

Comments
 (0)