Skip to content
Snippets Groups Projects
Commit 7b69405f authored by Liam Morland's avatar Liam Morland
Browse files

Issue #3323376: Convert xfdf.inc to a class

parent f85a9098
No related branches found
No related tags found
No related merge requests found
<?php
namespace Drupal\fillpdf\Component\Utility;
/**
* Provides functions for creating XFDF files.
*
* @package Drupal\fillpdf\Component\Utility
*/
class Xfdf {
/**
* Generates XFDF file object from values given in an associative array.
*
* @param array $info
* Key/value pairs of the field data.
* @param string|null $file
* The PDF file: URL or file path accepted. Use NULL to skip setting
* file-related properties.
* @param string $enc
* The character encoding. Must match server output: default_charset in
* php.ini.
*
* @return \DOMDocument
* A object representing the XFDF file contents.
*/
public static function createDomDocument(array $info, string $file = NULL, string $enc = 'UTF-8'): \DOMDocument {
$doc = new \DOMDocument('1.0', $enc);
$xfdf_ele = $doc->appendChild($doc->createElement('xfdf'));
$xfdf_ele->setAttribute('xmlns', 'http://ns.adobe.com/xfdf/');
$xfdf_ele->setAttribute('xml:space', 'preserve');
$fields_ele = $xfdf_ele->appendChild($doc->createElement('fields'));
foreach ($info as $name => $value) {
$field_ele = $fields_ele->appendChild($doc->createElement('field'));
$field_ele->setAttribute('name', $name);
$value_ele = $field_ele->appendChild($doc->createElement('value'));
$value_ele->appendChild($doc->createTextNode($value ?: ''));
}
$ids_ele = $xfdf_ele->appendChild($doc->createElement('ids'));
if ($file) {
$ids_ele->setAttribute('original', md5($file));
}
$ids_ele->setAttribute('modified', \Drupal::time()->getRequestTime());
if ($file) {
$f_ele = $xfdf_ele->appendChild($doc->createElement('f'));
$f_ele->setAttribute('href', $file);
}
return $doc;
}
/**
* Generates XFDF file contents from values given in an associative array.
*
* @param array $info
* Key/value pairs of the field data.
* @param string|null $file
* The PDF file: URL or file path accepted. Use NULL to skip setting
* file-related properties.
* @param string $enc
* The character encoding. Must match server output: default_charset in
* php.ini.
*
* @return string
* The contents of the XFDF file.
*/
public static function createString(array $info, string $file = NULL, string $enc = 'UTF-8'): string {
return static::createDomDocument($info, $file, $enc)->saveXML();
}
}
......@@ -8,6 +8,7 @@ use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\file\FileInterface;
use Drupal\fillpdf\Component\Utility\FillPdf;
use Drupal\fillpdf\Component\Utility\Xfdf;
use Drupal\fillpdf\FieldMapping\TextFieldMapping;
use Drupal\fillpdf\Plugin\PdfBackendBase;
use Drupal\fillpdf\ShellManager;
......@@ -182,9 +183,8 @@ class PdftkPdfBackend extends PdfBackendBase implements ContainerFactoryPluginIn
}
}
module_load_include('inc', 'fillpdf', 'xfdf');
$xfdf_name = $template_uri . '.xfdf';
$xfdf = create_xfdf(basename($xfdf_name), $fields);
$xfdf = Xfdf::createString($fields, basename($xfdf_name));
// Generate the file.
$xfdf_file = \Drupal::service('file.repository')->writeData($xfdf, $xfdf_name, FileSystemInterface::EXISTS_RENAME);
......
<?php
namespace Drupal\Tests\fillpdf\Kernel;
use Drupal\fillpdf\Component\Utility\Xfdf;
use Drupal\KernelTests\KernelTestBase;
/**
* Test the XfdfUnitTest class.
*
* @group fillpdf
* @covers \Drupal\fillpdf\Component\Utility\Xfdf
*/
class XfdfKernelTest extends KernelTestBase {
/**
* Test Xfdf.
*/
public function test() {
// Random input values.
$key = $this->randomMachineName();
$value = $this->randomString() . '"';
$fields = [
$key => $value,
];
$expected = '<?xml version="1.0" encoding="UTF-8"?>
<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve"><fields><field name="' . $key . '"><value>' . htmlspecialchars($value, ENT_NOQUOTES) . '</value></field></fields><ids modified="1234"/></xfdf>
';
$actual = Xfdf::createString($fields);
// Make the timestamp always be the same.
$actual = preg_replace('/ modified="\d+"/', ' modified="1234"', $actual);
$this->assertSame($expected, $actual);
}
}
<?php
/**
* @file
* Provides functions for creating XFDF files.
*/
/**
* Generates an XFDF file from values given in an associative array.
*
* @param string|null $file
* The PDF file: URL or file path accepted. Use NULL to skip setting
* file-related properties.
* @param array $info
* Key/value pairs of the field data.
* @param string $enc
* The character encoding. Must match server output: default_charset in
* php.ini.
*
* @return string
* The contents of the XFDF file.
*/
function create_xfdf($file, array $info, $enc = 'UTF-8') {
$doc = new DOMDocument('1.0', $enc);
$xfdf_ele = $doc->appendChild($doc->createElement('xfdf'));
$xfdf_ele->setAttribute('xmlns', 'http://ns.adobe.com/xfdf/');
$xfdf_ele->setAttribute('xml:space', 'preserve');
$fields_ele = $xfdf_ele->appendChild($doc->createElement('fields'));
foreach ($info as $name => $value) {
$field_ele = $fields_ele->appendChild($doc->createElement('field'));
$field_ele->setAttribute('name', $name);
$value_ele = $field_ele->appendChild($doc->createElement('value'));
$value_ele->appendChild($doc->createTextNode($value ?: ''));
}
$ids_ele = $xfdf_ele->appendChild($doc->createElement('ids'));
if ($file) {
$ids_ele->setAttribute('original', md5($file));
}
$ids_ele->setAttribute('modified', \Drupal::time()->getRequestTime());
if ($file) {
$f_ele = $xfdf_ele->appendChild($doc->createElement('f'));
$f_ele->setAttribute('href', $file);
}
return $doc->saveXML();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment