Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
71 / 71 |
|
100.00% |
19 / 19 |
CRAP | |
100.00% |
1 / 1 |
| PhpWord | |
100.00% |
71 / 71 |
|
100.00% |
19 / 19 |
31 | |
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 | |||
| getDefaultAsianFontName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setDefaultAsianFontName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setDefaultFontColor | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getDefaultFontColor | |
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 | /** |
| 4 | * This file is part of PHPWord - A pure PHP library for reading and writing |
| 5 | * word processing documents. |
| 6 | * |
| 7 | * PHPWord is free software distributed under the terms of the GNU Lesser |
| 8 | * General Public License version 3 as published by the Free Software Foundation. |
| 9 | * |
| 10 | * For the full copyright and license information, please read the LICENSE |
| 11 | * file that was distributed with this source code. For the full list of |
| 12 | * contributors, visit https://github.com/PHPOffice/PHPWord/contributors. |
| 13 | * |
| 14 | * @see https://github.com/PHPOffice/PHPWord |
| 15 | * |
| 16 | * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 |
| 17 | */ |
| 18 | |
| 19 | namespace PhpOffice\PhpWord; |
| 20 | |
| 21 | use BadMethodCallException; |
| 22 | use PhpOffice\PhpWord\Element\Section; |
| 23 | use PhpOffice\PhpWord\Exception\Exception; |
| 24 | |
| 25 | /** |
| 26 | * PHPWord main class. |
| 27 | * |
| 28 | * @method Collection\Titles getTitles() |
| 29 | * @method Collection\Footnotes getFootnotes() |
| 30 | * @method Collection\Endnotes getEndnotes() |
| 31 | * @method Collection\Charts getCharts() |
| 32 | * @method Collection\Comments getComments() |
| 33 | * @method int addBookmark(Element\Bookmark $bookmark) |
| 34 | * @method int addTitle(Element\Title $title) |
| 35 | * @method int addFootnote(Element\Footnote $footnote) |
| 36 | * @method int addEndnote(Element\Endnote $endnote) |
| 37 | * @method int addChart(Element\Chart $chart) |
| 38 | * @method int addComment(Element\Comment $comment) |
| 39 | * @method Style\Paragraph addParagraphStyle(string $styleName, mixed $styles) |
| 40 | * @method Style\Font addFontStyle(string $styleName, mixed $fontStyle, mixed $paragraphStyle = null) |
| 41 | * @method Style\Font addLinkStyle(string $styleName, mixed $styles) |
| 42 | * @method Style\Font addTitleStyle(mixed $depth, mixed $fontStyle, mixed $paragraphStyle = null) |
| 43 | * @method Style\Table addTableStyle(string $styleName, mixed $styleTable, mixed $styleFirstRow = null) |
| 44 | * @method Style\Numbering addNumberingStyle(string $styleName, mixed $styles) |
| 45 | */ |
| 46 | class PhpWord |
| 47 | { |
| 48 | /** |
| 49 | * Collection of sections. |
| 50 | * |
| 51 | * @var Section[] |
| 52 | */ |
| 53 | private $sections = []; |
| 54 | |
| 55 | /** |
| 56 | * Collections. |
| 57 | * |
| 58 | * @var array |
| 59 | */ |
| 60 | private $collections = []; |
| 61 | |
| 62 | /** |
| 63 | * Metadata. |
| 64 | * |
| 65 | * @var array |
| 66 | * |
| 67 | * @since 0.12.0 |
| 68 | */ |
| 69 | private $metadata = []; |
| 70 | |
| 71 | /** |
| 72 | * Create new instance. |
| 73 | * |
| 74 | * Collections are created dynamically |
| 75 | */ |
| 76 | public function __construct() |
| 77 | { |
| 78 | // Reset Media and styles |
| 79 | Media::resetElements(); |
| 80 | Style::resetStyles(); |
| 81 | Settings::setDefaultRtl(null); |
| 82 | |
| 83 | // Collection |
| 84 | $collections = ['Bookmarks', 'Titles', 'Footnotes', 'Endnotes', 'Charts', 'Comments']; |
| 85 | foreach ($collections as $collection) { |
| 86 | $class = 'PhpOffice\\PhpWord\\Collection\\' . $collection; |
| 87 | $this->collections[$collection] = new $class(); |
| 88 | } |
| 89 | |
| 90 | // Metadata |
| 91 | $metadata = ['DocInfo', 'Settings', 'Compatibility']; |
| 92 | foreach ($metadata as $meta) { |
| 93 | $class = 'PhpOffice\\PhpWord\\Metadata\\' . $meta; |
| 94 | $this->metadata[$meta] = new $class(); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Dynamic function call to reduce static dependency. |
| 100 | * |
| 101 | * @since 0.12.0 |
| 102 | * |
| 103 | * @param mixed $function |
| 104 | * @param mixed $args |
| 105 | * |
| 106 | * @return mixed |
| 107 | */ |
| 108 | public function __call($function, $args) |
| 109 | { |
| 110 | $function = strtolower($function); |
| 111 | |
| 112 | $getCollection = []; |
| 113 | $addCollection = []; |
| 114 | $addStyle = []; |
| 115 | |
| 116 | $collections = ['Bookmark', 'Title', 'Footnote', 'Endnote', 'Chart', 'Comment']; |
| 117 | foreach ($collections as $collection) { |
| 118 | $getCollection[] = strtolower("get{$collection}s"); |
| 119 | $addCollection[] = strtolower("add{$collection}"); |
| 120 | } |
| 121 | |
| 122 | $styles = ['Paragraph', 'Font', 'Table', 'Numbering', 'Link', 'Title']; |
| 123 | foreach ($styles as $style) { |
| 124 | $addStyle[] = strtolower("add{$style}Style"); |
| 125 | } |
| 126 | |
| 127 | // Run get collection method |
| 128 | if (in_array($function, $getCollection)) { |
| 129 | $key = ucfirst(str_replace('get', '', $function)); |
| 130 | |
| 131 | return $this->collections[$key]; |
| 132 | } |
| 133 | |
| 134 | // Run add collection item method |
| 135 | if (in_array($function, $addCollection)) { |
| 136 | $key = ucfirst(str_replace('add', '', $function) . 's'); |
| 137 | |
| 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 Metadata\DocInfo |
| 156 | */ |
| 157 | public function getDocInfo() |
| 158 | { |
| 159 | return $this->metadata['DocInfo']; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Get compatibility. |
| 164 | * |
| 165 | * @return 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 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 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|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 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 asian font name. |
| 262 | */ |
| 263 | public function getDefaultAsianFontName(): string |
| 264 | { |
| 265 | return Settings::getDefaultAsianFontName(); |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Set default asian font name. |
| 270 | * |
| 271 | * @param string $fontName |
| 272 | */ |
| 273 | public function setDefaultAsianFontName($fontName): void |
| 274 | { |
| 275 | Settings::setDefaultAsianFontName($fontName); |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Set default font color. |
| 280 | */ |
| 281 | public function setDefaultFontColor(string $fontColor): void |
| 282 | { |
| 283 | Settings::setDefaultFontColor($fontColor); |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Get default font color. |
| 288 | */ |
| 289 | public function getDefaultFontColor(): string |
| 290 | { |
| 291 | return Settings::getDefaultFontColor(); |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Get default font size. |
| 296 | * |
| 297 | * @return int |
| 298 | */ |
| 299 | public function getDefaultFontSize() |
| 300 | { |
| 301 | return Settings::getDefaultFontSize(); |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Set default font size. |
| 306 | * |
| 307 | * @param int $fontSize |
| 308 | */ |
| 309 | public function setDefaultFontSize($fontSize): void |
| 310 | { |
| 311 | Settings::setDefaultFontSize($fontSize); |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Set default paragraph style definition to styles.xml. |
| 316 | * |
| 317 | * @param array $styles Paragraph style definition |
| 318 | * |
| 319 | * @return Style\Paragraph |
| 320 | */ |
| 321 | public function setDefaultParagraphStyle($styles) |
| 322 | { |
| 323 | return Style::setDefaultParagraphStyle($styles); |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Save to file or download. |
| 328 | * |
| 329 | * All exceptions should already been handled by the writers |
| 330 | * |
| 331 | * @param string $filename |
| 332 | * @param string $format |
| 333 | * @param bool $download |
| 334 | * |
| 335 | * @return bool |
| 336 | */ |
| 337 | public function save($filename, $format = 'Word2007', $download = false) |
| 338 | { |
| 339 | $mime = [ |
| 340 | 'Word2007' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
| 341 | 'ODText' => 'application/vnd.oasis.opendocument.text', |
| 342 | 'RTF' => 'application/rtf', |
| 343 | 'HTML' => 'text/html', |
| 344 | 'PDF' => 'application/pdf', |
| 345 | ]; |
| 346 | |
| 347 | $writer = IOFactory::createWriter($this, $format); |
| 348 | |
| 349 | if ($download === true) { |
| 350 | header('Content-Description: File Transfer'); |
| 351 | header('Content-Disposition: attachment; filename="' . $filename . '"'); |
| 352 | header('Content-Type: ' . $mime[$format]); |
| 353 | header('Content-Transfer-Encoding: binary'); |
| 354 | header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); |
| 355 | header('Expires: 0'); |
| 356 | $filename = 'php://output'; // Change filename to force download |
| 357 | } |
| 358 | |
| 359 | $writer->save($filename); |
| 360 | |
| 361 | return true; |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Create new section. |
| 366 | * |
| 367 | * @deprecated 0.10.0 |
| 368 | * |
| 369 | * @param array $settings |
| 370 | * |
| 371 | * @return Section |
| 372 | * |
| 373 | * @codeCoverageIgnore |
| 374 | */ |
| 375 | public function createSection($settings = null) |
| 376 | { |
| 377 | return $this->addSection($settings); |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Get document properties object. |
| 382 | * |
| 383 | * @deprecated 0.12.0 |
| 384 | * |
| 385 | * @return Metadata\DocInfo |
| 386 | * |
| 387 | * @codeCoverageIgnore |
| 388 | */ |
| 389 | public function getDocumentProperties() |
| 390 | { |
| 391 | return $this->getDocInfo(); |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Set document properties object. |
| 396 | * |
| 397 | * @deprecated 0.12.0 |
| 398 | * |
| 399 | * @param Metadata\DocInfo $documentProperties |
| 400 | * |
| 401 | * @return self |
| 402 | * |
| 403 | * @codeCoverageIgnore |
| 404 | */ |
| 405 | public function setDocumentProperties($documentProperties) |
| 406 | { |
| 407 | $this->metadata['Document'] = $documentProperties; |
| 408 | |
| 409 | return $this; |
| 410 | } |
| 411 | } |