XMLWriter.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * This file is part of PHPOffice Common
  4. *
  5. * PHPOffice Common is free software distributed under the terms of the GNU Lesser
  6. * General Public License version 3 as published by the Free Software Foundation.
  7. *
  8. * For the full copyright and license information, please read the LICENSE
  9. * file that was distributed with this source code. For the full list of
  10. * contributors, visit https://github.com/PHPOffice/Common/contributors.
  11. *
  12. * @link https://github.com/PHPOffice/Common
  13. * @copyright 2009-2016 PHPOffice Common contributors
  14. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  15. */
  16. namespace PhpOffice\Common;
  17. /**
  18. * XMLWriter
  19. *
  20. * @method bool endElement()
  21. * @method mixed flush(bool $empty = null)
  22. * @method bool openMemory()
  23. * @method string outputMemory(bool $flush = null)
  24. * @method bool setIndent(bool $indent)
  25. * @method bool startDocument(string $version = 1.0, string $encoding = null, string $standalone = null)
  26. * @method bool startElement(string $name)
  27. * @method bool text(string $content)
  28. * @method bool writeCData(string $content)
  29. * @method bool writeComment(string $content)
  30. * @method bool writeElement(string $name, string $content = null)
  31. * @method bool writeRaw(string $content)
  32. */
  33. class XMLWriter extends \XMLWriter
  34. {
  35. /** Temporary storage method */
  36. const STORAGE_MEMORY = 1;
  37. const STORAGE_DISK = 2;
  38. /**
  39. * Temporary filename
  40. *
  41. * @var string
  42. */
  43. private $tempFileName = '';
  44. /**
  45. * Create a new \PhpOffice\PhpPowerpoint\Shared\XMLWriter instance
  46. *
  47. * @param int $pTemporaryStorage Temporary storage location
  48. * @param string $pTemporaryStorageDir Temporary storage folder
  49. */
  50. public function __construct($pTemporaryStorage = self::STORAGE_MEMORY, $pTemporaryStorageDir = null, $compatibility = false)
  51. {
  52. // Open temporary storage
  53. if ($pTemporaryStorage == self::STORAGE_MEMORY) {
  54. $this->openMemory();
  55. } else {
  56. if (!is_dir($pTemporaryStorageDir)) {
  57. $pTemporaryStorageDir = sys_get_temp_dir();
  58. }
  59. // Create temporary filename
  60. $this->tempFileName = @tempnam($pTemporaryStorageDir, 'xml');
  61. // Open storage
  62. $this->openUri($this->tempFileName);
  63. }
  64. if ($compatibility) {
  65. $this->setIndent(false);
  66. $this->setIndentString('');
  67. } else {
  68. $this->setIndent(true);
  69. $this->setIndentString(' ');
  70. }
  71. }
  72. /**
  73. * Destructor
  74. */
  75. public function __destruct()
  76. {
  77. // Unlink temporary files
  78. if (empty($this->tempFileName)) {
  79. return;
  80. }
  81. if (PHP_OS != 'WINNT' && @unlink($this->tempFileName) === false) {
  82. throw new \Exception('The file '.$this->tempFileName.' could not be deleted.');
  83. }
  84. }
  85. /**
  86. * Get written data
  87. *
  88. * @return string
  89. */
  90. public function getData()
  91. {
  92. if ($this->tempFileName == '') {
  93. return $this->outputMemory(true);
  94. }
  95. $this->flush();
  96. return file_get_contents($this->tempFileName);
  97. }
  98. /**
  99. * Write simple element and attribute(s) block
  100. *
  101. * There are two options:
  102. * 1. If the `$attributes` is an array, then it's an associative array of attributes
  103. * 2. If not, then it's a simple attribute-value pair
  104. *
  105. * @param string $element
  106. * @param string|array $attributes
  107. * @param string $value
  108. * @return void
  109. */
  110. public function writeElementBlock($element, $attributes, $value = null)
  111. {
  112. $this->startElement($element);
  113. if (!is_array($attributes)) {
  114. $attributes = array($attributes => $value);
  115. }
  116. foreach ($attributes as $attribute => $value) {
  117. $this->writeAttribute($attribute, $value);
  118. }
  119. $this->endElement();
  120. }
  121. /**
  122. * Write element if ...
  123. *
  124. * @param bool $condition
  125. * @param string $element
  126. * @param string $attribute
  127. * @param mixed $value
  128. * @return void
  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. * @return void
  149. */
  150. public function writeAttributeIf($condition, $attribute, $value)
  151. {
  152. if ($condition == true) {
  153. $this->writeAttribute($attribute, $value);
  154. }
  155. }
  156. /**
  157. * @param string $name
  158. * @param mixed $value
  159. * @return bool
  160. */
  161. public function writeAttribute($name, $value)
  162. {
  163. if (is_float($value)) {
  164. $value = json_encode($value);
  165. }
  166. return parent::writeAttribute($name, $value);
  167. }
  168. }