Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractStyle
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
7 / 7
15
100.00% covered (success)
100.00%
1 / 1
 write
n/a
0 / 0
n/a
0 / 0
0
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getXmlWriter
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getStyle
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 convertTwip
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
3
 writeChildStyle
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 writeOnOf
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 assembleStyle
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
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
18namespace PhpOffice\PhpWord\Writer\Word2007\Style;
19
20use PhpOffice\PhpWord\Settings;
21use PhpOffice\PhpWord\Shared\XMLWriter;
22
23/**
24 * Style writer.
25 *
26 * @since 0.10.0
27 */
28abstract class AbstractStyle
29{
30    /**
31     * XML writer.
32     *
33     * @var \PhpOffice\PhpWord\Shared\XMLWriter
34     */
35    private $xmlWriter;
36
37    /**
38     * Style; set protected for a while.
39     *
40     * @var \PhpOffice\PhpWord\Style\AbstractStyle|string
41     */
42    protected $style;
43
44    /**
45     * Write style.
46     */
47    abstract public function write();
48
49    /**
50     * Create new instance.
51     *
52     * @param \PhpOffice\PhpWord\Style\AbstractStyle|string $style
53     */
54    public function __construct(XMLWriter $xmlWriter, $style = null)
55    {
56        $this->xmlWriter = $xmlWriter;
57        $this->style = $style;
58    }
59
60    /**
61     * Get XML Writer.
62     *
63     * @return \PhpOffice\PhpWord\Shared\XMLWriter
64     */
65    protected function getXmlWriter()
66    {
67        return $this->xmlWriter;
68    }
69
70    /**
71     * Get Style.
72     *
73     * @return \PhpOffice\PhpWord\Style\AbstractStyle|string
74     */
75    protected function getStyle()
76    {
77        return $this->style;
78    }
79
80    /**
81     * Convert twip value.
82     *
83     * @param float|int $value
84     * @param int $default (int|float)
85     *
86     * @return float|int
87     */
88    protected function convertTwip($value, $default = 0)
89    {
90        $factors = [
91            Settings::UNIT_CM => 567,
92            Settings::UNIT_MM => 56.7,
93            Settings::UNIT_INCH => 1440,
94            Settings::UNIT_POINT => 20,
95            Settings::UNIT_PICA => 240,
96        ];
97        $unit = Settings::getMeasurementUnit();
98        $factor = 1;
99        if (array_key_exists($unit, $factors) && $value != $default) {
100            $factor = $factors[$unit];
101        }
102
103        return $value * $factor;
104    }
105
106    /**
107     * Write child style.
108     *
109     * @param string $name
110     * @param mixed $value
111     */
112    protected function writeChildStyle(XMLWriter $xmlWriter, $name, $value): void
113    {
114        if ($value !== null) {
115            $class = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Style\\' . $name;
116
117            /** @var \PhpOffice\PhpWord\Writer\Word2007\Style\AbstractStyle $writer */
118            $writer = new $class($xmlWriter, $value);
119            $writer->write();
120        }
121    }
122
123    /**
124     * Writes boolean as 0 or 1.
125     *
126     * @param bool $value
127     *
128     * @return null|string
129     */
130    protected function writeOnOf($value = null)
131    {
132        if ($value === null) {
133            return null;
134        }
135
136        return $value ? '1' : '0';
137    }
138
139    /**
140     * Assemble style array into style string.
141     *
142     * @param array $styles
143     *
144     * @return string
145     */
146    protected function assembleStyle($styles = [])
147    {
148        $style = '';
149        foreach ($styles as $key => $value) {
150            if (null !== $value && $value != '') {
151                $style .= "{$key}:{$value}";
152            }
153        }
154
155        return trim($style);
156    }
157}