Unit test for Reader and additional methods for DocumentProperties, adapted from PHPExcel
This commit is contained in:
parent
d2a231799d
commit
589e603277
@ -30,6 +30,13 @@
|
|||||||
*/
|
*/
|
||||||
class PHPWord_DocumentProperties
|
class PHPWord_DocumentProperties
|
||||||
{
|
{
|
||||||
|
/** Constants */
|
||||||
|
const PROPERTY_TYPE_BOOLEAN = 'b';
|
||||||
|
const PROPERTY_TYPE_INTEGER = 'i';
|
||||||
|
const PROPERTY_TYPE_FLOAT = 'f';
|
||||||
|
const PROPERTY_TYPE_DATE = 'd';
|
||||||
|
const PROPERTY_TYPE_STRING = 's';
|
||||||
|
const PROPERTY_TYPE_UNKNOWN = 'u';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creator
|
* Creator
|
||||||
@ -101,6 +108,20 @@ class PHPWord_DocumentProperties
|
|||||||
*/
|
*/
|
||||||
private $_company;
|
private $_company;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Manager
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $_manager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Custom Properties
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $_customProperties = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create new PHPWord_DocumentProperties
|
* Create new PHPWord_DocumentProperties
|
||||||
*/
|
*/
|
||||||
@ -116,6 +137,7 @@ class PHPWord_DocumentProperties
|
|||||||
$this->_keywords = '';
|
$this->_keywords = '';
|
||||||
$this->_category = '';
|
$this->_category = '';
|
||||||
$this->_company = '';
|
$this->_company = '';
|
||||||
|
$this->_manager = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -343,4 +365,243 @@ class PHPWord_DocumentProperties
|
|||||||
$this->_company = $pValue;
|
$this->_company = $pValue;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get Manager
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getManager()
|
||||||
|
{
|
||||||
|
return $this->_manager;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set Manager
|
||||||
|
*
|
||||||
|
* @param string $pValue
|
||||||
|
* @return PHPExcel_DocumentProperties
|
||||||
|
*/
|
||||||
|
public function setManager($pValue = '')
|
||||||
|
{
|
||||||
|
$this->_manager = $pValue;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a List of Custom Property Names
|
||||||
|
*
|
||||||
|
* @return array of string
|
||||||
|
*/
|
||||||
|
public function getCustomProperties()
|
||||||
|
{
|
||||||
|
return array_keys($this->_customProperties);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if a Custom Property is defined
|
||||||
|
*
|
||||||
|
* @param string $propertyName
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function isCustomPropertySet($propertyName)
|
||||||
|
{
|
||||||
|
return isset($this->_customProperties[$propertyName]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a Custom Property Value
|
||||||
|
*
|
||||||
|
* @param string $propertyName
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getCustomPropertyValue($propertyName)
|
||||||
|
{
|
||||||
|
if (isset($this->_customProperties[$propertyName])) {
|
||||||
|
return $this->_customProperties[$propertyName]['value'];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a Custom Property Type
|
||||||
|
*
|
||||||
|
* @param string $propertyName
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getCustomPropertyType($propertyName)
|
||||||
|
{
|
||||||
|
if (isset($this->_customProperties[$propertyName])) {
|
||||||
|
return $this->_customProperties[$propertyName]['type'];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set a Custom Property
|
||||||
|
*
|
||||||
|
* @param string $propertyName
|
||||||
|
* @param mixed $propertyValue
|
||||||
|
* @param string $propertyType
|
||||||
|
* 'i': Integer
|
||||||
|
* 'f': Floating Point
|
||||||
|
* 's': String
|
||||||
|
* 'd': Date/Time
|
||||||
|
* 'b': Boolean
|
||||||
|
* @return PHPExcel_DocumentProperties
|
||||||
|
*/
|
||||||
|
public function setCustomProperty($propertyName, $propertyValue = '', $propertyType = null)
|
||||||
|
{
|
||||||
|
if (($propertyType === null) || (!in_array($propertyType, array(
|
||||||
|
self::PROPERTY_TYPE_INTEGER,
|
||||||
|
self::PROPERTY_TYPE_FLOAT,
|
||||||
|
self::PROPERTY_TYPE_STRING,
|
||||||
|
self::PROPERTY_TYPE_DATE,
|
||||||
|
self::PROPERTY_TYPE_BOOLEAN
|
||||||
|
)))) {
|
||||||
|
if ($propertyValue === null) {
|
||||||
|
$propertyType = self::PROPERTY_TYPE_STRING;
|
||||||
|
} elseif (is_float($propertyValue)) {
|
||||||
|
$propertyType = self::PROPERTY_TYPE_FLOAT;
|
||||||
|
} elseif (is_int($propertyValue)) {
|
||||||
|
$propertyType = self::PROPERTY_TYPE_INTEGER;
|
||||||
|
} elseif (is_bool($propertyValue)) {
|
||||||
|
$propertyType = self::PROPERTY_TYPE_BOOLEAN;
|
||||||
|
} else {
|
||||||
|
$propertyType = self::PROPERTY_TYPE_STRING;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->_customProperties[$propertyName] = array(
|
||||||
|
'value' => $propertyValue,
|
||||||
|
'type' => $propertyType
|
||||||
|
);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert document propery based on type
|
||||||
|
*
|
||||||
|
* @param mixed $propertyValue
|
||||||
|
* @param string $propertyType
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public static function convertProperty($propertyValue, $propertyType)
|
||||||
|
{
|
||||||
|
switch ($propertyType) {
|
||||||
|
case 'empty': // Empty
|
||||||
|
return '';
|
||||||
|
break;
|
||||||
|
case 'null': // Null
|
||||||
|
return null;
|
||||||
|
break;
|
||||||
|
case 'i1': // 1-Byte Signed Integer
|
||||||
|
case 'i2': // 2-Byte Signed Integer
|
||||||
|
case 'i4': // 4-Byte Signed Integer
|
||||||
|
case 'i8': // 8-Byte Signed Integer
|
||||||
|
case 'int': // Integer
|
||||||
|
return (int) $propertyValue;
|
||||||
|
break;
|
||||||
|
case 'ui1': // 1-Byte Unsigned Integer
|
||||||
|
case 'ui2': // 2-Byte Unsigned Integer
|
||||||
|
case 'ui4': // 4-Byte Unsigned Integer
|
||||||
|
case 'ui8': // 8-Byte Unsigned Integer
|
||||||
|
case 'uint': // Unsigned Integer
|
||||||
|
return abs((int) $propertyValue);
|
||||||
|
break;
|
||||||
|
case 'r4': // 4-Byte Real Number
|
||||||
|
case 'r8': // 8-Byte Real Number
|
||||||
|
case 'decimal': // Decimal
|
||||||
|
return (float) $propertyValue;
|
||||||
|
break;
|
||||||
|
case 'lpstr': // LPSTR
|
||||||
|
case 'lpwstr': // LPWSTR
|
||||||
|
case 'bstr': // Basic String
|
||||||
|
return $propertyValue;
|
||||||
|
break;
|
||||||
|
case 'date': // Date and Time
|
||||||
|
case 'filetime': // File Time
|
||||||
|
return strtotime($propertyValue);
|
||||||
|
break;
|
||||||
|
case 'bool': // Boolean
|
||||||
|
return ($propertyValue == 'true') ? true : false;
|
||||||
|
break;
|
||||||
|
case 'cy': // Currency
|
||||||
|
case 'error': // Error Status Code
|
||||||
|
case 'vector': // Vector
|
||||||
|
case 'array': // Array
|
||||||
|
case 'blob': // Binary Blob
|
||||||
|
case 'oblob': // Binary Blob Object
|
||||||
|
case 'stream': // Binary Stream
|
||||||
|
case 'ostream': // Binary Stream Object
|
||||||
|
case 'storage': // Binary Storage
|
||||||
|
case 'ostorage': // Binary Storage Object
|
||||||
|
case 'vstream': // Binary Versioned Stream
|
||||||
|
case 'clsid': // Class ID
|
||||||
|
case 'cf': // Clipboard Data
|
||||||
|
return $propertyValue;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $propertyValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert document property type
|
||||||
|
*
|
||||||
|
* @param string $propertyType
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public static function convertPropertyType($propertyType)
|
||||||
|
{
|
||||||
|
switch ($propertyType) {
|
||||||
|
case 'i1': // 1-Byte Signed Integer
|
||||||
|
case 'i2': // 2-Byte Signed Integer
|
||||||
|
case 'i4': // 4-Byte Signed Integer
|
||||||
|
case 'i8': // 8-Byte Signed Integer
|
||||||
|
case 'int': // Integer
|
||||||
|
case 'ui1': // 1-Byte Unsigned Integer
|
||||||
|
case 'ui2': // 2-Byte Unsigned Integer
|
||||||
|
case 'ui4': // 4-Byte Unsigned Integer
|
||||||
|
case 'ui8': // 8-Byte Unsigned Integer
|
||||||
|
case 'uint': // Unsigned Integer
|
||||||
|
return self::PROPERTY_TYPE_INTEGER;
|
||||||
|
break;
|
||||||
|
case 'r4': // 4-Byte Real Number
|
||||||
|
case 'r8': // 8-Byte Real Number
|
||||||
|
case 'decimal': // Decimal
|
||||||
|
return self::PROPERTY_TYPE_FLOAT;
|
||||||
|
break;
|
||||||
|
case 'empty': // Empty
|
||||||
|
case 'null': // Null
|
||||||
|
case 'lpstr': // LPSTR
|
||||||
|
case 'lpwstr': // LPWSTR
|
||||||
|
case 'bstr': // Basic String
|
||||||
|
return self::PROPERTY_TYPE_STRING;
|
||||||
|
break;
|
||||||
|
case 'date': // Date and Time
|
||||||
|
case 'filetime': // File Time
|
||||||
|
return self::PROPERTY_TYPE_DATE;
|
||||||
|
break;
|
||||||
|
case 'bool': // Boolean
|
||||||
|
return self::PROPERTY_TYPE_BOOLEAN;
|
||||||
|
break;
|
||||||
|
case 'cy': // Currency
|
||||||
|
case 'error': // Error Status Code
|
||||||
|
case 'vector': // Vector
|
||||||
|
case 'array': // Array
|
||||||
|
case 'blob': // Binary Blob
|
||||||
|
case 'oblob': // Binary Blob Object
|
||||||
|
case 'stream': // Binary Stream
|
||||||
|
case 'ostream': // Binary Stream Object
|
||||||
|
case 'storage': // Binary Storage
|
||||||
|
case 'ostorage': // Binary Storage Object
|
||||||
|
case 'vstream': // Binary Versioned Stream
|
||||||
|
case 'clsid': // Class ID
|
||||||
|
case 'cf': // Clipboard Data
|
||||||
|
return self::PROPERTY_TYPE_UNKNOWN;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return self::PROPERTY_TYPE_UNKNOWN;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -38,7 +38,6 @@ if (!defined('PHPWORD_BASE_PATH')) {
|
|||||||
class PHPWord_Reader_Word2007 extends PHPWord_Reader_Abstract implements
|
class PHPWord_Reader_Word2007 extends PHPWord_Reader_Abstract implements
|
||||||
PHPWord_Reader_IReader
|
PHPWord_Reader_IReader
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new PHPWord_Reader_Word2007 instance
|
* Create a new PHPWord_Reader_Word2007 instance
|
||||||
*/
|
*/
|
||||||
@ -57,8 +56,7 @@ class PHPWord_Reader_Word2007 extends PHPWord_Reader_Abstract implements
|
|||||||
// Check if file exists
|
// Check if file exists
|
||||||
if (!file_exists($pFilename)) {
|
if (!file_exists($pFilename)) {
|
||||||
throw new PHPWord_Exception(
|
throw new PHPWord_Exception(
|
||||||
"Could not open " . $pFilename .
|
"Could not open {$pFilename} for reading! File does not exist."
|
||||||
" for reading! File does not exist."
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,16 +121,13 @@ class PHPWord_Reader_Word2007 extends PHPWord_Reader_Abstract implements
|
|||||||
* Loads PHPWord from file
|
* Loads PHPWord from file
|
||||||
*
|
*
|
||||||
* @param string $pFilename
|
* @param string $pFilename
|
||||||
* @return PHPWord
|
* @return PHPWord|null
|
||||||
*/
|
*/
|
||||||
public function load($pFilename)
|
public function load($pFilename)
|
||||||
{
|
{
|
||||||
// Check if file exists
|
// Check if file exists and can be read
|
||||||
if (!file_exists($pFilename)) {
|
if (!$this->canRead($pFilename)) {
|
||||||
throw new PHPWord_Exception(
|
return;
|
||||||
"Could not open " . $pFilename .
|
|
||||||
" for reading! File does not exist."
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialisations
|
// Initialisations
|
||||||
@ -211,7 +206,7 @@ class PHPWord_Reader_Word2007 extends PHPWord_Reader_Abstract implements
|
|||||||
foreach ($xmlDoc->body->children() as $elm) {
|
foreach ($xmlDoc->body->children() as $elm) {
|
||||||
$elmName = $elm->getName();
|
$elmName = $elm->getName();
|
||||||
if ($elmName == 'p') { // Paragraph/section
|
if ($elmName == 'p') { // Paragraph/section
|
||||||
// Create new section if section section found
|
// Create new section if section setting found
|
||||||
if ($elm->pPr->sectPr) {
|
if ($elm->pPr->sectPr) {
|
||||||
$section->setSettings($this->loadSectionSettings($elm->pPr));
|
$section->setSettings($this->loadSectionSettings($elm->pPr));
|
||||||
$section = $word->createSection();
|
$section = $word->createSection();
|
||||||
@ -262,8 +257,8 @@ class PHPWord_Reader_Word2007 extends PHPWord_Reader_Abstract implements
|
|||||||
}
|
}
|
||||||
unset($pStyle);
|
unset($pStyle);
|
||||||
unset($fStyle);
|
unset($fStyle);
|
||||||
$hasParagraphStyle = $elm->pPr && ($elm->pPr != '');
|
$hasParagraphStyle = isset($elm->pPr);
|
||||||
$hasFontStyle = $elm->rPr && ($elm->rPr != '');
|
$hasFontStyle = isset($elm->rPr);
|
||||||
$styleName = (string)$elm->name['val'];
|
$styleName = (string)$elm->name['val'];
|
||||||
if ($hasParagraphStyle) {
|
if ($hasParagraphStyle) {
|
||||||
$pStyle = $this->loadParagraphStyle($elm);
|
$pStyle = $this->loadParagraphStyle($elm);
|
||||||
|
|||||||
@ -411,7 +411,7 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter
|
|||||||
if ($this->_lastParagraphStyle != '' || $styleFont) {
|
if ($this->_lastParagraphStyle != '' || $styleFont) {
|
||||||
$sRTFText .= ' ';
|
$sRTFText .= ' ';
|
||||||
}
|
}
|
||||||
$sRTFText .= $text->getDataContentText();
|
$sRTFText .= $text->getText();
|
||||||
|
|
||||||
if ($styleFont) {
|
if ($styleFont) {
|
||||||
$sRTFText .= '\cf0';
|
$sRTFText .= '\cf0';
|
||||||
|
|||||||
69
Tests/PHPWord/Reader/Word2007.php
Normal file
69
Tests/PHPWord/Reader/Word2007.php
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
<?php
|
||||||
|
namespace PHPWord\Tests\Reader;
|
||||||
|
|
||||||
|
use PHPUnit_Framework_TestCase;
|
||||||
|
use PHPWord_Reader_Word2007;
|
||||||
|
use PHPWord_IOFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class Word2007Test
|
||||||
|
*
|
||||||
|
* @package PHPWord\Tests
|
||||||
|
*/
|
||||||
|
class Word2007Test extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
/** @var Test file directory */
|
||||||
|
private $dir;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Init
|
||||||
|
*/
|
||||||
|
public function tearDown()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test canRead() method
|
||||||
|
*/
|
||||||
|
public function testCanRead()
|
||||||
|
{
|
||||||
|
$dir = join(
|
||||||
|
DIRECTORY_SEPARATOR,
|
||||||
|
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'documents')
|
||||||
|
);
|
||||||
|
$object = new PHPWord_Reader_Word2007;
|
||||||
|
$file = $dir . DIRECTORY_SEPARATOR . 'reader.docx';
|
||||||
|
$this->assertTrue($object->canRead($file));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test canRead() failure
|
||||||
|
*
|
||||||
|
* @expectedException Exception
|
||||||
|
*/
|
||||||
|
public function testCanReadFailed()
|
||||||
|
{
|
||||||
|
$dir = join(
|
||||||
|
DIRECTORY_SEPARATOR,
|
||||||
|
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'documents')
|
||||||
|
);
|
||||||
|
$object = new PHPWord_Reader_Word2007;
|
||||||
|
$file = $dir . DIRECTORY_SEPARATOR . 'foo.docx';
|
||||||
|
$this->assertFalse($object->canRead($file));
|
||||||
|
$object = PHPWord_IOFactory::load($file);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test load document
|
||||||
|
*/
|
||||||
|
public function testLoad()
|
||||||
|
{
|
||||||
|
$dir = join(
|
||||||
|
DIRECTORY_SEPARATOR,
|
||||||
|
array(PHPWORD_TESTS_DIR_ROOT, '_files', 'documents')
|
||||||
|
);
|
||||||
|
$file = $dir . DIRECTORY_SEPARATOR . 'reader.docx';
|
||||||
|
$object = PHPWord_IOFactory::load($file);
|
||||||
|
$this->assertInstanceOf('PHPWord', $object);
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
Tests/_files/documents/reader.docx
Normal file
BIN
Tests/_files/documents/reader.docx
Normal file
Binary file not shown.
@ -2,7 +2,6 @@
|
|||||||
namespace PHPWord\Tests;
|
namespace PHPWord\Tests;
|
||||||
|
|
||||||
use PHPWord;
|
use PHPWord;
|
||||||
use DOMDocument;
|
|
||||||
|
|
||||||
class TestHelperDOCX
|
class TestHelperDOCX
|
||||||
{
|
{
|
||||||
@ -10,7 +9,7 @@ class TestHelperDOCX
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \PHPWord $PHPWord
|
* @param \PHPWord $PHPWord
|
||||||
* @return \PHPWord\Tests\Xml_Document
|
* @return \PHPWord\Tests\XmlDocument
|
||||||
*/
|
*/
|
||||||
public static function getDocument(PHPWord $PHPWord)
|
public static function getDocument(PHPWord $PHPWord)
|
||||||
{
|
{
|
||||||
@ -29,7 +28,7 @@ class TestHelperDOCX
|
|||||||
$zip->close();
|
$zip->close();
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Xml_Document(sys_get_temp_dir() . '/PHPWord_Unit_Test/');
|
return new XmlDocument(sys_get_temp_dir() . '/PHPWord_Unit_Test/');
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function clear()
|
public static function clear()
|
||||||
@ -60,65 +59,3 @@ class TestHelperDOCX
|
|||||||
rmdir($dir);
|
rmdir($dir);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Xml_Document
|
|
||||||
{
|
|
||||||
/** @var string $path */
|
|
||||||
private $path;
|
|
||||||
|
|
||||||
/** @var \DOMDocument $dom */
|
|
||||||
private $dom;
|
|
||||||
|
|
||||||
/** @var \DOMXpath $xpath */
|
|
||||||
private $xpath;
|
|
||||||
|
|
||||||
/** @var string $file */
|
|
||||||
private $file;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $path
|
|
||||||
*/
|
|
||||||
public function __construct($path)
|
|
||||||
{
|
|
||||||
$this->path = realpath($path);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $file
|
|
||||||
* @return \DOMDocument
|
|
||||||
*/
|
|
||||||
public function getFileDom($file = 'word/document.xml')
|
|
||||||
{
|
|
||||||
if (null !== $this->dom && $file === $this->file) {
|
|
||||||
return $this->dom;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->xpath = null;
|
|
||||||
$this->file = $file;
|
|
||||||
|
|
||||||
$file = $this->path . '/' . $file;
|
|
||||||
$this->dom = new DOMDocument();
|
|
||||||
$this->dom->load($file);
|
|
||||||
return $this->dom;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $path
|
|
||||||
* @param string $file
|
|
||||||
* @return \DOMElement
|
|
||||||
*/
|
|
||||||
public function getElement($path, $file = 'word/document.xml')
|
|
||||||
{
|
|
||||||
if ($this->dom === null || $file !== $this->file) {
|
|
||||||
$this->getFileDom($file);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (null === $this->xpath) {
|
|
||||||
$this->xpath = new \DOMXpath($this->dom);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
$elements = $this->xpath->query($path);
|
|
||||||
return $elements->item(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
66
Tests/_inc/XmlDocument.php
Normal file
66
Tests/_inc/XmlDocument.php
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
<?php
|
||||||
|
namespace PHPWord\Tests;
|
||||||
|
|
||||||
|
use DOMDocument;
|
||||||
|
|
||||||
|
class XmlDocument
|
||||||
|
{
|
||||||
|
/** @var string $path */
|
||||||
|
private $path;
|
||||||
|
|
||||||
|
/** @var \DOMDocument $dom */
|
||||||
|
private $dom;
|
||||||
|
|
||||||
|
/** @var \DOMXpath $xpath */
|
||||||
|
private $xpath;
|
||||||
|
|
||||||
|
/** @var string $file */
|
||||||
|
private $file;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $path
|
||||||
|
*/
|
||||||
|
public function __construct($path)
|
||||||
|
{
|
||||||
|
$this->path = realpath($path);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $file
|
||||||
|
* @return \DOMDocument
|
||||||
|
*/
|
||||||
|
public function getFileDom($file = 'word/document.xml')
|
||||||
|
{
|
||||||
|
if (null !== $this->dom && $file === $this->file) {
|
||||||
|
return $this->dom;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->xpath = null;
|
||||||
|
$this->file = $file;
|
||||||
|
|
||||||
|
$file = $this->path . '/' . $file;
|
||||||
|
$this->dom = new DOMDocument();
|
||||||
|
$this->dom->load($file);
|
||||||
|
return $this->dom;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $path
|
||||||
|
* @param string $file
|
||||||
|
* @return \DOMElement
|
||||||
|
*/
|
||||||
|
public function getElement($path, $file = 'word/document.xml')
|
||||||
|
{
|
||||||
|
if ($this->dom === null || $file !== $this->file) {
|
||||||
|
$this->getFileDom($file);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (null === $this->xpath) {
|
||||||
|
$this->xpath = new \DOMXpath($this->dom);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
$elements = $this->xpath->query($path);
|
||||||
|
return $elements->item(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -12,3 +12,4 @@ require_once __DIR__ . '/../Classes/PHPWord/Autoloader.php';
|
|||||||
PHPWord_Autoloader::Register();
|
PHPWord_Autoloader::Register();
|
||||||
|
|
||||||
require_once __DIR__ . '/_inc/TestHelperDOCX.php';
|
require_once __DIR__ . '/_inc/TestHelperDOCX.php';
|
||||||
|
require_once __DIR__ . '/_inc/XmlDocument.php';
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user