\PhpOffice\Common\XMLReader -> \PhpOffice\PhpWord\Shared\XMLReader
This commit is contained in:
parent
9a26ad9189
commit
0cca050bcd
|
|
@ -17,8 +17,8 @@
|
||||||
|
|
||||||
namespace PhpOffice\PhpWord\Reader;
|
namespace PhpOffice\PhpWord\Reader;
|
||||||
|
|
||||||
use PhpOffice\Common\XMLReader;
|
|
||||||
use PhpOffice\PhpWord\PhpWord;
|
use PhpOffice\PhpWord\PhpWord;
|
||||||
|
use PhpOffice\PhpWord\Shared\XMLReader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reader for ODText
|
* Reader for ODText
|
||||||
|
|
|
||||||
|
|
@ -17,9 +17,9 @@
|
||||||
|
|
||||||
namespace PhpOffice\PhpWord\Reader\ODText;
|
namespace PhpOffice\PhpWord\Reader\ODText;
|
||||||
|
|
||||||
use PhpOffice\Common\XMLReader;
|
|
||||||
use PhpOffice\PhpWord\Element\TrackChange;
|
use PhpOffice\PhpWord\Element\TrackChange;
|
||||||
use PhpOffice\PhpWord\PhpWord;
|
use PhpOffice\PhpWord\PhpWord;
|
||||||
|
use PhpOffice\PhpWord\Shared\XMLReader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Content reader
|
* Content reader
|
||||||
|
|
|
||||||
|
|
@ -17,8 +17,8 @@
|
||||||
|
|
||||||
namespace PhpOffice\PhpWord\Reader\ODText;
|
namespace PhpOffice\PhpWord\Reader\ODText;
|
||||||
|
|
||||||
use PhpOffice\Common\XMLReader;
|
|
||||||
use PhpOffice\PhpWord\PhpWord;
|
use PhpOffice\PhpWord\PhpWord;
|
||||||
|
use PhpOffice\PhpWord\Shared\XMLReader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Meta reader
|
* Meta reader
|
||||||
|
|
|
||||||
|
|
@ -17,8 +17,8 @@
|
||||||
|
|
||||||
namespace PhpOffice\PhpWord\Reader;
|
namespace PhpOffice\PhpWord\Reader;
|
||||||
|
|
||||||
use PhpOffice\Common\XMLReader;
|
|
||||||
use PhpOffice\PhpWord\PhpWord;
|
use PhpOffice\PhpWord\PhpWord;
|
||||||
|
use PhpOffice\PhpWord\Shared\XMLReader;
|
||||||
use PhpOffice\PhpWord\Shared\ZipArchive;
|
use PhpOffice\PhpWord\Shared\ZipArchive;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -17,12 +17,12 @@
|
||||||
|
|
||||||
namespace PhpOffice\PhpWord\Reader\Word2007;
|
namespace PhpOffice\PhpWord\Reader\Word2007;
|
||||||
|
|
||||||
use PhpOffice\Common\XMLReader;
|
|
||||||
use PhpOffice\PhpWord\ComplexType\TblWidth as TblWidthComplexType;
|
use PhpOffice\PhpWord\ComplexType\TblWidth as TblWidthComplexType;
|
||||||
use PhpOffice\PhpWord\Element\AbstractContainer;
|
use PhpOffice\PhpWord\Element\AbstractContainer;
|
||||||
use PhpOffice\PhpWord\Element\TextRun;
|
use PhpOffice\PhpWord\Element\TextRun;
|
||||||
use PhpOffice\PhpWord\Element\TrackChange;
|
use PhpOffice\PhpWord\Element\TrackChange;
|
||||||
use PhpOffice\PhpWord\PhpWord;
|
use PhpOffice\PhpWord\PhpWord;
|
||||||
|
use PhpOffice\PhpWord\Shared\XMLReader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Abstract part reader
|
* Abstract part reader
|
||||||
|
|
@ -95,7 +95,7 @@ abstract class AbstractPart
|
||||||
/**
|
/**
|
||||||
* Read w:p.
|
* Read w:p.
|
||||||
*
|
*
|
||||||
* @param \PhpOffice\Common\XMLReader $xmlReader
|
* @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader
|
||||||
* @param \DOMElement $domNode
|
* @param \DOMElement $domNode
|
||||||
* @param \PhpOffice\PhpWord\Element\AbstractContainer $parent
|
* @param \PhpOffice\PhpWord\Element\AbstractContainer $parent
|
||||||
* @param string $docPart
|
* @param string $docPart
|
||||||
|
|
@ -202,7 +202,7 @@ abstract class AbstractPart
|
||||||
/**
|
/**
|
||||||
* Read w:r.
|
* Read w:r.
|
||||||
*
|
*
|
||||||
* @param \PhpOffice\Common\XMLReader $xmlReader
|
* @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader
|
||||||
* @param \DOMElement $domNode
|
* @param \DOMElement $domNode
|
||||||
* @param \PhpOffice\PhpWord\Element\AbstractContainer $parent
|
* @param \PhpOffice\PhpWord\Element\AbstractContainer $parent
|
||||||
* @param string $docPart
|
* @param string $docPart
|
||||||
|
|
@ -320,7 +320,7 @@ abstract class AbstractPart
|
||||||
/**
|
/**
|
||||||
* Read w:tbl.
|
* Read w:tbl.
|
||||||
*
|
*
|
||||||
* @param \PhpOffice\Common\XMLReader $xmlReader
|
* @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader
|
||||||
* @param \DOMElement $domNode
|
* @param \DOMElement $domNode
|
||||||
* @param mixed $parent
|
* @param mixed $parent
|
||||||
* @param string $docPart
|
* @param string $docPart
|
||||||
|
|
@ -378,7 +378,7 @@ abstract class AbstractPart
|
||||||
/**
|
/**
|
||||||
* Read w:pPr.
|
* Read w:pPr.
|
||||||
*
|
*
|
||||||
* @param \PhpOffice\Common\XMLReader $xmlReader
|
* @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader
|
||||||
* @param \DOMElement $domNode
|
* @param \DOMElement $domNode
|
||||||
* @return array|null
|
* @return array|null
|
||||||
*/
|
*/
|
||||||
|
|
@ -413,7 +413,7 @@ abstract class AbstractPart
|
||||||
/**
|
/**
|
||||||
* Read w:rPr
|
* Read w:rPr
|
||||||
*
|
*
|
||||||
* @param \PhpOffice\Common\XMLReader $xmlReader
|
* @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader
|
||||||
* @param \DOMElement $domNode
|
* @param \DOMElement $domNode
|
||||||
* @return array|null
|
* @return array|null
|
||||||
*/
|
*/
|
||||||
|
|
@ -459,7 +459,7 @@ abstract class AbstractPart
|
||||||
/**
|
/**
|
||||||
* Read w:tblPr
|
* Read w:tblPr
|
||||||
*
|
*
|
||||||
* @param \PhpOffice\Common\XMLReader $xmlReader
|
* @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader
|
||||||
* @param \DOMElement $domNode
|
* @param \DOMElement $domNode
|
||||||
* @return string|array|null
|
* @return string|array|null
|
||||||
* @todo Capture w:tblStylePr w:type="firstRow"
|
* @todo Capture w:tblStylePr w:type="firstRow"
|
||||||
|
|
@ -509,7 +509,7 @@ abstract class AbstractPart
|
||||||
/**
|
/**
|
||||||
* Read w:tblpPr
|
* Read w:tblpPr
|
||||||
*
|
*
|
||||||
* @param \PhpOffice\Common\XMLReader $xmlReader
|
* @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader
|
||||||
* @param \DOMElement $domNode
|
* @param \DOMElement $domNode
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
|
|
@ -534,7 +534,7 @@ abstract class AbstractPart
|
||||||
/**
|
/**
|
||||||
* Read w:tblInd
|
* Read w:tblInd
|
||||||
*
|
*
|
||||||
* @param \PhpOffice\Common\XMLReader $xmlReader
|
* @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader
|
||||||
* @param \DOMElement $domNode
|
* @param \DOMElement $domNode
|
||||||
* @return TblWidthComplexType
|
* @return TblWidthComplexType
|
||||||
*/
|
*/
|
||||||
|
|
@ -552,7 +552,7 @@ abstract class AbstractPart
|
||||||
/**
|
/**
|
||||||
* Read w:tcPr
|
* Read w:tcPr
|
||||||
*
|
*
|
||||||
* @param \PhpOffice\Common\XMLReader $xmlReader
|
* @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader
|
||||||
* @param \DOMElement $domNode
|
* @param \DOMElement $domNode
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
|
|
@ -620,7 +620,7 @@ abstract class AbstractPart
|
||||||
/**
|
/**
|
||||||
* Read style definition
|
* Read style definition
|
||||||
*
|
*
|
||||||
* @param \PhpOffice\Common\XMLReader $xmlReader
|
* @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader
|
||||||
* @param \DOMElement $parentNode
|
* @param \DOMElement $parentNode
|
||||||
* @param array $styleDefs
|
* @param array $styleDefs
|
||||||
* @ignoreScrutinizerPatch
|
* @ignoreScrutinizerPatch
|
||||||
|
|
|
||||||
|
|
@ -17,8 +17,8 @@
|
||||||
|
|
||||||
namespace PhpOffice\PhpWord\Reader\Word2007;
|
namespace PhpOffice\PhpWord\Reader\Word2007;
|
||||||
|
|
||||||
use PhpOffice\Common\XMLReader;
|
|
||||||
use PhpOffice\PhpWord\PhpWord;
|
use PhpOffice\PhpWord\PhpWord;
|
||||||
|
use PhpOffice\PhpWord\Shared\XMLReader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Core properties reader
|
* Core properties reader
|
||||||
|
|
|
||||||
|
|
@ -17,9 +17,9 @@
|
||||||
|
|
||||||
namespace PhpOffice\PhpWord\Reader\Word2007;
|
namespace PhpOffice\PhpWord\Reader\Word2007;
|
||||||
|
|
||||||
use PhpOffice\Common\XMLReader;
|
|
||||||
use PhpOffice\PhpWord\Metadata\DocInfo;
|
use PhpOffice\PhpWord\Metadata\DocInfo;
|
||||||
use PhpOffice\PhpWord\PhpWord;
|
use PhpOffice\PhpWord\PhpWord;
|
||||||
|
use PhpOffice\PhpWord\Shared\XMLReader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Custom properties reader
|
* Custom properties reader
|
||||||
|
|
|
||||||
|
|
@ -17,9 +17,9 @@
|
||||||
|
|
||||||
namespace PhpOffice\PhpWord\Reader\Word2007;
|
namespace PhpOffice\PhpWord\Reader\Word2007;
|
||||||
|
|
||||||
use PhpOffice\Common\XMLReader;
|
|
||||||
use PhpOffice\PhpWord\Element\Section;
|
use PhpOffice\PhpWord\Element\Section;
|
||||||
use PhpOffice\PhpWord\PhpWord;
|
use PhpOffice\PhpWord\PhpWord;
|
||||||
|
use PhpOffice\PhpWord\Shared\XMLReader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Document reader
|
* Document reader
|
||||||
|
|
@ -97,7 +97,7 @@ class Document extends AbstractPart
|
||||||
/**
|
/**
|
||||||
* Read w:sectPr
|
* Read w:sectPr
|
||||||
*
|
*
|
||||||
* @param \PhpOffice\Common\XMLReader $xmlReader
|
* @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader
|
||||||
* @param \DOMElement $domNode
|
* @param \DOMElement $domNode
|
||||||
* @ignoreScrutinizerPatch
|
* @ignoreScrutinizerPatch
|
||||||
* @return array
|
* @return array
|
||||||
|
|
@ -141,7 +141,7 @@ class Document extends AbstractPart
|
||||||
/**
|
/**
|
||||||
* Read w:p node.
|
* Read w:p node.
|
||||||
*
|
*
|
||||||
* @param \PhpOffice\Common\XMLReader $xmlReader
|
* @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader
|
||||||
* @param \DOMElement $node
|
* @param \DOMElement $node
|
||||||
* @param \PhpOffice\PhpWord\Element\Section &$section
|
* @param \PhpOffice\PhpWord\Element\Section &$section
|
||||||
*
|
*
|
||||||
|
|
@ -170,7 +170,7 @@ class Document extends AbstractPart
|
||||||
/**
|
/**
|
||||||
* Read w:sectPr node.
|
* Read w:sectPr node.
|
||||||
*
|
*
|
||||||
* @param \PhpOffice\Common\XMLReader $xmlReader
|
* @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader
|
||||||
* @param \DOMElement $node
|
* @param \DOMElement $node
|
||||||
* @param \PhpOffice\PhpWord\Element\Section &$section
|
* @param \PhpOffice\PhpWord\Element\Section &$section
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -17,8 +17,8 @@
|
||||||
|
|
||||||
namespace PhpOffice\PhpWord\Reader\Word2007;
|
namespace PhpOffice\PhpWord\Reader\Word2007;
|
||||||
|
|
||||||
use PhpOffice\Common\XMLReader;
|
|
||||||
use PhpOffice\PhpWord\PhpWord;
|
use PhpOffice\PhpWord\PhpWord;
|
||||||
|
use PhpOffice\PhpWord\Shared\XMLReader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Footnotes reader
|
* Footnotes reader
|
||||||
|
|
|
||||||
|
|
@ -17,8 +17,8 @@
|
||||||
|
|
||||||
namespace PhpOffice\PhpWord\Reader\Word2007;
|
namespace PhpOffice\PhpWord\Reader\Word2007;
|
||||||
|
|
||||||
use PhpOffice\Common\XMLReader;
|
|
||||||
use PhpOffice\PhpWord\PhpWord;
|
use PhpOffice\PhpWord\PhpWord;
|
||||||
|
use PhpOffice\PhpWord\Shared\XMLReader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Numbering reader
|
* Numbering reader
|
||||||
|
|
@ -89,7 +89,7 @@ class Numbering extends AbstractPart
|
||||||
/**
|
/**
|
||||||
* Read numbering level definition from w:abstractNum and w:num
|
* Read numbering level definition from w:abstractNum and w:num
|
||||||
*
|
*
|
||||||
* @param \PhpOffice\Common\XMLReader $xmlReader
|
* @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader
|
||||||
* @param \DOMElement $subnode
|
* @param \DOMElement $subnode
|
||||||
* @param int $levelId
|
* @param int $levelId
|
||||||
* @return array
|
* @return array
|
||||||
|
|
|
||||||
|
|
@ -17,9 +17,9 @@
|
||||||
|
|
||||||
namespace PhpOffice\PhpWord\Reader\Word2007;
|
namespace PhpOffice\PhpWord\Reader\Word2007;
|
||||||
|
|
||||||
use PhpOffice\Common\XMLReader;
|
|
||||||
use PhpOffice\PhpWord\ComplexType\TrackChangesView;
|
use PhpOffice\PhpWord\ComplexType\TrackChangesView;
|
||||||
use PhpOffice\PhpWord\PhpWord;
|
use PhpOffice\PhpWord\PhpWord;
|
||||||
|
use PhpOffice\PhpWord\Shared\XMLReader;
|
||||||
use PhpOffice\PhpWord\Style\Language;
|
use PhpOffice\PhpWord\Style\Language;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -17,8 +17,8 @@
|
||||||
|
|
||||||
namespace PhpOffice\PhpWord\Reader\Word2007;
|
namespace PhpOffice\PhpWord\Reader\Word2007;
|
||||||
|
|
||||||
use PhpOffice\Common\XMLReader;
|
|
||||||
use PhpOffice\PhpWord\PhpWord;
|
use PhpOffice\PhpWord\PhpWord;
|
||||||
|
use PhpOffice\PhpWord\Shared\XMLReader;
|
||||||
use PhpOffice\PhpWord\Style\Language;
|
use PhpOffice\PhpWord\Style\Language;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,211 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* This file is part of PHPWord - A pure PHP library for reading and writing
|
||||||
|
* word processing documents.
|
||||||
|
*
|
||||||
|
* PHPWord is free software distributed under the terms of the GNU Lesser
|
||||||
|
* General Public License version 3 as published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please read the LICENSE
|
||||||
|
* file that was distributed with this source code. For the full list of
|
||||||
|
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
|
||||||
|
*
|
||||||
|
* @see https://github.com/PHPOffice/PHPWord
|
||||||
|
* @copyright 2010-2018 PHPWord contributors
|
||||||
|
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace PhpOffice\PhpWord\Shared;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* XML Reader wrapper
|
||||||
|
*
|
||||||
|
* @since 0.2.1
|
||||||
|
*/
|
||||||
|
class XMLReader
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* DOMDocument object
|
||||||
|
*
|
||||||
|
* @var \DOMDocument
|
||||||
|
*/
|
||||||
|
private $dom = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DOMXpath object
|
||||||
|
*
|
||||||
|
* @var \DOMXpath
|
||||||
|
*/
|
||||||
|
private $xpath = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get DOMDocument from ZipArchive
|
||||||
|
*
|
||||||
|
* @param string $zipFile
|
||||||
|
* @param string $xmlFile
|
||||||
|
* @return \DOMDocument|false
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function getDomFromZip($zipFile, $xmlFile)
|
||||||
|
{
|
||||||
|
if (file_exists($zipFile) === false) {
|
||||||
|
throw new \Exception('Cannot find archive file.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$zip = new \ZipArchive();
|
||||||
|
$zip->open($zipFile);
|
||||||
|
$content = $zip->getFromName($xmlFile);
|
||||||
|
$zip->close();
|
||||||
|
|
||||||
|
if ($content === false) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->getDomFromString($content);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get DOMDocument from content string
|
||||||
|
*
|
||||||
|
* @param string $content
|
||||||
|
* @return \DOMDocument
|
||||||
|
*/
|
||||||
|
public function getDomFromString($content)
|
||||||
|
{
|
||||||
|
$originalLibXMLEntityValue = libxml_disable_entity_loader(true);
|
||||||
|
$this->dom = new \DOMDocument();
|
||||||
|
$this->dom->loadXML($content);
|
||||||
|
libxml_disable_entity_loader($originalLibXMLEntityValue);
|
||||||
|
|
||||||
|
return $this->dom;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get elements
|
||||||
|
*
|
||||||
|
* @param string $path
|
||||||
|
* @param \DOMElement $contextNode
|
||||||
|
* @return \DOMNodeList
|
||||||
|
*/
|
||||||
|
public function getElements($path, \DOMElement $contextNode = null)
|
||||||
|
{
|
||||||
|
if ($this->dom === null) {
|
||||||
|
return array();
|
||||||
|
}
|
||||||
|
if ($this->xpath === null) {
|
||||||
|
$this->xpath = new \DOMXpath($this->dom);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_null($contextNode)) {
|
||||||
|
return $this->xpath->query($path);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->xpath->query($path, $contextNode);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers the namespace with the DOMXPath object
|
||||||
|
*
|
||||||
|
* @param string $prefix The prefix
|
||||||
|
* @param string $namespaceURI The URI of the namespace
|
||||||
|
* @return bool true on success or false on failure
|
||||||
|
* @throws \InvalidArgumentException If called before having loaded the DOM document
|
||||||
|
*/
|
||||||
|
public function registerNamespace($prefix, $namespaceURI)
|
||||||
|
{
|
||||||
|
if ($this->dom === null) {
|
||||||
|
throw new \InvalidArgumentException('Dom needs to be loaded before registering a namespace');
|
||||||
|
}
|
||||||
|
if ($this->xpath === null) {
|
||||||
|
$this->xpath = new \DOMXpath($this->dom);
|
||||||
|
}
|
||||||
|
return $this->xpath->registerNamespace($prefix, $namespaceURI);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get element
|
||||||
|
*
|
||||||
|
* @param string $path
|
||||||
|
* @param \DOMElement $contextNode
|
||||||
|
* @return \DOMElement|null
|
||||||
|
*/
|
||||||
|
public function getElement($path, \DOMElement $contextNode = null)
|
||||||
|
{
|
||||||
|
$elements = $this->getElements($path, $contextNode);
|
||||||
|
if ($elements->length > 0) {
|
||||||
|
return $elements->item(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get element attribute
|
||||||
|
*
|
||||||
|
* @param string $attribute
|
||||||
|
* @param \DOMElement $contextNode
|
||||||
|
* @param string $path
|
||||||
|
* @return string|null
|
||||||
|
*/
|
||||||
|
public function getAttribute($attribute, \DOMElement $contextNode = null, $path = null)
|
||||||
|
{
|
||||||
|
$return = null;
|
||||||
|
if ($path !== null) {
|
||||||
|
$elements = $this->getElements($path, $contextNode);
|
||||||
|
if ($elements->length > 0) {
|
||||||
|
/** @var \DOMElement $node Type hint */
|
||||||
|
$node = $elements->item(0);
|
||||||
|
$return = $node->getAttribute($attribute);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if ($contextNode !== null) {
|
||||||
|
$return = $contextNode->getAttribute($attribute);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ($return == '') ? null : $return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get element value
|
||||||
|
*
|
||||||
|
* @param string $path
|
||||||
|
* @param \DOMElement $contextNode
|
||||||
|
* @return string|null
|
||||||
|
*/
|
||||||
|
public function getValue($path, \DOMElement $contextNode = null)
|
||||||
|
{
|
||||||
|
$elements = $this->getElements($path, $contextNode);
|
||||||
|
if ($elements->length > 0) {
|
||||||
|
return $elements->item(0)->nodeValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Count elements
|
||||||
|
*
|
||||||
|
* @param string $path
|
||||||
|
* @param \DOMElement $contextNode
|
||||||
|
* @return integer
|
||||||
|
*/
|
||||||
|
public function countElements($path, \DOMElement $contextNode = null)
|
||||||
|
{
|
||||||
|
$elements = $this->getElements($path, $contextNode);
|
||||||
|
|
||||||
|
return $elements->length;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Element exists
|
||||||
|
*
|
||||||
|
* @param string $path
|
||||||
|
* @param \DOMElement $contextNode
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function elementExists($path, \DOMElement $contextNode = null)
|
||||||
|
{
|
||||||
|
return $this->getElements($path, $contextNode)->length > 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,134 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* This file is part of PHPWord - A pure PHP library for reading and writing
|
||||||
|
* word processing documents.
|
||||||
|
*
|
||||||
|
* PHPWord is free software distributed under the terms of the GNU Lesser
|
||||||
|
* General Public License version 3 as published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please read the LICENSE
|
||||||
|
* file that was distributed with this source code. For the full list of
|
||||||
|
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
|
||||||
|
*
|
||||||
|
* @see https://github.com/PHPOffice/PHPWord
|
||||||
|
* @copyright 2010-2018 PHPWord contributors
|
||||||
|
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace PhpOffice\PhpWord\Shared;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test class for XMLReader
|
||||||
|
*
|
||||||
|
* @coversDefaultClass PhpOffice\PhpWord\Shared\XMLReader
|
||||||
|
*/
|
||||||
|
class XMLReaderTest extends \PHPUnit\Framework\TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Test reading XML from string
|
||||||
|
*/
|
||||||
|
public function testDomFromString()
|
||||||
|
{
|
||||||
|
$reader = new XMLReader();
|
||||||
|
$reader->getDomFromString('<element attr="test"><child attr="subtest">AAA</child></element>');
|
||||||
|
|
||||||
|
$this->assertTrue($reader->elementExists('/element/child'));
|
||||||
|
$this->assertEquals('AAA', $reader->getElement('/element/child')->textContent);
|
||||||
|
$this->assertEquals('AAA', $reader->getValue('/element/child'));
|
||||||
|
$this->assertEquals('test', $reader->getAttribute('attr', $reader->getElement('/element')));
|
||||||
|
$this->assertEquals('subtest', $reader->getAttribute('attr', $reader->getElement('/element'), 'child'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test reading XML from zip
|
||||||
|
*/
|
||||||
|
public function testDomFromZip()
|
||||||
|
{
|
||||||
|
$archiveFile = __DIR__ . '/../_files/xml/reader.zip';
|
||||||
|
|
||||||
|
$reader = new XMLReader();
|
||||||
|
$reader->getDomFromZip($archiveFile, 'test.xml');
|
||||||
|
|
||||||
|
$this->assertTrue($reader->elementExists('/element/child'));
|
||||||
|
|
||||||
|
$this->assertFalse($reader->getDomFromZip($archiveFile, 'non_existing_xml_file.xml'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that read from non existing archive throws exception
|
||||||
|
*
|
||||||
|
* @expectedException Exception
|
||||||
|
*/
|
||||||
|
public function testThrowsExceptionOnNonExistingArchive()
|
||||||
|
{
|
||||||
|
$archiveFile = __DIR__ . '/../_files/xml/readers.zip';
|
||||||
|
|
||||||
|
$reader = new XMLReader();
|
||||||
|
$reader->getDomFromZip($archiveFile, 'test.xml');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test elements count
|
||||||
|
*/
|
||||||
|
public function testCountElements()
|
||||||
|
{
|
||||||
|
$reader = new XMLReader();
|
||||||
|
$reader->getDomFromString('<element attr="test"><child>AAA</child><child>BBB</child></element>');
|
||||||
|
|
||||||
|
$this->assertEquals(2, $reader->countElements('/element/child'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test read non existing elements
|
||||||
|
*/
|
||||||
|
public function testReturnNullOnNonExistingNode()
|
||||||
|
{
|
||||||
|
$reader = new XMLReader();
|
||||||
|
$this->assertEmpty($reader->getElements('/element/children'));
|
||||||
|
$reader->getDomFromString('<element><child>AAA</child></element>');
|
||||||
|
|
||||||
|
$this->assertNull($reader->getElement('/element/children'));
|
||||||
|
$this->assertNull($reader->getValue('/element/children'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that xpath fails if custom namespace is not registered
|
||||||
|
*/
|
||||||
|
public function testShouldThrowExceptionIfNamespaceIsNotKnown()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$reader = new XMLReader();
|
||||||
|
$reader->getDomFromString('<element><test:child xmlns:test="http://phpword.com/my/custom/namespace">AAA</test:child></element>');
|
||||||
|
|
||||||
|
$this->assertTrue($reader->elementExists('/element/test:child'));
|
||||||
|
$this->assertEquals('AAA', $reader->getElement('/element/test:child')->textContent);
|
||||||
|
$this->fail();
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$this->assertTrue(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test reading XML with manually registered namespace
|
||||||
|
*/
|
||||||
|
public function testShouldParseXmlWithCustomNamespace()
|
||||||
|
{
|
||||||
|
$reader = new XMLReader();
|
||||||
|
$reader->getDomFromString('<element><test:child xmlns:test="http://phpword.com/my/custom/namespace">AAA</test:child></element>');
|
||||||
|
$reader->registerNamespace('test', 'http://phpword.com/my/custom/namespace');
|
||||||
|
|
||||||
|
$this->assertTrue($reader->elementExists('/element/test:child'));
|
||||||
|
$this->assertEquals('AAA', $reader->getElement('/element/test:child')->textContent);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that xpath fails if custom namespace is not registered
|
||||||
|
*
|
||||||
|
* @expectedException InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function testShouldThowExceptionIfTryingToRegisterNamespaceBeforeReadingDoc()
|
||||||
|
{
|
||||||
|
$reader = new XMLReader();
|
||||||
|
$reader->registerNamespace('test', 'http://phpword.com/my/custom/namespace');
|
||||||
|
}
|
||||||
|
}
|
||||||
Binary file not shown.
Loading…
Reference in New Issue