XMLWriter.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. * XMLWriter
  20. *
  21. * @method bool endElement()
  22. * @method mixed flush(bool $empty = null)
  23. * @method bool openMemory()
  24. * @method string outputMemory(bool $flush = null)
  25. * @method bool setIndent(bool $indent)
  26. * @method bool startDocument(string $version = 1.0, string $encoding = null, string $standalone = null)
  27. * @method bool startElement(string $name)
  28. * @method bool text(string $content)
  29. * @method bool writeCData(string $content)
  30. * @method bool writeComment(string $content)
  31. * @method bool writeElement(string $name, string $content = null)
  32. * @method bool writeRaw(string $content)
  33. */
  34. class XMLWriter extends \XMLWriter
  35. {
  36. /** Temporary storage method */
  37. const STORAGE_MEMORY = 1;
  38. const STORAGE_DISK = 2;
  39. /**
  40. * Temporary filename
  41. *
  42. * @var string
  43. */
  44. private $tempFileName = '';
  45. /**
  46. * Create a new \PhpOffice\PhpWord\Shared\XMLWriter instance
  47. *
  48. * @param int $pTemporaryStorage Temporary storage location
  49. * @param string $pTemporaryStorageDir Temporary storage folder
  50. * @param bool $compatibility
  51. */
  52. public function __construct($pTemporaryStorage = self::STORAGE_MEMORY, $pTemporaryStorageDir = null, $compatibility = false)
  53. {
  54. // Open temporary storage
  55. if ($pTemporaryStorage == self::STORAGE_MEMORY) {
  56. $this->openMemory();
  57. } else {
  58. if (!is_dir($pTemporaryStorageDir)) {
  59. $pTemporaryStorageDir = sys_get_temp_dir();
  60. }
  61. // Create temporary filename
  62. $this->tempFileName = @tempnam($pTemporaryStorageDir, 'xml');
  63. // Open storage
  64. $this->openUri($this->tempFileName);
  65. }
  66. if ($compatibility) {
  67. $this->setIndent(false);
  68. $this->setIndentString('');
  69. } else {
  70. $this->setIndent(true);
  71. $this->setIndentString(' ');
  72. }
  73. }
  74. /**
  75. * Destructor
  76. */
  77. public function __destruct()
  78. {
  79. // Unlink temporary files
  80. if (empty($this->tempFileName)) {
  81. return;
  82. }
  83. if (PHP_OS != 'WINNT' && @unlink($this->tempFileName) === false) {
  84. throw new \Exception('The file ' . $this->tempFileName . ' could not be deleted.');
  85. }
  86. }
  87. /**
  88. * Get written data
  89. *
  90. * @return string
  91. */
  92. public function getData()
  93. {
  94. if ($this->tempFileName == '') {
  95. return $this->outputMemory(true);
  96. }
  97. $this->flush();
  98. return file_get_contents($this->tempFileName);
  99. }
  100. /**
  101. * Write simple element and attribute(s) block
  102. *
  103. * There are two options:
  104. * 1. If the `$attributes` is an array, then it's an associative array of attributes
  105. * 2. If not, then it's a simple attribute-value pair
  106. *
  107. * @param string $element
  108. * @param string|array $attributes
  109. * @param string $value
  110. */
  111. public function writeElementBlock($element, $attributes, $value = null)
  112. {
  113. $this->startElement($element);
  114. if (!is_array($attributes)) {
  115. $attributes = array($attributes => $value);
  116. }
  117. foreach ($attributes as $attribute => $value) {
  118. $this->writeAttribute($attribute, $value);
  119. }
  120. $this->endElement();
  121. }
  122. /**
  123. * Write element if ...
  124. *
  125. * @param bool $condition
  126. * @param string $element
  127. * @param string $attribute
  128. * @param mixed $value
  129. */
  130. public function writeElementIf($condition, $element, $attribute = null, $value = null)
  131. {
  132. if ($condition == true) {
  133. if (is_null($attribute)) {
  134. $this->writeElement($element, $value);
  135. } else {
  136. $this->startElement($element);
  137. $this->writeAttribute($attribute, $value);
  138. $this->endElement();
  139. }
  140. }
  141. }
  142. /**
  143. * Write attribute if ...
  144. *
  145. * @param bool $condition
  146. * @param string $attribute
  147. * @param mixed $value
  148. */
  149. public function writeAttributeIf($condition, $attribute, $value)
  150. {
  151. if ($condition == true) {
  152. $this->writeAttribute($attribute, $value);
  153. }
  154. }
  155. /**
  156. * @param string $name
  157. * @param mixed $value
  158. * @return bool
  159. */
  160. public function writeAttribute($name, $value)
  161. {
  162. if (is_float($value)) {
  163. $value = json_encode($value);
  164. }
  165. return parent::writeAttribute($name, $value);
  166. }
  167. }