PHPWord/tests/PhpWord/_includes/XmlDocument.php

195 lines
4.3 KiB
PHP
Raw Normal View History

<?php
2014-03-30 14:15:23 +07:00
/**
* 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.
2014-03-30 14:15:23 +07:00
*
* @see https://github.com/PHPOffice/PHPWord
2018-03-08 23:52:25 +01:00
* @copyright 2010-2018 PHPWord contributors
2014-05-04 21:03:28 +04:00
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
2014-03-30 14:15:23 +07:00
*/
2015-11-15 13:33:05 +04:00
namespace PhpOffice\PhpWord;
2014-03-30 14:15:23 +07:00
/**
* DOM wrapper class
*/
class XmlDocument
{
2014-03-30 14:15:23 +07:00
/**
* Path
*
* @var string
2014-03-30 14:15:23 +07:00
*/
private $path;
2014-03-30 14:15:23 +07:00
/**
* DOMDocument object
*
* @var \DOMDocument
*/
private $dom;
2014-03-30 14:15:23 +07:00
/**
2018-07-14 02:54:17 +02:00
* DOMXPath object
2014-03-30 14:15:23 +07:00
*
2018-07-14 02:54:17 +02:00
* @var \DOMXPath
2014-03-30 14:15:23 +07:00
*/
private $xpath;
2014-03-30 14:15:23 +07:00
/**
* File name
*
* @var string
*/
private $file;
/**
2014-03-30 14:15:23 +07:00
* Create new instance
*
* @param string $path
*/
public function __construct($path)
{
$this->path = realpath($path);
}
/**
2014-03-30 14:15:23 +07:00
* Get DOM from file
*
* @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;
2018-07-14 00:50:01 +02:00
libxml_disable_entity_loader(false);
$this->dom = new \DOMDocument();
$this->dom->load($file);
2018-07-14 00:50:01 +02:00
libxml_disable_entity_loader(true);
return $this->dom;
}
/**
2014-03-30 14:15:23 +07:00
* Get node list
*
* @param string $path
* @param string $file
* @return \DOMNodeList
*/
public function getNodeList($path, $file = 'word/document.xml')
{
if (null === $this->dom || $file !== $this->file) {
$this->getFileDom($file);
}
if (null === $this->xpath) {
2018-07-14 02:54:17 +02:00
$this->xpath = new \DOMXPath($this->dom);
$this->xpath->registerNamespace('w14', 'http://schemas.microsoft.com/office/word/2010/wordml');
}
return $this->xpath->query($path);
}
/**
2014-03-30 14:15:23 +07:00
* Get element
*
* @param string $path
* @param string $file
* @return \DOMElement
*/
public function getElement($path, $file = 'word/document.xml')
{
$elements = $this->getNodeList($path, $file);
return $elements->item(0);
}
2014-03-12 10:09:42 -04:00
/**
2014-03-30 14:15:23 +07:00
* Get file name
*
2014-03-12 10:09:42 -04:00
* @return string
*/
public function getFile()
{
return $this->file;
}
/**
2014-03-30 14:15:23 +07:00
* Get path
*
2014-03-12 10:09:42 -04:00
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
2014-03-30 14:15:23 +07:00
* Get element attribute
*
* @param string $path
* @param string $attribute
* @param string $file
* @return string
*/
public function getElementAttribute($path, $attribute, $file = 'word/document.xml')
{
return $this->getElement($path, $file)->getAttribute($attribute);
}
/**
2014-03-30 14:15:23 +07:00
* Check if element exists
*
* @param string $path
* @param string $file
* @return string
*/
public function elementExists($path, $file = 'word/document.xml')
{
$nodeList = $this->getNodeList($path, $file);
return !($nodeList->length == 0);
}
2017-11-06 21:47:02 +01:00
/**
* Returns the xml, or part of it as a formatted string
*
* @param string $path
* @param string $file
* @return string
*/
2017-11-09 00:41:56 +01:00
public function printXml($path = '/', $file = 'word/document.xml')
2017-11-06 21:47:02 +01:00
{
2017-11-09 00:41:56 +01:00
$element = $this->getElement($path, $file);
if ($element instanceof \DOMDocument) {
$element->formatOutput = true;
$element->preserveWhiteSpace = false;
return $element->saveXML();
}
2017-11-06 21:47:02 +01:00
$newdoc = new \DOMDocument();
$newdoc->formatOutput = true;
$newdoc->preserveWhiteSpace = false;
2017-11-09 00:41:56 +01:00
$node = $newdoc->importNode($element, true);
2017-11-06 21:47:02 +01:00
$newdoc->appendChild($node);
return $newdoc->saveXML($node);
}
}