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