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