XmlDocument.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * This file is part of PHPPowerPoint - A pure PHP library for reading and writing
  4. * word processing documents.
  5. *
  6. * PHPPowerPoint 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/PHPPowerPoint/contributors.
  12. *
  13. * @link https://github.com/PHPOffice/PHPPowerPoint
  14. * @copyright 2010-2016 PHPPowerPoint contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpPowerpoint\Tests;
  18. /**
  19. * DOM wrapper class
  20. */
  21. class XmlDocument
  22. {
  23. /**
  24. * Path
  25. *
  26. * @var string $path
  27. */
  28. private $path;
  29. /**
  30. * DOMDocument object
  31. *
  32. * @var \DOMDocument
  33. */
  34. private $dom;
  35. /**
  36. * DOMXpath object
  37. *
  38. * @var \DOMXpath
  39. */
  40. private $xpath;
  41. /**
  42. * File name
  43. *
  44. * @var string
  45. */
  46. private $file;
  47. /**
  48. * Create new instance
  49. *
  50. * @param string $path
  51. */
  52. public function __construct($path)
  53. {
  54. $this->path = realpath($path);
  55. }
  56. /**
  57. * Get DOM from file
  58. *
  59. * @param string $file
  60. * @return \DOMDocument
  61. */
  62. public function getFileDom($file = 'word/document.xml')
  63. {
  64. if (null !== $this->dom && $file === $this->file) {
  65. return $this->dom;
  66. }
  67. $this->xpath = null;
  68. $this->file = $file;
  69. $file = $this->path . '/' . $file;
  70. $this->dom = new \DOMDocument();
  71. $this->dom->load($file);
  72. return $this->dom;
  73. }
  74. /**
  75. * Get node list
  76. *
  77. * @param string $path
  78. * @param string $file
  79. * @return \DOMNodeList
  80. */
  81. public function getNodeList($path, $file = 'word/document.xml')
  82. {
  83. if ($this->dom === null || $file !== $this->file) {
  84. $this->getFileDom($file);
  85. }
  86. if (null === $this->xpath) {
  87. $this->xpath = new \DOMXpath($this->dom);
  88. }
  89. return $this->xpath->query($path);
  90. }
  91. /**
  92. * Get element
  93. *
  94. * @param string $path
  95. * @param string $file
  96. * @return \DOMElement
  97. */
  98. public function getElement($path, $file = 'word/document.xml')
  99. {
  100. $elements = $this->getNodeList($path, $file);
  101. return $elements->item(0);
  102. }
  103. /**
  104. * Get file name
  105. *
  106. * @return string
  107. */
  108. public function getFile()
  109. {
  110. return $this->file;
  111. }
  112. /**
  113. * Get path
  114. *
  115. * @return string
  116. */
  117. public function getPath()
  118. {
  119. return $this->path;
  120. }
  121. /**
  122. * Get element attribute
  123. *
  124. * @param string $path
  125. * @param string $attribute
  126. * @param string $file
  127. * @return string
  128. */
  129. public function getElementAttribute($path, $attribute, $file = 'word/document.xml')
  130. {
  131. return $this->getElement($path, $file)->getAttribute($attribute);
  132. }
  133. /**
  134. * Get element attribute
  135. *
  136. * @param string $path
  137. * @param string $attribute
  138. * @param string $file
  139. * @return string
  140. */
  141. public function attributeElementExists($path, $attribute, $file = 'word/document.xml')
  142. {
  143. return $this->getElement($path, $file)->hasAttribute($attribute);
  144. }
  145. /**
  146. * Check if element exists
  147. *
  148. * @param string $path
  149. * @param string $file
  150. * @return string
  151. */
  152. public function elementExists($path, $file = 'word/document.xml')
  153. {
  154. $nodeList = $this->getNodeList($path, $file);
  155. return !($nodeList->length == 0);
  156. }
  157. }