Geen omschrijving

XMLWriter.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. * @link https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2014 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Shared;
  18. use PhpOffice\PhpWord\Settings;
  19. /**
  20. * XMLWriter wrapper
  21. *
  22. * @method bool endElement()
  23. * @method bool startDocument(string $version = 1.0, string $encoding = null, string $standalone = null)
  24. * @method bool startElement(string $name)
  25. * @method bool text(string $content)
  26. * @method bool writeAttribute(string $name, mixed $value)
  27. * @method bool writeElement(string $name, string $content = null)
  28. * @method bool writeRaw(string $content)
  29. */
  30. class XMLWriter
  31. {
  32. /** Temporary storage location */
  33. const STORAGE_MEMORY = 1;
  34. const STORAGE_DISK = 2;
  35. /**
  36. * Internal XMLWriter
  37. *
  38. * @var \XMLWriter
  39. */
  40. private $xmlWriter;
  41. /**
  42. * Temporary filename
  43. *
  44. * @var string
  45. */
  46. private $tempFile = '';
  47. /**
  48. * Create new XMLWriter
  49. *
  50. * @param int $tempLocation Temporary storage location
  51. * @param string $tempFolder Temporary storage folder
  52. */
  53. public function __construct($tempLocation = self::STORAGE_MEMORY, $tempFolder = './')
  54. {
  55. // Create internal XMLWriter
  56. $this->xmlWriter = new \XMLWriter();
  57. // Open temporary storage
  58. if ($tempLocation == self::STORAGE_MEMORY) {
  59. $this->xmlWriter->openMemory();
  60. } else {
  61. // Create temporary filename
  62. $this->tempFile = tempnam($tempFolder, 'xml');
  63. // Fallback to memory when temporary file cannot be used
  64. // @codeCoverageIgnoreStart
  65. // Can't find any test case. Uncomment when found.
  66. if (false === $this->tempFile || false === $this->xmlWriter->openUri($this->tempFile)) {
  67. $this->xmlWriter->openMemory();
  68. }
  69. // @codeCoverageIgnoreEnd
  70. }
  71. // Set xml Compatibility
  72. $compatibility = Settings::hasCompatibility();
  73. if ($compatibility) {
  74. $this->xmlWriter->setIndent(false);
  75. $this->xmlWriter->setIndentString('');
  76. } else {
  77. $this->xmlWriter->setIndent(true);
  78. $this->xmlWriter->setIndentString(' ');
  79. }
  80. }
  81. /**
  82. * Destructor
  83. */
  84. public function __destruct()
  85. {
  86. // Destruct XMLWriter
  87. unset($this->xmlWriter);
  88. // Unlink temporary files
  89. if ($this->tempFile != '') {
  90. @unlink($this->tempFile);
  91. }
  92. }
  93. /**
  94. * Catch function calls (and pass them to internal XMLWriter)
  95. *
  96. * @param mixed $function
  97. * @param mixed $args
  98. * @throws \BadMethodCallException
  99. */
  100. public function __call($function, $args)
  101. {
  102. // Catch exception
  103. if (method_exists($this->xmlWriter, $function) === false) {
  104. throw new \BadMethodCallException("Method '{$function}' does not exists.");
  105. }
  106. // Run method
  107. try {
  108. @call_user_func_array(array($this->xmlWriter, $function), $args);
  109. } catch (\Exception $ex) {
  110. // Do nothing!
  111. }
  112. }
  113. /**
  114. * Get written data
  115. *
  116. * @return string XML data
  117. */
  118. public function getData()
  119. {
  120. if ($this->tempFile == '') {
  121. return $this->xmlWriter->outputMemory(true);
  122. } else {
  123. $this->xmlWriter->flush();
  124. return file_get_contents($this->tempFile);
  125. }
  126. }
  127. /**
  128. * Write simple element and attribute(s) block
  129. *
  130. * There are two options:
  131. * 1. If the `$attributes` is an array, then it's an associative array of attributes
  132. * 2. If not, then it's a simple attribute-value pair
  133. *
  134. * @param string $element
  135. * @param string|array $attributes
  136. * @param string $value
  137. * @return void
  138. */
  139. public function writeElementBlock($element, $attributes, $value = null)
  140. {
  141. $this->xmlWriter->startElement($element);
  142. if (!is_array($attributes)) {
  143. $attributes = array($attributes => $value);
  144. }
  145. foreach ($attributes as $attribute => $value) {
  146. $this->xmlWriter->writeAttribute($attribute, $value);
  147. }
  148. $this->xmlWriter->endElement();
  149. }
  150. /**
  151. * Write element if ...
  152. *
  153. * @param bool $condition
  154. * @param string $element
  155. * @param string $attribute
  156. * @param mixed $value
  157. * @return void
  158. */
  159. public function writeElementIf($condition, $element, $attribute = null, $value = null)
  160. {
  161. if ($condition == true) {
  162. if (is_null($attribute)) {
  163. $this->xmlWriter->writeElement($element, $value);
  164. } else {
  165. $this->xmlWriter->startElement($element);
  166. $this->xmlWriter->writeAttribute($attribute, $value);
  167. $this->xmlWriter->endElement();
  168. }
  169. }
  170. }
  171. /**
  172. * Write attribute if ...
  173. *
  174. * @param bool $condition
  175. * @param string $attribute
  176. * @param mixed $value
  177. * @return void
  178. */
  179. public function writeAttributeIf($condition, $attribute, $value)
  180. {
  181. if ($condition == true) {
  182. $this->xmlWriter->writeAttribute($attribute, $value);
  183. }
  184. }
  185. }