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