Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
45 / 45 |
|
100.00% |
14 / 14 |
CRAP | |
100.00% |
1 / 1 |
AbstractElement | |
100.00% |
45 / 45 |
|
100.00% |
14 / 14 |
26 | |
100.00% |
1 / 1 |
write | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
getXmlWriter | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getElement | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
startElementP | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
endElementP | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
writeCommentRangeStart | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
writeCommentRangeEnd | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
6 | |||
writeParagraphStyle | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
writeFontStyle | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
writeTextStyle | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
getText | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
writeText | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
setPart | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getPart | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
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\Writer\Word2007\Element; |
20 | |
21 | use PhpOffice\PhpWord\Element\AbstractElement as Element; |
22 | use PhpOffice\PhpWord\Settings; |
23 | use PhpOffice\PhpWord\Shared\Text as SharedText; |
24 | use PhpOffice\PhpWord\Shared\XMLWriter; |
25 | use PhpOffice\PhpWord\Writer\Word2007\Part\AbstractPart; |
26 | |
27 | /** |
28 | * Abstract element writer. |
29 | * |
30 | * @since 0.11.0 |
31 | */ |
32 | abstract class AbstractElement |
33 | { |
34 | /** |
35 | * XML writer. |
36 | * |
37 | * @var XMLWriter |
38 | */ |
39 | private $xmlWriter; |
40 | |
41 | /** |
42 | * Element. |
43 | * |
44 | * @var Element |
45 | */ |
46 | private $element; |
47 | |
48 | /** |
49 | * Without paragraph. |
50 | * |
51 | * @var bool |
52 | */ |
53 | protected $withoutP = false; |
54 | |
55 | /** |
56 | * @var null|AbstractPart |
57 | */ |
58 | protected $part; |
59 | |
60 | /** |
61 | * Write element. |
62 | */ |
63 | abstract public function write(); |
64 | |
65 | /** |
66 | * Create new instance. |
67 | */ |
68 | public function __construct(XMLWriter $xmlWriter, Element $element, bool $withoutP = false) |
69 | { |
70 | $this->xmlWriter = $xmlWriter; |
71 | $this->element = $element; |
72 | $this->withoutP = $withoutP; |
73 | } |
74 | |
75 | /** |
76 | * Get XML Writer. |
77 | * |
78 | * @return XMLWriter |
79 | */ |
80 | protected function getXmlWriter() |
81 | { |
82 | return $this->xmlWriter; |
83 | } |
84 | |
85 | /** |
86 | * Get element. |
87 | * |
88 | * @return Element |
89 | */ |
90 | protected function getElement() |
91 | { |
92 | return $this->element; |
93 | } |
94 | |
95 | /** |
96 | * Start w:p DOM element. |
97 | * |
98 | * @uses \PhpOffice\PhpWord\Writer\Word2007\Element\PageBreak::write() |
99 | */ |
100 | protected function startElementP(): void |
101 | { |
102 | if (!$this->withoutP) { |
103 | $this->xmlWriter->startElement('w:p'); |
104 | // Paragraph style |
105 | if (method_exists($this->element, 'getParagraphStyle')) { |
106 | $this->writeParagraphStyle(); |
107 | } |
108 | } |
109 | $this->writeCommentRangeStart(); |
110 | } |
111 | |
112 | /** |
113 | * End w:p DOM element. |
114 | */ |
115 | protected function endElementP(): void |
116 | { |
117 | $this->writeCommentRangeEnd(); |
118 | if (!$this->withoutP) { |
119 | $this->xmlWriter->endElement(); // w:p |
120 | } |
121 | } |
122 | |
123 | /** |
124 | * Writes the w:commentRangeStart DOM element. |
125 | */ |
126 | protected function writeCommentRangeStart(): void |
127 | { |
128 | if ($this->element->getCommentsRangeStart() != null) { |
129 | foreach ($this->element->getCommentsRangeStart()->getItems() as $comment) { |
130 | $this->xmlWriter->writeElementBlock('w:commentRangeStart', ['w:id' => $comment->getElementId()]); |
131 | } |
132 | } |
133 | } |
134 | |
135 | /** |
136 | * Writes the w:commentRangeEnd DOM element. |
137 | */ |
138 | protected function writeCommentRangeEnd(): void |
139 | { |
140 | if ($this->element->getCommentsRangeEnd() != null) { |
141 | foreach ($this->element->getCommentsRangeEnd()->getItems() as $comment) { |
142 | $this->xmlWriter->writeElementBlock('w:commentRangeEnd', ['w:id' => $comment->getElementId()]); |
143 | $this->xmlWriter->startElement('w:r'); |
144 | $this->xmlWriter->writeElementBlock('w:commentReference', ['w:id' => $comment->getElementId()]); |
145 | $this->xmlWriter->endElement(); |
146 | } |
147 | } |
148 | if ($this->element->getCommentsRangeStart() != null) { |
149 | foreach ($this->element->getCommentsRangeStart()->getItems() as $comment) { |
150 | if ($comment->getEndElement() == null) { |
151 | $this->xmlWriter->writeElementBlock('w:commentRangeEnd', ['w:id' => $comment->getElementId()]); |
152 | $this->xmlWriter->startElement('w:r'); |
153 | $this->xmlWriter->writeElementBlock('w:commentReference', ['w:id' => $comment->getElementId()]); |
154 | $this->xmlWriter->endElement(); |
155 | } |
156 | } |
157 | } |
158 | } |
159 | |
160 | /** |
161 | * Write ending. |
162 | */ |
163 | protected function writeParagraphStyle(): void |
164 | { |
165 | $this->writeTextStyle('Paragraph'); |
166 | } |
167 | |
168 | /** |
169 | * Write ending. |
170 | */ |
171 | protected function writeFontStyle(): void |
172 | { |
173 | $this->writeTextStyle('Font'); |
174 | } |
175 | |
176 | /** |
177 | * Write text style. |
178 | * |
179 | * @param string $styleType Font|Paragraph |
180 | */ |
181 | private function writeTextStyle($styleType): void |
182 | { |
183 | $method = "get{$styleType}Style"; |
184 | $class = "PhpOffice\\PhpWord\\Writer\\Word2007\\Style\\{$styleType}"; |
185 | $styleObject = $this->element->$method(); |
186 | |
187 | /** @var \PhpOffice\PhpWord\Writer\Word2007\Style\AbstractStyle $styleWriter Type Hint */ |
188 | $styleWriter = new $class($this->xmlWriter, $styleObject); |
189 | if (method_exists($styleWriter, 'setIsInline')) { |
190 | $styleWriter->setIsInline(true); |
191 | } |
192 | |
193 | $styleWriter->write(); |
194 | } |
195 | |
196 | /** |
197 | * Convert text to valid format. |
198 | * |
199 | * @param string $text |
200 | * |
201 | * @return string |
202 | */ |
203 | protected function getText($text) |
204 | { |
205 | return SharedText::controlCharacterPHP2OOXML($text); |
206 | } |
207 | |
208 | /** |
209 | * Write an XML text, this will call text() or writeRaw() depending on the value of Settings::isOutputEscapingEnabled(). |
210 | * |
211 | * @param string $content The text string to write |
212 | * |
213 | * @return bool Returns true on success or false on failure |
214 | */ |
215 | protected function writeText($content) |
216 | { |
217 | if (Settings::isOutputEscapingEnabled()) { |
218 | return $this->getXmlWriter()->text($content); |
219 | } |
220 | |
221 | return $this->getXmlWriter()->writeRaw($content); |
222 | } |
223 | |
224 | public function setPart(?AbstractPart $part): self |
225 | { |
226 | $this->part = $part; |
227 | |
228 | return $this; |
229 | } |
230 | |
231 | public function getPart(): ?AbstractPart |
232 | { |
233 | return $this->part; |
234 | } |
235 | } |