Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
67 / 67 |
|
100.00% |
15 / 15 |
CRAP | |
100.00% |
1 / 1 |
PhpWord | |
100.00% |
67 / 67 |
|
100.00% |
15 / 15 |
27 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 | |||
__call | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
6 | |||
getDocInfo | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getCompatibility | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getSettings | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getSections | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getSection | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
addSection | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
sortSections | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getDefaultFontName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setDefaultFontName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getDefaultFontSize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setDefaultFontSize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setDefaultParagraphStyle | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
save | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
2 | |||
createSection | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
getDocumentProperties | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
setDocumentProperties | n/a |
0 / 0 |
n/a |
0 / 0 |
1 |
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 | * |
15 | * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 |
16 | */ |
17 | |
18 | namespace PhpOffice\PhpWord; |
19 | |
20 | use BadMethodCallException; |
21 | use PhpOffice\PhpWord\Element\Section; |
22 | use PhpOffice\PhpWord\Exception\Exception; |
23 | |
24 | /** |
25 | * PHPWord main class. |
26 | * |
27 | * @method Collection\Titles getTitles() |
28 | * @method Collection\Footnotes getFootnotes() |
29 | * @method Collection\Endnotes getEndnotes() |
30 | * @method Collection\Charts getCharts() |
31 | * @method Collection\Comments getComments() |
32 | * @method int addBookmark(Element\Bookmark $bookmark) |
33 | * @method int addTitle(Element\Title $title) |
34 | * @method int addFootnote(Element\Footnote $footnote) |
35 | * @method int addEndnote(Element\Endnote $endnote) |
36 | * @method int addChart(Element\Chart $chart) |
37 | * @method int addComment(Element\Comment $comment) |
38 | * @method Style\Paragraph addParagraphStyle(string $styleName, mixed $styles) |
39 | * @method Style\Font addFontStyle(string $styleName, mixed $fontStyle, mixed $paragraphStyle = null) |
40 | * @method Style\Font addLinkStyle(string $styleName, mixed $styles) |
41 | * @method Style\Font addTitleStyle(mixed $depth, mixed $fontStyle, mixed $paragraphStyle = null) |
42 | * @method Style\Table addTableStyle(string $styleName, mixed $styleTable, mixed $styleFirstRow = null) |
43 | * @method Style\Numbering addNumberingStyle(string $styleName, mixed $styles) |
44 | */ |
45 | class PhpWord |
46 | { |
47 | /** |
48 | * Collection of sections. |
49 | * |
50 | * @var \PhpOffice\PhpWord\Element\Section[] |
51 | */ |
52 | private $sections = []; |
53 | |
54 | /** |
55 | * Collections. |
56 | * |
57 | * @var array |
58 | */ |
59 | private $collections = []; |
60 | |
61 | /** |
62 | * Metadata. |
63 | * |
64 | * @var array |
65 | * |
66 | * @since 0.12.0 |
67 | */ |
68 | private $metadata = []; |
69 | |
70 | /** |
71 | * Create new instance. |
72 | * |
73 | * Collections are created dynamically |
74 | */ |
75 | public function __construct() |
76 | { |
77 | // Reset Media and styles |
78 | Media::resetElements(); |
79 | Style::resetStyles(); |
80 | Settings::setDefaultRtl(null); |
81 | |
82 | // Collection |
83 | $collections = ['Bookmarks', 'Titles', 'Footnotes', 'Endnotes', 'Charts', 'Comments']; |
84 | foreach ($collections as $collection) { |
85 | $class = 'PhpOffice\\PhpWord\\Collection\\' . $collection; |
86 | $this->collections[$collection] = new $class(); |
87 | } |
88 | |
89 | // Metadata |
90 | $metadata = ['DocInfo', 'Settings', 'Compatibility']; |
91 | foreach ($metadata as $meta) { |
92 | $class = 'PhpOffice\\PhpWord\\Metadata\\' . $meta; |
93 | $this->metadata[$meta] = new $class(); |
94 | } |
95 | } |
96 | |
97 | /** |
98 | * Dynamic function call to reduce static dependency. |
99 | * |
100 | * @since 0.12.0 |
101 | * |
102 | * @param mixed $function |
103 | * @param mixed $args |
104 | * |
105 | * @return mixed |
106 | */ |
107 | public function __call($function, $args) |
108 | { |
109 | $function = strtolower($function); |
110 | |
111 | $getCollection = []; |
112 | $addCollection = []; |
113 | $addStyle = []; |
114 | |
115 | $collections = ['Bookmark', 'Title', 'Footnote', 'Endnote', 'Chart', 'Comment']; |
116 | foreach ($collections as $collection) { |
117 | $getCollection[] = strtolower("get{$collection}s"); |
118 | $addCollection[] = strtolower("add{$collection}"); |
119 | } |
120 | |
121 | $styles = ['Paragraph', 'Font', 'Table', 'Numbering', 'Link', 'Title']; |
122 | foreach ($styles as $style) { |
123 | $addStyle[] = strtolower("add{$style}Style"); |
124 | } |
125 | |
126 | // Run get collection method |
127 | if (in_array($function, $getCollection)) { |
128 | $key = ucfirst(str_replace('get', '', $function)); |
129 | |
130 | return $this->collections[$key]; |
131 | } |
132 | |
133 | // Run add collection item method |
134 | if (in_array($function, $addCollection)) { |
135 | $key = ucfirst(str_replace('add', '', $function) . 's'); |
136 | |
137 | $collectionObject = $this->collections[$key]; |
138 | |
139 | return $collectionObject->addItem($args[0] ?? null); |
140 | } |
141 | |
142 | // Run add style method |
143 | if (in_array($function, $addStyle)) { |
144 | return forward_static_call_array(['PhpOffice\\PhpWord\\Style', $function], $args); |
145 | } |
146 | |
147 | // Exception |
148 | throw new BadMethodCallException("Method $function is not defined."); |
149 | } |
150 | |
151 | /** |
152 | * Get document properties object. |
153 | * |
154 | * @return \PhpOffice\PhpWord\Metadata\DocInfo |
155 | */ |
156 | public function getDocInfo() |
157 | { |
158 | return $this->metadata['DocInfo']; |
159 | } |
160 | |
161 | /** |
162 | * Get compatibility. |
163 | * |
164 | * @return \PhpOffice\PhpWord\Metadata\Compatibility |
165 | * |
166 | * @since 0.12.0 |
167 | */ |
168 | public function getCompatibility() |
169 | { |
170 | return $this->metadata['Compatibility']; |
171 | } |
172 | |
173 | /** |
174 | * Get compatibility. |
175 | * |
176 | * @return \PhpOffice\PhpWord\Metadata\Settings |
177 | * |
178 | * @since 0.14.0 |
179 | */ |
180 | public function getSettings() |
181 | { |
182 | return $this->metadata['Settings']; |
183 | } |
184 | |
185 | /** |
186 | * Get all sections. |
187 | * |
188 | * @return \PhpOffice\PhpWord\Element\Section[] |
189 | */ |
190 | public function getSections() |
191 | { |
192 | return $this->sections; |
193 | } |
194 | |
195 | /** |
196 | * Returns the section at the requested position. |
197 | * |
198 | * @param int $index |
199 | * |
200 | * @return null|\PhpOffice\PhpWord\Element\Section |
201 | */ |
202 | public function getSection($index) |
203 | { |
204 | if (array_key_exists($index, $this->sections)) { |
205 | return $this->sections[$index]; |
206 | } |
207 | |
208 | return null; |
209 | } |
210 | |
211 | /** |
212 | * Create new section. |
213 | * |
214 | * @param null|array|string $style |
215 | * |
216 | * @return \PhpOffice\PhpWord\Element\Section |
217 | */ |
218 | public function addSection($style = null) |
219 | { |
220 | $section = new Section(count($this->sections) + 1, $style); |
221 | $section->setPhpWord($this); |
222 | $this->sections[] = $section; |
223 | |
224 | return $section; |
225 | } |
226 | |
227 | /** |
228 | * Sorts the sections using the callable passed. |
229 | * |
230 | * @see http://php.net/manual/en/function.usort.php for usage |
231 | * |
232 | * @param callable $sorter |
233 | */ |
234 | public function sortSections($sorter): void |
235 | { |
236 | usort($this->sections, $sorter); |
237 | } |
238 | |
239 | /** |
240 | * Get default font name. |
241 | * |
242 | * @return string |
243 | */ |
244 | public function getDefaultFontName() |
245 | { |
246 | return Settings::getDefaultFontName(); |
247 | } |
248 | |
249 | /** |
250 | * Set default font name. |
251 | * |
252 | * @param string $fontName |
253 | */ |
254 | public function setDefaultFontName($fontName): void |
255 | { |
256 | Settings::setDefaultFontName($fontName); |
257 | } |
258 | |
259 | /** |
260 | * Get default font size. |
261 | * |
262 | * @return int |
263 | */ |
264 | public function getDefaultFontSize() |
265 | { |
266 | return Settings::getDefaultFontSize(); |
267 | } |
268 | |
269 | /** |
270 | * Set default font size. |
271 | * |
272 | * @param int $fontSize |
273 | */ |
274 | public function setDefaultFontSize($fontSize): void |
275 | { |
276 | Settings::setDefaultFontSize($fontSize); |
277 | } |
278 | |
279 | /** |
280 | * Set default paragraph style definition to styles.xml. |
281 | * |
282 | * @param array $styles Paragraph style definition |
283 | * |
284 | * @return \PhpOffice\PhpWord\Style\Paragraph |
285 | */ |
286 | public function setDefaultParagraphStyle($styles) |
287 | { |
288 | return Style::setDefaultParagraphStyle($styles); |
289 | } |
290 | |
291 | /** |
292 | * Save to file or download. |
293 | * |
294 | * All exceptions should already been handled by the writers |
295 | * |
296 | * @param string $filename |
297 | * @param string $format |
298 | * @param bool $download |
299 | * |
300 | * @return bool |
301 | */ |
302 | public function save($filename, $format = 'Word2007', $download = false) |
303 | { |
304 | $mime = [ |
305 | 'Word2007' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
306 | 'ODText' => 'application/vnd.oasis.opendocument.text', |
307 | 'RTF' => 'application/rtf', |
308 | 'HTML' => 'text/html', |
309 | 'PDF' => 'application/pdf', |
310 | ]; |
311 | |
312 | $writer = IOFactory::createWriter($this, $format); |
313 | |
314 | if ($download === true) { |
315 | header('Content-Description: File Transfer'); |
316 | header('Content-Disposition: attachment; filename="' . $filename . '"'); |
317 | header('Content-Type: ' . $mime[$format]); |
318 | header('Content-Transfer-Encoding: binary'); |
319 | header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); |
320 | header('Expires: 0'); |
321 | $filename = 'php://output'; // Change filename to force download |
322 | } |
323 | |
324 | $writer->save($filename); |
325 | |
326 | return true; |
327 | } |
328 | |
329 | /** |
330 | * Create new section. |
331 | * |
332 | * @deprecated 0.10.0 |
333 | * |
334 | * @param array $settings |
335 | * |
336 | * @return \PhpOffice\PhpWord\Element\Section |
337 | * |
338 | * @codeCoverageIgnore |
339 | */ |
340 | public function createSection($settings = null) |
341 | { |
342 | return $this->addSection($settings); |
343 | } |
344 | |
345 | /** |
346 | * Get document properties object. |
347 | * |
348 | * @deprecated 0.12.0 |
349 | * |
350 | * @return \PhpOffice\PhpWord\Metadata\DocInfo |
351 | * |
352 | * @codeCoverageIgnore |
353 | */ |
354 | public function getDocumentProperties() |
355 | { |
356 | return $this->getDocInfo(); |
357 | } |
358 | |
359 | /** |
360 | * Set document properties object. |
361 | * |
362 | * @deprecated 0.12.0 |
363 | * |
364 | * @param \PhpOffice\PhpWord\Metadata\DocInfo $documentProperties |
365 | * |
366 | * @return self |
367 | * |
368 | * @codeCoverageIgnore |
369 | */ |
370 | public function setDocumentProperties($documentProperties) |
371 | { |
372 | $this->metadata['Document'] = $documentProperties; |
373 | |
374 | return $this; |
375 | } |
376 | } |