1 | <?php |
||
36 | class XMLWriter extends \XMLWriter |
||
37 | { |
||
38 | /** Temporary storage method */ |
||
39 | const STORAGE_MEMORY = 1; |
||
40 | const STORAGE_DISK = 2; |
||
41 | |||
42 | /** |
||
43 | * Temporary filename |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | private $tempFileName = ''; |
||
48 | |||
49 | /** |
||
50 | * Create a new \PhpOffice\PhpPowerpoint\Shared\XMLWriter instance |
||
51 | * |
||
52 | * @param int $pTemporaryStorage Temporary storage location |
||
53 | * @param string $pTemporaryStorageDir Temporary storage folder |
||
54 | */ |
||
55 | 1 | public function __construct($pTemporaryStorage = self::STORAGE_MEMORY, $pTemporaryStorageDir = './', $compatibility = false) |
|
56 | { |
||
57 | // Open temporary storage |
||
58 | 1 | if ($pTemporaryStorage == self::STORAGE_MEMORY) { |
|
59 | 1 | $this->openMemory(); |
|
60 | 1 | } else { |
|
61 | // Create temporary filename |
||
62 | 1 | $this->tempFileName = @tempnam($pTemporaryStorageDir, 'xml'); |
|
63 | |||
64 | // Open storage |
||
65 | 1 | $this->openUri($this->tempFileName); |
|
66 | } |
||
67 | |||
68 | 1 | if ($compatibility) { |
|
69 | $this->setIndent(false); |
||
70 | $this->setIndentString(''); |
||
71 | } else { |
||
72 | 1 | $this->setIndent(true); |
|
73 | 1 | $this->setIndentString(' '); |
|
74 | } |
||
75 | 1 | } |
|
76 | |||
77 | /** |
||
78 | * Destructor |
||
79 | */ |
||
80 | 1 | public function __destruct() |
|
81 | { |
||
82 | // Unlink temporary files |
||
83 | 1 | if ($this->tempFileName != '') { |
|
84 | 1 | if (@unlink($this->tempFileName) === false) { |
|
85 | throw new \Exception('The file '.$this->tempFileName.' could not be deleted.'); |
||
86 | } |
||
87 | 1 | } |
|
88 | 1 | } |
|
89 | |||
90 | /** |
||
91 | * Get written data |
||
92 | * |
||
93 | * @return string |
||
94 | */ |
||
95 | 1 | public function getData() |
|
104 | |||
105 | |||
106 | /** |
||
107 | * Write simple element and attribute(s) block |
||
108 | * |
||
109 | * There are two options: |
||
110 | * 1. If the `$attributes` is an array, then it's an associative array of attributes |
||
111 | * 2. If not, then it's a simple attribute-value pair |
||
112 | * |
||
113 | * @param string $element |
||
114 | * @param string|array $attributes |
||
115 | * @param string $value |
||
116 | * @return void |
||
117 | */ |
||
118 | public function writeElementBlock($element, $attributes, $value = null) |
||
129 | |||
130 | /** |
||
131 | * Write element if ... |
||
132 | * |
||
133 | * @param bool $condition |
||
134 | * @param string $element |
||
135 | * @param string $attribute |
||
136 | * @param mixed $value |
||
137 | * @return void |
||
138 | */ |
||
139 | public function writeElementIf($condition, $element, $attribute = null, $value = null) |
||
151 | |||
152 | /** |
||
153 | * Write attribute if ... |
||
154 | * |
||
155 | * @param bool $condition |
||
156 | * @param string $attribute |
||
157 | * @param mixed $value |
||
158 | * @return void |
||
159 | */ |
||
160 | public function writeAttributeIf($condition, $attribute, $value) |
||
166 | } |
||
167 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.