Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
45 / 45 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
Section | |
100.00% |
45 / 45 |
|
100.00% |
1 / 1 |
4 | |
100.00% |
1 / 1 |
write | |
100.00% |
45 / 45 |
|
100.00% |
1 / 1 |
4 |
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\Style; |
19 | |
20 | use PhpOffice\PhpWord\Style\Section as SectionStyle; |
21 | |
22 | /** |
23 | * Section style writer. |
24 | * |
25 | * @since 0.10.0 |
26 | */ |
27 | class Section extends AbstractStyle |
28 | { |
29 | /** |
30 | * Write style. |
31 | */ |
32 | public function write(): void |
33 | { |
34 | $style = $this->getStyle(); |
35 | if (!$style instanceof SectionStyle) { |
36 | return; |
37 | } |
38 | $xmlWriter = $this->getXmlWriter(); |
39 | |
40 | // Break type |
41 | $breakType = $style->getBreakType(); |
42 | $xmlWriter->writeElementIf(null !== $breakType, 'w:type', 'w:val', $breakType); |
43 | |
44 | // Page size & orientation |
45 | $xmlWriter->startElement('w:pgSz'); |
46 | $xmlWriter->writeAttribute('w:orient', $style->getOrientation()); |
47 | $xmlWriter->writeAttribute('w:w', $style->getPageSizeW()); |
48 | $xmlWriter->writeAttribute('w:h', $style->getPageSizeH()); |
49 | $xmlWriter->endElement(); // w:pgSz |
50 | |
51 | // Vertical alignment |
52 | $vAlign = $style->getVAlign(); |
53 | $xmlWriter->writeElementIf(null !== $vAlign, 'w:vAlign', 'w:val', $vAlign); |
54 | |
55 | // Margins |
56 | $margins = [ |
57 | 'w:top' => ['getMarginTop', SectionStyle::DEFAULT_MARGIN], |
58 | 'w:right' => ['getMarginRight', SectionStyle::DEFAULT_MARGIN], |
59 | 'w:bottom' => ['getMarginBottom', SectionStyle::DEFAULT_MARGIN], |
60 | 'w:left' => ['getMarginLeft', SectionStyle::DEFAULT_MARGIN], |
61 | 'w:header' => ['getHeaderHeight', SectionStyle::DEFAULT_HEADER_HEIGHT], |
62 | 'w:footer' => ['getFooterHeight', SectionStyle::DEFAULT_FOOTER_HEIGHT], |
63 | 'w:gutter' => ['getGutter', SectionStyle::DEFAULT_GUTTER], |
64 | ]; |
65 | $xmlWriter->startElement('w:pgMar'); |
66 | foreach ($margins as $attribute => $value) { |
67 | [$method, $default] = $value; |
68 | $xmlWriter->writeAttribute($attribute, $this->convertTwip($style->$method(), $default)); |
69 | } |
70 | $xmlWriter->endElement(); |
71 | |
72 | // Borders |
73 | if ($style->hasBorder()) { |
74 | $xmlWriter->startElement('w:pgBorders'); |
75 | $xmlWriter->writeAttribute('w:offsetFrom', 'page'); |
76 | |
77 | $styleWriter = new MarginBorder($xmlWriter); |
78 | $styleWriter->setSizes($style->getBorderSize()); |
79 | $styleWriter->setColors($style->getBorderColor()); |
80 | $styleWriter->setAttributes(['space' => '24']); |
81 | $styleWriter->write(); |
82 | |
83 | $xmlWriter->endElement(); |
84 | } |
85 | |
86 | // Columns |
87 | $colsSpace = $style->getColsSpace(); |
88 | $xmlWriter->startElement('w:cols'); |
89 | $xmlWriter->writeAttribute('w:num', $style->getColsNum()); |
90 | $xmlWriter->writeAttribute('w:space', $this->convertTwip($colsSpace, SectionStyle::DEFAULT_COLUMN_SPACING)); |
91 | $xmlWriter->endElement(); |
92 | |
93 | // Page numbering start |
94 | $pageNum = $style->getPageNumberingStart(); |
95 | $xmlWriter->writeElementIf(null !== $pageNum, 'w:pgNumType', 'w:start', $pageNum); |
96 | |
97 | // Line numbering |
98 | $styleWriter = new LineNumbering($xmlWriter, $style->getLineNumbering()); |
99 | $styleWriter->write(); |
100 | } |
101 | } |