Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.88% covered (success)
96.88%
31 / 32
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
Numbering
96.88% covered (success)
96.88%
31 / 32
0.00% covered (danger)
0.00%
0 / 1
3
0.00% covered (danger)
0.00%
0 / 1
 write
96.88% covered (success)
96.88%
31 / 32
0.00% covered (danger)
0.00%
0 / 1
3
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
18namespace PhpOffice\PhpWord\Writer\ODText\Style;
19
20use PhpOffice\PhpWord\Shared\Converter;
21use PhpOffice\PhpWord\Style\Numbering as StyleNumbering;
22
23/**
24 * Numbering style writer.
25 */
26class Numbering extends AbstractStyle
27{
28    /**
29     * Write style.
30     */
31    public function write(): void
32    {
33        /** @var StyleNumbering $style Type hint */
34        $style = $this->getStyle();
35        if (!$style instanceof StyleNumbering) {
36            return;
37        }
38        $xmlWriter = $this->getXmlWriter();
39
40        $xmlWriter->startElement('text:list-style');
41        $xmlWriter->writeAttribute('style:name', $style->getStyleName());
42
43        foreach ($style->getLevels() as $styleLevel) {
44            $numLevel = $styleLevel->getLevel() + 1;
45
46            // In Twips
47            $tabPos = $styleLevel->getTabPos();
48            // In Inches
49            $tabPos /= Converter::INCH_TO_TWIP;
50            // In Centimeters
51            $tabPos *= Converter::INCH_TO_CM;
52
53            // In Twips
54            $hanging = $styleLevel->getHanging();
55            // In Inches
56            $hanging /= Converter::INCH_TO_TWIP;
57            // In Centimeters
58            $hanging *= Converter::INCH_TO_CM;
59
60            $xmlWriter->startElement('text:list-level-style-bullet');
61            $xmlWriter->writeAttribute('text:level', $numLevel);
62            $xmlWriter->writeAttribute('text:style-name', $style->getStyleName() . '_' . $numLevel);
63            $xmlWriter->writeAttribute('text:bullet-char', $styleLevel->getText());
64
65            $xmlWriter->startElement('style:list-level-properties');
66            $xmlWriter->writeAttribute('text:list-level-position-and-space-mode', 'label-alignment');
67
68            $xmlWriter->startElement('style:list-level-label-alignment');
69            $xmlWriter->writeAttribute('text:label-followed-by', 'listtab');
70            $xmlWriter->writeAttribute('text:list-tab-stop-position', number_format($tabPos, 2, '.', '') . 'cm');
71            $xmlWriter->writeAttribute('fo:text-indent', '-' . number_format($hanging, 2, '.', '') . 'cm');
72            $xmlWriter->writeAttribute('fo:margin-left', number_format($tabPos, 2, '.', '') . 'cm');
73
74            $xmlWriter->endElement(); // style:list-level-label-alignment
75          $xmlWriter->endElement(); // style:list-level-properties
76
77          $xmlWriter->startElement('style:text-properties');
78            $xmlWriter->writeAttribute('style:font-name', $styleLevel->getFont());
79            $xmlWriter->endElement(); // style:text-properties
80
81            $xmlWriter->endElement(); // text:list-level-style-bullet
82        }
83
84        $xmlWriter->endElement(); // text:list-style
85    }
86}