Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
22 / 22 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
Container | |
100.00% |
22 / 22 |
|
100.00% |
2 / 2 |
8 | |
100.00% |
1 / 1 |
write | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
6 | |||
writeElement | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 |
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\Writer\Word2007\Element; |
20 | |
21 | use PhpOffice\PhpWord\Element\AbstractContainer as ContainerElement; |
22 | use PhpOffice\PhpWord\Element\AbstractElement as Element; |
23 | use PhpOffice\PhpWord\Element\TextBreak as TextBreakElement; |
24 | use PhpOffice\PhpWord\Shared\XMLWriter; |
25 | |
26 | /** |
27 | * Container element writer (section, textrun, header, footnote, cell, etc.). |
28 | * |
29 | * @since 0.11.0 |
30 | */ |
31 | class Container extends AbstractElement |
32 | { |
33 | /** |
34 | * Namespace; Can't use __NAMESPACE__ in inherited class (ODText). |
35 | * |
36 | * @var string |
37 | */ |
38 | protected $namespace = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Element'; |
39 | |
40 | /** |
41 | * @var array<string> |
42 | */ |
43 | protected $containerWithoutP = ['TextRun', 'Footnote', 'Endnote', 'ListItemRun']; |
44 | |
45 | /** |
46 | * Write element. |
47 | */ |
48 | public function write(): void |
49 | { |
50 | $container = $this->getElement(); |
51 | if (!$container instanceof ContainerElement) { |
52 | return; |
53 | } |
54 | $containerClass = substr(get_class($container), strrpos(get_class($container), '\\') + 1); |
55 | $withoutP = in_array($containerClass, $this->containerWithoutP); |
56 | $xmlWriter = $this->getXmlWriter(); |
57 | |
58 | // Loop through elements |
59 | $elements = $container->getElements(); |
60 | $elementClass = ''; |
61 | foreach ($elements as $element) { |
62 | $elementClass = $this->writeElement($xmlWriter, $element, $withoutP); |
63 | } |
64 | |
65 | // Special case for Cell: They have to contain a w:p element at the end. |
66 | // The $elementClass contains the last element name. If it's empty string |
67 | // or Table, the last element is not w:p |
68 | $writeLastTextBreak = ($containerClass == 'Cell') && ($elementClass == '' || $elementClass == 'Table'); |
69 | if ($writeLastTextBreak) { |
70 | $writerClass = $this->namespace . '\\TextBreak'; |
71 | /** @var AbstractElement $writer Type hint */ |
72 | $writer = new $writerClass($xmlWriter, new TextBreakElement(), $withoutP); |
73 | $writer->write(); |
74 | } |
75 | } |
76 | |
77 | /** |
78 | * Write individual element. |
79 | */ |
80 | private function writeElement(XMLWriter $xmlWriter, Element $element, bool $withoutP): string |
81 | { |
82 | $elementClass = substr(get_class($element), strrpos(get_class($element), '\\') + 1); |
83 | $writerClass = $this->namespace . '\\' . $elementClass; |
84 | |
85 | if (class_exists($writerClass)) { |
86 | /** @var AbstractElement $writer Type hint */ |
87 | $writer = new $writerClass($xmlWriter, $element, $withoutP); |
88 | $writer->setPart($this->getPart()); |
89 | $writer->write(); |
90 | } |
91 | |
92 | return $elementClass; |
93 | } |
94 | } |