Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| XfdfReader | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
7 | |
100.00% |
1 / 1 |
| parse | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
7 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Phpdftk\Pdf\Core\Interactive\Form; |
| 6 | |
| 7 | /** |
| 8 | * XFDF (XML Forms Data Format) reader — ISO 32000-2 §12.7.8. |
| 9 | * |
| 10 | * Parses a standalone .xfdf XML file and extracts field name/value pairs. |
| 11 | */ |
| 12 | final class XfdfReader |
| 13 | { |
| 14 | /** |
| 15 | * Parse XFDF content and return a field name → value map. |
| 16 | * |
| 17 | * @return array<string, string> |
| 18 | */ |
| 19 | public static function parse(string $xfdfContent): array |
| 20 | { |
| 21 | $fields = []; |
| 22 | |
| 23 | $xml = @simplexml_load_string($xfdfContent); |
| 24 | if ($xml === false) { |
| 25 | return $fields; |
| 26 | } |
| 27 | |
| 28 | // Register the XFDF namespace |
| 29 | $xml->registerXPathNamespace('x', 'http://ns.adobe.com/xfdf/'); |
| 30 | |
| 31 | // Try with namespace |
| 32 | $fieldNodes = $xml->xpath('//x:field'); |
| 33 | if ($fieldNodes === false || $fieldNodes === []) { |
| 34 | // Try without namespace (some XFDF files omit it) |
| 35 | $fieldNodes = $xml->xpath('//field'); |
| 36 | } |
| 37 | |
| 38 | if ($fieldNodes !== false) { |
| 39 | foreach ($fieldNodes as $field) { |
| 40 | $name = (string) $field['name']; |
| 41 | $value = (string) ($field->value ?? ''); |
| 42 | if ($name !== '') { |
| 43 | $fields[$name] = $value; |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | return $fields; |
| 49 | } |
| 50 | } |