Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| XfdfWriter | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 | |
100.00% |
1 / 1 |
| generate | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Phpdftk\Pdf\Core\Interactive\Form; |
| 6 | |
| 7 | /** |
| 8 | * XFDF (XML Forms Data Format) writer — ISO 32000-2 §12.7.8. |
| 9 | * |
| 10 | * Generates a standalone .xfdf XML file containing field name/value pairs. |
| 11 | */ |
| 12 | final class XfdfWriter |
| 13 | { |
| 14 | /** |
| 15 | * Generate XFDF content from a field name → value map. |
| 16 | * |
| 17 | * @param array<string, string> $fields Field name => value |
| 18 | * @param string|null $pdfPath Optional href attribute pointing to the PDF |
| 19 | */ |
| 20 | public static function generate(array $fields, ?string $pdfPath = null): string |
| 21 | { |
| 22 | $href = $pdfPath !== null ? ' href="' . htmlspecialchars($pdfPath, ENT_XML1) . '"' : ''; |
| 23 | |
| 24 | $xml = '<?xml version="1.0" encoding="UTF-8"?>' . "\n"; |
| 25 | $xml .= '<xfdf xmlns="http://ns.adobe.com/xfdf/"' . $href . ">\n"; |
| 26 | $xml .= " <fields>\n"; |
| 27 | |
| 28 | foreach ($fields as $name => $value) { |
| 29 | $xml .= ' <field name="' . htmlspecialchars($name, ENT_XML1) . "\">\n"; |
| 30 | $xml .= ' <value>' . htmlspecialchars($value, ENT_XML1) . "</value>\n"; |
| 31 | $xml .= " </field>\n"; |
| 32 | } |
| 33 | |
| 34 | $xml .= " </fields>\n"; |
| 35 | $xml .= "</xfdf>\n"; |
| 36 | |
| 37 | return $xml; |
| 38 | } |
| 39 | } |