XMLReader.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /**
  3. * This file is part of PHPWord - A pure PHP library for reading and writing
  4. * word processing documents.
  5. *
  6. * PHPWord is free software distributed under the terms of the GNU Lesser
  7. * General Public License version 3 as published by the Free Software Foundation.
  8. *
  9. * For the full copyright and license information, please read the LICENSE
  10. * file that was distributed with this source code. For the full list of
  11. * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
  12. *
  13. * @see https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2018 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Shared;
  18. /**
  19. * XML Reader wrapper
  20. *
  21. * @since 0.2.1
  22. */
  23. class XMLReader
  24. {
  25. /**
  26. * DOMDocument object
  27. *
  28. * @var \DOMDocument
  29. */
  30. private $dom = null;
  31. /**
  32. * DOMXpath object
  33. *
  34. * @var \DOMXpath
  35. */
  36. private $xpath = null;
  37. /**
  38. * Get DOMDocument from ZipArchive
  39. *
  40. * @param string $zipFile
  41. * @param string $xmlFile
  42. * @throws \Exception
  43. * @return \DOMDocument|false
  44. */
  45. public function getDomFromZip($zipFile, $xmlFile)
  46. {
  47. if (file_exists($zipFile) === false) {
  48. throw new \Exception('Cannot find archive file.');
  49. }
  50. $zip = new \ZipArchive();
  51. $zip->open($zipFile);
  52. $content = $zip->getFromName($xmlFile);
  53. $zip->close();
  54. if ($content === false) {
  55. return false;
  56. }
  57. return $this->getDomFromString($content);
  58. }
  59. /**
  60. * Get DOMDocument from content string
  61. *
  62. * @param string $content
  63. * @return \DOMDocument
  64. */
  65. public function getDomFromString($content)
  66. {
  67. if (\PHP_VERSION_ID < 80000) {
  68. $originalLibXMLEntityValue = libxml_disable_entity_loader(true);
  69. }
  70. $this->dom = new \DOMDocument();
  71. $this->dom->loadXML($content);
  72. if (\PHP_VERSION_ID < 80000) {
  73. libxml_disable_entity_loader($originalLibXMLEntityValue);
  74. }
  75. return $this->dom;
  76. }
  77. /**
  78. * Get elements
  79. *
  80. * @param string $path
  81. * @param \DOMElement $contextNode
  82. * @return \DOMNodeList
  83. */
  84. public function getElements($path, \DOMElement $contextNode = null)
  85. {
  86. if ($this->dom === null) {
  87. return array();
  88. }
  89. if ($this->xpath === null) {
  90. $this->xpath = new \DOMXpath($this->dom);
  91. }
  92. if (is_null($contextNode)) {
  93. return $this->xpath->query($path);
  94. }
  95. return $this->xpath->query($path, $contextNode);
  96. }
  97. /**
  98. * Registers the namespace with the DOMXPath object
  99. *
  100. * @param string $prefix The prefix
  101. * @param string $namespaceURI The URI of the namespace
  102. * @throws \InvalidArgumentException If called before having loaded the DOM document
  103. * @return bool true on success or false on failure
  104. */
  105. public function registerNamespace($prefix, $namespaceURI)
  106. {
  107. if ($this->dom === null) {
  108. throw new \InvalidArgumentException('Dom needs to be loaded before registering a namespace');
  109. }
  110. if ($this->xpath === null) {
  111. $this->xpath = new \DOMXpath($this->dom);
  112. }
  113. return $this->xpath->registerNamespace($prefix, $namespaceURI);
  114. }
  115. /**
  116. * Get element
  117. *
  118. * @param string $path
  119. * @param \DOMElement $contextNode
  120. * @return \DOMElement|null
  121. */
  122. public function getElement($path, \DOMElement $contextNode = null)
  123. {
  124. $elements = $this->getElements($path, $contextNode);
  125. if ($elements->length > 0) {
  126. return $elements->item(0);
  127. }
  128. return null;
  129. }
  130. /**
  131. * Get element attribute
  132. *
  133. * @param string $attribute
  134. * @param \DOMElement $contextNode
  135. * @param string $path
  136. * @return string|null
  137. */
  138. public function getAttribute($attribute, \DOMElement $contextNode = null, $path = null)
  139. {
  140. $return = null;
  141. if ($path !== null) {
  142. $elements = $this->getElements($path, $contextNode);
  143. if ($elements->length > 0) {
  144. /** @var \DOMElement $node Type hint */
  145. $node = $elements->item(0);
  146. $return = $node->getAttribute($attribute);
  147. }
  148. } else {
  149. if ($contextNode !== null) {
  150. $return = $contextNode->getAttribute($attribute);
  151. }
  152. }
  153. return ($return == '') ? null : $return;
  154. }
  155. /**
  156. * Get element value
  157. *
  158. * @param string $path
  159. * @param \DOMElement $contextNode
  160. * @return string|null
  161. */
  162. public function getValue($path, \DOMElement $contextNode = null)
  163. {
  164. $elements = $this->getElements($path, $contextNode);
  165. if ($elements->length > 0) {
  166. return $elements->item(0)->nodeValue;
  167. }
  168. return null;
  169. }
  170. /**
  171. * Count elements
  172. *
  173. * @param string $path
  174. * @param \DOMElement $contextNode
  175. * @return int
  176. */
  177. public function countElements($path, \DOMElement $contextNode = null)
  178. {
  179. $elements = $this->getElements($path, $contextNode);
  180. return $elements->length;
  181. }
  182. /**
  183. * Element exists
  184. *
  185. * @param string $path
  186. * @param \DOMElement $contextNode
  187. * @return bool
  188. */
  189. public function elementExists($path, \DOMElement $contextNode = null)
  190. {
  191. return $this->getElements($path, $contextNode)->length > 0;
  192. }
  193. }