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