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