Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
67 / 67
100.00% covered (success)
100.00%
15 / 15
CRAP
100.00% covered (success)
100.00%
1 / 1
PhpWord
100.00% covered (success)
100.00%
67 / 67
100.00% covered (success)
100.00%
15 / 15
27
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
3
 __call
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
6
 getDocInfo
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCompatibility
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSettings
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSections
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSection
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 addSection
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 sortSections
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDefaultFontName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setDefaultFontName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDefaultFontSize
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setDefaultFontSize
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setDefaultParagraphStyle
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 save
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
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
18namespace PhpOffice\PhpWord;
19
20use BadMethodCallException;
21use PhpOffice\PhpWord\Element\Section;
22use 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 */
45class 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            /** @var \PhpOffice\PhpWord\Collection\AbstractCollection $collectionObject */
138            $collectionObject = $this->collections[$key];
139
140            return $collectionObject->addItem($args[0] ?? null);
141        }
142
143        // Run add style method
144        if (in_array($function, $addStyle)) {
145            return forward_static_call_array(['PhpOffice\\PhpWord\\Style', $function], $args);
146        }
147
148        // Exception
149        throw new BadMethodCallException("Method $function is not defined.");
150    }
151
152    /**
153     * Get document properties object.
154     *
155     * @return \PhpOffice\PhpWord\Metadata\DocInfo
156     */
157    public function getDocInfo()
158    {
159        return $this->metadata['DocInfo'];
160    }
161
162    /**
163     * Get compatibility.
164     *
165     * @return \PhpOffice\PhpWord\Metadata\Compatibility
166     *
167     * @since 0.12.0
168     */
169    public function getCompatibility()
170    {
171        return $this->metadata['Compatibility'];
172    }
173
174    /**
175     * Get compatibility.
176     *
177     * @return \PhpOffice\PhpWord\Metadata\Settings
178     *
179     * @since 0.14.0
180     */
181    public function getSettings()
182    {
183        return $this->metadata['Settings'];
184    }
185
186    /**
187     * Get all sections.
188     *
189     * @return \PhpOffice\PhpWord\Element\Section[]
190     */
191    public function getSections()
192    {
193        return $this->sections;
194    }
195
196    /**
197     * Returns the section at the requested position.
198     *
199     * @param int $index
200     *
201     * @return null|\PhpOffice\PhpWord\Element\Section
202     */
203    public function getSection($index)
204    {
205        if (array_key_exists($index, $this->sections)) {
206            return $this->sections[$index];
207        }
208
209        return null;
210    }
211
212    /**
213     * Create new section.
214     *
215     * @param null|array|string $style
216     *
217     * @return \PhpOffice\PhpWord\Element\Section
218     */
219    public function addSection($style = null)
220    {
221        $section = new Section(count($this->sections) + 1, $style);
222        $section->setPhpWord($this);
223        $this->sections[] = $section;
224
225        return $section;
226    }
227
228    /**
229     * Sorts the sections using the callable passed.
230     *
231     * @see http://php.net/manual/en/function.usort.php for usage
232     *
233     * @param callable $sorter
234     */
235    public function sortSections($sorter): void
236    {
237        usort($this->sections, $sorter);
238    }
239
240    /**
241     * Get default font name.
242     *
243     * @return string
244     */
245    public function getDefaultFontName()
246    {
247        return Settings::getDefaultFontName();
248    }
249
250    /**
251     * Set default font name.
252     *
253     * @param string $fontName
254     */
255    public function setDefaultFontName($fontName): void
256    {
257        Settings::setDefaultFontName($fontName);
258    }
259
260    /**
261     * Get default font size.
262     *
263     * @return int
264     */
265    public function getDefaultFontSize()
266    {
267        return Settings::getDefaultFontSize();
268    }
269
270    /**
271     * Set default font size.
272     *
273     * @param int $fontSize
274     */
275    public function setDefaultFontSize($fontSize): void
276    {
277        Settings::setDefaultFontSize($fontSize);
278    }
279
280    /**
281     * Set default paragraph style definition to styles.xml.
282     *
283     * @param array $styles Paragraph style definition
284     *
285     * @return \PhpOffice\PhpWord\Style\Paragraph
286     */
287    public function setDefaultParagraphStyle($styles)
288    {
289        return Style::setDefaultParagraphStyle($styles);
290    }
291
292    /**
293     * Save to file or download.
294     *
295     * All exceptions should already been handled by the writers
296     *
297     * @param string $filename
298     * @param string $format
299     * @param bool $download
300     *
301     * @return bool
302     */
303    public function save($filename, $format = 'Word2007', $download = false)
304    {
305        $mime = [
306            'Word2007' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
307            'ODText' => 'application/vnd.oasis.opendocument.text',
308            'RTF' => 'application/rtf',
309            'HTML' => 'text/html',
310            'PDF' => 'application/pdf',
311        ];
312
313        $writer = IOFactory::createWriter($this, $format);
314
315        if ($download === true) {
316            header('Content-Description: File Transfer');
317            header('Content-Disposition: attachment; filename="' . $filename . '"');
318            header('Content-Type: ' . $mime[$format]);
319            header('Content-Transfer-Encoding: binary');
320            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
321            header('Expires: 0');
322            $filename = 'php://output'; // Change filename to force download
323        }
324
325        $writer->save($filename);
326
327        return true;
328    }
329
330    /**
331     * Create new section.
332     *
333     * @deprecated 0.10.0
334     *
335     * @param array $settings
336     *
337     * @return \PhpOffice\PhpWord\Element\Section
338     *
339     * @codeCoverageIgnore
340     */
341    public function createSection($settings = null)
342    {
343        return $this->addSection($settings);
344    }
345
346    /**
347     * Get document properties object.
348     *
349     * @deprecated 0.12.0
350     *
351     * @return \PhpOffice\PhpWord\Metadata\DocInfo
352     *
353     * @codeCoverageIgnore
354     */
355    public function getDocumentProperties()
356    {
357        return $this->getDocInfo();
358    }
359
360    /**
361     * Set document properties object.
362     *
363     * @deprecated 0.12.0
364     *
365     * @param \PhpOffice\PhpWord\Metadata\DocInfo $documentProperties
366     *
367     * @return self
368     *
369     * @codeCoverageIgnore
370     */
371    public function setDocumentProperties($documentProperties)
372    {
373        $this->metadata['Document'] = $documentProperties;
374
375        return $this;
376    }
377}