Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
97.32% |
109 / 112 |
|
66.67% |
4 / 6 |
CRAP | |
0.00% |
0 / 1 |
Document | |
97.32% |
109 / 112 |
|
66.67% |
4 / 6 |
36 | |
0.00% |
0 / 1 |
write | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
writeInfo | |
100.00% |
28 / 28 |
|
100.00% |
1 / 1 |
5 | |||
writeFormatting | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
4 | |||
writeTitlepg | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
5.07 | |||
writeSections | |
95.56% |
43 / 45 |
|
0.00% |
0 / 1 |
19 | |||
getDateValue | |
100.00% |
12 / 12 |
|
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\RTF\Part; |
20 | |
21 | use PhpOffice\PhpWord\Element\Footer; |
22 | use PhpOffice\PhpWord\Settings; |
23 | use PhpOffice\PhpWord\Writer\RTF\Element\Container; |
24 | use PhpOffice\PhpWord\Writer\RTF\Style\Section as SectionStyleWriter; |
25 | |
26 | /** |
27 | * RTF document part writer. |
28 | * |
29 | * @since 0.11.0 |
30 | * @see http://www.biblioscape.com/rtf15_spec.htm#Heading24 |
31 | */ |
32 | class Document extends AbstractPart |
33 | { |
34 | /** |
35 | * Write part. |
36 | * |
37 | * @return string |
38 | */ |
39 | public function write() |
40 | { |
41 | $content = ''; |
42 | |
43 | $content .= $this->writeInfo(); |
44 | $content .= $this->writeFormatting(); |
45 | $content .= $this->writeSections(); |
46 | |
47 | return $content; |
48 | } |
49 | |
50 | /** |
51 | * Write document information. |
52 | * |
53 | * @return string |
54 | */ |
55 | private function writeInfo() |
56 | { |
57 | $docProps = $this->getParentWriter()->getPhpWord()->getDocInfo(); |
58 | $properties = [ |
59 | 'title' => 'title', |
60 | 'subject' => 'subject', |
61 | 'category' => 'category', |
62 | 'keywords' => 'keywords', |
63 | 'comment' => 'description', |
64 | 'author' => 'creator', |
65 | 'operator' => 'lastModifiedBy', |
66 | 'creatim' => 'created', |
67 | 'revtim' => 'modified', |
68 | 'company' => 'company', |
69 | 'manager' => 'manager', |
70 | ]; |
71 | $dateFields = ['creatim', 'revtim']; |
72 | |
73 | $content = ''; |
74 | |
75 | $content .= '{'; |
76 | $content .= '\info'; |
77 | foreach ($properties as $property => $propertyMethod) { |
78 | $method = 'get' . $propertyMethod; |
79 | |
80 | $value = $docProps->$method(); |
81 | if (!in_array($property, $dateFields) && Settings::isOutputEscapingEnabled()) { |
82 | $value = $this->escaper->escape($value); |
83 | } |
84 | |
85 | $value = in_array($property, $dateFields) ? $this->getDateValue($value) : $value; |
86 | $content .= "{\\{$property} {$value}}"; |
87 | } |
88 | $content .= '}'; |
89 | $content .= PHP_EOL; |
90 | |
91 | return $content; |
92 | } |
93 | |
94 | /** |
95 | * Write document formatting properties. |
96 | * |
97 | * @return string |
98 | */ |
99 | private function writeFormatting() |
100 | { |
101 | $docSettings = $this->getParentWriter()->getPhpWord()->getSettings(); |
102 | // Applies a language to a text run (defaults to 1036 : French (France)) |
103 | $langId = $docSettings->getThemeFontLang() != null && $docSettings->getThemeFontLang()->getLangId() != null ? $docSettings->getThemeFontLang()->getLangId() : 1036; |
104 | |
105 | $content = ''; |
106 | |
107 | $content .= '\deftab720'; // Set the default tab size (720 twips) |
108 | $content .= '\viewkind1'; // Set the view mode of the document |
109 | |
110 | $content .= '\uc1'; // Set the numberof bytes that follows a unicode character |
111 | $content .= '\pard'; // Resets to default paragraph properties. |
112 | $content .= '\nowidctlpar'; // No widow/orphan control |
113 | $content .= '\lang' . $langId; |
114 | $content .= '\kerning1'; // Point size (in half-points) above which to kern character pairs |
115 | $content .= '\fs' . (Settings::getDefaultFontSize() * 2); // Set the font size in half-points |
116 | if ($docSettings->hasEvenAndOddHeaders()) { |
117 | $content .= '\\facingp'; |
118 | } |
119 | $content .= PHP_EOL; |
120 | |
121 | return $content; |
122 | } |
123 | |
124 | /** |
125 | * Write titlepg directive if any "f" headers or footers. |
126 | * |
127 | * @param \PhpOffice\PhpWord\Element\Section $section |
128 | * |
129 | * @return string |
130 | */ |
131 | private static function writeTitlepg($section) |
132 | { |
133 | foreach ($section->getHeaders() as $header) { |
134 | if ($header->getType() === Footer::FIRST) { |
135 | return '\\titlepg' . PHP_EOL; |
136 | } |
137 | } |
138 | foreach ($section->getFooters() as $header) { |
139 | if ($header->getType() === Footer::FIRST) { |
140 | return '\\titlepg' . PHP_EOL; |
141 | } |
142 | } |
143 | |
144 | return ''; |
145 | } |
146 | |
147 | /** |
148 | * Write sections. |
149 | * |
150 | * @return string |
151 | */ |
152 | private function writeSections() |
153 | { |
154 | $content = ''; |
155 | |
156 | $sections = $this->getParentWriter()->getPhpWord()->getSections(); |
157 | $evenOdd = $this->getParentWriter()->getPhpWord()->getSettings()->hasEvenAndOddHeaders(); |
158 | $sectOwed = false; |
159 | foreach ($sections as $section) { |
160 | if ($sectOwed) { |
161 | $content .= '\sect' . PHP_EOL; |
162 | } else { |
163 | $sectOwed = true; |
164 | } |
165 | $styleWriter = new SectionStyleWriter($section->getStyle()); |
166 | $styleWriter->setParentWriter($this->getParentWriter()); |
167 | $content .= $styleWriter->write(); |
168 | $content .= self::writeTitlepg($section); |
169 | |
170 | foreach ($section->getHeaders() as $header) { |
171 | $type = $header->getType(); |
172 | if ($evenOdd || $type !== Footer::EVEN) { |
173 | $content .= '{\\header'; |
174 | if ($type === Footer::FIRST) { |
175 | $content .= 'f'; |
176 | } elseif ($evenOdd) { |
177 | $content .= ($type === Footer::EVEN) ? 'l' : 'r'; |
178 | } |
179 | foreach ($header->getElements() as $element) { |
180 | $cl = get_class($element); |
181 | $cl2 = str_replace('Element', 'Writer\\RTF\\Element', $cl); |
182 | if (class_exists($cl2)) { |
183 | $elementWriter = new $cl2($this->getParentWriter(), $element); |
184 | $content .= $elementWriter->write(); |
185 | } |
186 | } |
187 | $content .= '}' . PHP_EOL; |
188 | } |
189 | } |
190 | foreach ($section->getFooters() as $footer) { |
191 | $type = $footer->getType(); |
192 | if ($evenOdd || $type !== Footer::EVEN) { |
193 | $content .= '{\\footer'; |
194 | if ($type === Footer::FIRST) { |
195 | $content .= 'f'; |
196 | } elseif ($evenOdd) { |
197 | $content .= ($type === Footer::EVEN) ? 'l' : 'r'; |
198 | } |
199 | foreach ($footer->getElements() as $element) { |
200 | $cl = get_class($element); |
201 | $cl2 = str_replace('Element', 'Writer\\RTF\\Element', $cl); |
202 | if (class_exists($cl2)) { |
203 | $elementWriter = new $cl2($this->getParentWriter(), $element); |
204 | $content .= $elementWriter->write(); |
205 | } |
206 | } |
207 | $content .= '}' . PHP_EOL; |
208 | } |
209 | } |
210 | |
211 | $elementWriter = new Container($this->getParentWriter(), $section); |
212 | $content .= $elementWriter->write(); |
213 | } |
214 | |
215 | return $content; |
216 | } |
217 | |
218 | /** |
219 | * Get date value. |
220 | * |
221 | * The format of date value is `\yr?\mo?\dy?\hr?\min?\sec?` |
222 | * |
223 | * @param int $value |
224 | * |
225 | * @return string |
226 | */ |
227 | private function getDateValue($value) |
228 | { |
229 | $dateParts = [ |
230 | 'Y' => 'yr', |
231 | 'm' => 'mo', |
232 | 'd' => 'dy', |
233 | 'H' => 'hr', |
234 | 'i' => 'min', |
235 | 's' => 'sec', |
236 | ]; |
237 | $result = ''; |
238 | foreach ($dateParts as $dateFormat => $controlWord) { |
239 | $result .= '\\' . $controlWord . date($dateFormat, $value); |
240 | } |
241 | |
242 | return $result; |
243 | } |
244 | } |