Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
46 / 46 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
AbstractElement | |
100.00% |
46 / 46 |
|
100.00% |
6 / 6 |
22 | |
100.00% |
1 / 1 |
write | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
getStyles | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
8 | |||
writeOpening | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
writeText | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
writeClosing | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
writeFontStyle | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
6 |
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\RTF\Element; |
19 | |
20 | use PhpOffice\PhpWord\Element\AbstractElement as Element; |
21 | use PhpOffice\PhpWord\Escaper\Rtf; |
22 | use PhpOffice\PhpWord\Settings; |
23 | use PhpOffice\PhpWord\Shared\Text as SharedText; |
24 | use PhpOffice\PhpWord\Style; |
25 | use PhpOffice\PhpWord\Style\Font as FontStyle; |
26 | use PhpOffice\PhpWord\Style\Paragraph as ParagraphStyle; |
27 | use PhpOffice\PhpWord\Writer\RTF as WriterRTF; |
28 | use PhpOffice\PhpWord\Writer\RTF\Style\Font as FontStyleWriter; |
29 | use PhpOffice\PhpWord\Writer\RTF\Style\Paragraph as ParagraphStyleWriter; |
30 | |
31 | /** |
32 | * Abstract RTF element writer. |
33 | * |
34 | * @since 0.11.0 |
35 | */ |
36 | abstract class AbstractElement |
37 | { |
38 | /** |
39 | * Parent writer. |
40 | * |
41 | * @var WriterRTF |
42 | */ |
43 | protected $parentWriter; |
44 | |
45 | /** |
46 | * Element. |
47 | * |
48 | * @var \PhpOffice\PhpWord\Element\AbstractElement |
49 | */ |
50 | protected $element; |
51 | |
52 | /** |
53 | * Without paragraph. |
54 | * |
55 | * @var bool |
56 | */ |
57 | protected $withoutP = false; |
58 | |
59 | /** |
60 | * Write element. |
61 | * |
62 | * @return string |
63 | */ |
64 | abstract public function write(); |
65 | |
66 | /** |
67 | * Font style. |
68 | * |
69 | * @var \PhpOffice\PhpWord\Style\Font |
70 | */ |
71 | protected $fontStyle; |
72 | |
73 | /** |
74 | * Paragraph style. |
75 | * |
76 | * @var \PhpOffice\PhpWord\Style\Paragraph |
77 | */ |
78 | protected $paragraphStyle; |
79 | |
80 | /** |
81 | * @var \PhpOffice\PhpWord\Escaper\EscaperInterface |
82 | */ |
83 | protected $escaper; |
84 | |
85 | public function __construct(WriterRTF $parentWriter, Element $element, bool $withoutP = false) |
86 | { |
87 | $this->parentWriter = $parentWriter; |
88 | $this->element = $element; |
89 | $this->withoutP = $withoutP; |
90 | $this->escaper = new Rtf(); |
91 | } |
92 | |
93 | /** |
94 | * Get font and paragraph styles. |
95 | */ |
96 | protected function getStyles(): void |
97 | { |
98 | /** @var \PhpOffice\PhpWord\Writer\RTF $parentWriter Type hint */ |
99 | $parentWriter = $this->parentWriter; |
100 | |
101 | /** @var \PhpOffice\PhpWord\Element\Text $element Type hint */ |
102 | $element = $this->element; |
103 | |
104 | // Font style |
105 | if (method_exists($element, 'getFontStyle')) { |
106 | $this->fontStyle = $element->getFontStyle(); |
107 | if (is_string($this->fontStyle)) { |
108 | $this->fontStyle = Style::getStyle($this->fontStyle); |
109 | } |
110 | } |
111 | |
112 | // Paragraph style |
113 | if (method_exists($element, 'getParagraphStyle')) { |
114 | $this->paragraphStyle = $element->getParagraphStyle(); |
115 | if (is_string($this->paragraphStyle)) { |
116 | $this->paragraphStyle = Style::getStyle($this->paragraphStyle); |
117 | } |
118 | |
119 | if ($this->paragraphStyle !== null && !$this->withoutP) { |
120 | if ($parentWriter->getLastParagraphStyle() != $element->getParagraphStyle()) { |
121 | $parentWriter->setLastParagraphStyle($element->getParagraphStyle()); |
122 | } else { |
123 | $parentWriter->setLastParagraphStyle(); |
124 | $this->paragraphStyle = null; |
125 | } |
126 | } else { |
127 | $parentWriter->setLastParagraphStyle(); |
128 | $this->paragraphStyle = null; |
129 | } |
130 | } |
131 | } |
132 | |
133 | /** |
134 | * Write opening. |
135 | * |
136 | * @return string |
137 | */ |
138 | protected function writeOpening() |
139 | { |
140 | if ($this->withoutP || !$this->paragraphStyle instanceof ParagraphStyle) { |
141 | return ''; |
142 | } |
143 | |
144 | $styleWriter = new ParagraphStyleWriter($this->paragraphStyle); |
145 | $styleWriter->setNestedLevel($this->element->getNestedLevel()); |
146 | |
147 | return $styleWriter->write(); |
148 | } |
149 | |
150 | /** |
151 | * Write text. |
152 | * |
153 | * @param string $text |
154 | * |
155 | * @return string |
156 | */ |
157 | protected function writeText($text) |
158 | { |
159 | if (Settings::isOutputEscapingEnabled()) { |
160 | return $this->escaper->escape($text); |
161 | } |
162 | |
163 | return SharedText::toUnicode($text); // todo: replace with `return $text;` later. |
164 | } |
165 | |
166 | /** |
167 | * Write closing. |
168 | * |
169 | * @return string |
170 | */ |
171 | protected function writeClosing() |
172 | { |
173 | if ($this->withoutP) { |
174 | return ''; |
175 | } |
176 | |
177 | return '\par' . PHP_EOL; |
178 | } |
179 | |
180 | /** |
181 | * Write font style. |
182 | * |
183 | * @return string |
184 | */ |
185 | protected function writeFontStyle() |
186 | { |
187 | if (!$this->fontStyle instanceof FontStyle) { |
188 | return ''; |
189 | } |
190 | |
191 | /** @var \PhpOffice\PhpWord\Writer\RTF $parentWriter Type hint */ |
192 | $parentWriter = $this->parentWriter; |
193 | |
194 | // Create style writer and set color/name index |
195 | $styleWriter = new FontStyleWriter($this->fontStyle); |
196 | if ($this->fontStyle->getColor() != null) { |
197 | $colorIndex = array_search($this->fontStyle->getColor(), $parentWriter->getColorTable()); |
198 | if ($colorIndex !== false) { |
199 | $styleWriter->setColorIndex($colorIndex + 1); |
200 | } |
201 | } |
202 | if ($this->fontStyle->getName() != null) { |
203 | $fontIndex = array_search($this->fontStyle->getName(), $parentWriter->getFontTable()); |
204 | if ($fontIndex !== false) { |
205 | $styleWriter->setNameIndex($fontIndex); |
206 | } |
207 | } |
208 | |
209 | // Write style |
210 | $content = $styleWriter->write(); |
211 | |
212 | return $content; |
213 | } |
214 | } |