Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #148 Support multi value fields #248

Merged
merged 1 commit into from
Jul 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 51 additions & 5 deletions src/XfdfFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,42 @@
* This class represents a temporary XFDF file that can be used to fill a PDF
* form with valid unicode characters.
*
* Form data must be passed to the constructor as an array in this form:
*
* ```
* [
* // Field name => field value
* 'Firstname' => 'John',
*
* // Hierarchical/nested fields in dot notation
* 'Address.Street' => 'Some Street',
* 'Address.City' => 'Any City',
*
* // Multi value fields
* 'Pets' => ['Cat', 'Mouse'],
* ]
* ```
*
* This will result in the following XML structure (header/footer omitted):
*
* ```
* <field name="Firstname">
* <Value>John</Value>
* </field>
* <field name="Address">
* <field name="Street">
* <Value>Some Street</Value>
* </field>
* <field name="City">
* <Value>Any City</Value>
* </field>
* </field>
* <field name="Pets">
* <Value>Cat</Value>
* <Value>Mouse</Value>
* </field>
* ```
*
* @author Tomas Holy <[email protected]>
* @author Michael Härtl <[email protected]>
* @license http://www.opensource.org/licenses/MIT
Expand All @@ -20,20 +56,23 @@ class XfdfFile extends File
<?xml version="1.0" encoding="UTF-8"?>
<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
<fields>

FDF;

// XFDF file footer
const XFDF_FOOTER = <<<FDF
</fields>
</xfdf>

FDF;

/**
* Constructor
*
*
* @param array $data the form data as name => value
* @param string|null $suffix the optional suffix for the tmp file
* @param string|null $suffix the optional prefix for the tmp file. If null
* @param string|null $prefix the optional prefix for the tmp file. If null
* 'php_tmpfile_' is used.
* @param string|null $directory directory where the file should be
* created. Autodetected if not provided.
Expand Down Expand Up @@ -142,11 +181,18 @@ protected function writeFields($fp, $fields)
foreach ($fields as $key => $value) {
$key = $this->xmlEncode($key);
fwrite($fp, "<field name=\"$key\">\n");
if (is_array($value)) {
$this->writeFields($fp, $value);
if (!is_array($value)) {
$value = array($value);
}
if (isset($value[0])) {
// Numeric keys: single or multi-value field
foreach($value as $val) {
$val = $this->xmlEncode($val);
fwrite($fp, "<value>$val</value>\n");
}
} else {
$value = $this->xmlEncode($value);
fwrite($fp, "<value>$value</value>\n");
// String keys: nested/hierarchical fields
$this->writeFields($fp, $value);
}
fwrite($fp, "</field>\n");
}
Expand Down
11 changes: 7 additions & 4 deletions tests/XfdfFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ public function testXfdfFileCreation()
'checkbox 2' => 0,
'radio 1' => 2,
'address.street' => 'some street',
"umlauts-in-value" => "öäüÖÄÜ",
"umlauts-in-value" => 'öäüÖÄÜ',
'some.other.value' => 'val1',
'some.other.value2' => 'val2',
"öäüÖÄÜ" => "umlauts in key",
"special-in-value" => "ۧ&()",
"€ key" => "special in key",
'öäüÖÄÜ' => "umlauts in key",
'special-in-value' => 'ۧ&()',
'€ key' => 'special in key',
'multivalue1' => array('val1', 'val2'),
'nested.dummy' => 'valX',
'nested.multivalue2' => array('val3', 'val4'),
);

$oXfdfFile = new XfdfFile($data, null, null, __DIR__);
Expand Down
18 changes: 16 additions & 2 deletions tests/files/XfdfFileTest.xfdf
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
<fields><field name="name">
<fields>
<field name="name">
<value>Jürgen čárka čČćĆđĐ мирано</value>
</field>
<field name="email">
Expand Down Expand Up @@ -45,5 +46,18 @@
<field name="€ key">
<value>special in key</value>
</field>
<field name="multivalue1">
<value>val1</value>
<value>val2</value>
</field>
<field name="nested">
<field name="dummy">
<value>valX</value>
</field>
<field name="multivalue2">
<value>val3</value>
<value>val4</value>
</field>
</field>
</fields>
</xfdf>
</xfdf>