Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
60 / 60 |
|
100.00% |
17 / 17 |
CRAP | |
100.00% |
1 / 1 |
AbstractStyle | |
100.00% |
60 / 60 |
|
100.00% |
17 / 17 |
41 | |
100.00% |
1 / 1 |
getStyleName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setStyleName | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getIndex | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setIndex | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
isAuto | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setAuto | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getChildStyleValue | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
setStyleValue | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
4 | |||
setStyleByArray | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
setNonEmptyVal | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
setBoolVal | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
setNumericVal | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
setIntVal | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
setFloatVal | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
setEnumVal | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
7 | |||
setObjectVal | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
setPairedVal | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 |
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\Style; |
19 | |
20 | use InvalidArgumentException; |
21 | use PhpOffice\PhpWord\Shared\Text; |
22 | |
23 | /** |
24 | * Abstract style class. |
25 | * |
26 | * @since 0.10.0 |
27 | */ |
28 | abstract class AbstractStyle |
29 | { |
30 | /** |
31 | * Style name. |
32 | * |
33 | * @var ?string |
34 | */ |
35 | protected $styleName; |
36 | |
37 | /** |
38 | * Index number in Style collection for named style. |
39 | * |
40 | * This number starts from one and defined in Style::setStyleValues() |
41 | * |
42 | * @var null|int |
43 | */ |
44 | protected $index; |
45 | |
46 | /** |
47 | * Aliases. |
48 | * |
49 | * @var array |
50 | */ |
51 | protected $aliases = []; |
52 | |
53 | /** |
54 | * Is this an automatic style? (Used primarily in OpenDocument driver). |
55 | * |
56 | * @var bool |
57 | * |
58 | * @since 0.11.0 |
59 | */ |
60 | private $isAuto = false; |
61 | |
62 | /** |
63 | * Get style name. |
64 | * |
65 | * @return ?string |
66 | */ |
67 | public function getStyleName() |
68 | { |
69 | return $this->styleName; |
70 | } |
71 | |
72 | /** |
73 | * Set style name. |
74 | * |
75 | * @param string $value |
76 | * |
77 | * @return self |
78 | */ |
79 | public function setStyleName($value) |
80 | { |
81 | $this->styleName = $value; |
82 | |
83 | return $this; |
84 | } |
85 | |
86 | /** |
87 | * Get index number. |
88 | * |
89 | * @return null|int |
90 | */ |
91 | public function getIndex() |
92 | { |
93 | return $this->index; |
94 | } |
95 | |
96 | /** |
97 | * Set index number. |
98 | * |
99 | * @param null|int $value |
100 | * |
101 | * @return self |
102 | */ |
103 | public function setIndex($value = null) |
104 | { |
105 | $this->index = $this->setIntVal($value, $this->index); |
106 | |
107 | return $this; |
108 | } |
109 | |
110 | /** |
111 | * Get is automatic style flag. |
112 | * |
113 | * @return bool |
114 | */ |
115 | public function isAuto() |
116 | { |
117 | return $this->isAuto; |
118 | } |
119 | |
120 | /** |
121 | * Set is automatic style flag. |
122 | * |
123 | * @param bool $value |
124 | * |
125 | * @return self |
126 | */ |
127 | public function setAuto($value = true) |
128 | { |
129 | $this->isAuto = $this->setBoolVal($value, $this->isAuto); |
130 | |
131 | return $this; |
132 | } |
133 | |
134 | /** |
135 | * Return style value of child style object, e.g. `left` from `Indentation` child style of `Paragraph`. |
136 | * |
137 | * @param \PhpOffice\PhpWord\Style\AbstractStyle $substyleObject |
138 | * @param string $substyleProperty |
139 | * |
140 | * @return mixed |
141 | * |
142 | * @since 0.12.0 |
143 | */ |
144 | public function getChildStyleValue($substyleObject, $substyleProperty) |
145 | { |
146 | if ($substyleObject !== null) { |
147 | $method = "get{$substyleProperty}"; |
148 | |
149 | return $substyleObject->$method(); |
150 | } |
151 | |
152 | return null; |
153 | } |
154 | |
155 | /** |
156 | * Set style value template method. |
157 | * |
158 | * Some child classes have their own specific overrides. |
159 | * Backward compability check for versions < 0.10.0 which use underscore |
160 | * prefix for their private properties. |
161 | * Check if the set method is exists. Throws an exception? |
162 | * |
163 | * @param string $key |
164 | * @param array|int|string $value |
165 | * |
166 | * @return self |
167 | */ |
168 | public function setStyleValue($key, $value) |
169 | { |
170 | if (isset($this->aliases[$key])) { |
171 | $key = $this->aliases[$key]; |
172 | } |
173 | |
174 | if ($key === 'align') { |
175 | $key = 'alignment'; |
176 | } |
177 | |
178 | $method = 'set' . Text::removeUnderscorePrefix($key); |
179 | if (method_exists($this, $method)) { |
180 | $this->$method($value); |
181 | } |
182 | |
183 | return $this; |
184 | } |
185 | |
186 | /** |
187 | * Set style by using associative array. |
188 | * |
189 | * @param array $values |
190 | * |
191 | * @return self |
192 | */ |
193 | public function setStyleByArray($values = []) |
194 | { |
195 | foreach ($values as $key => $value) { |
196 | $this->setStyleValue($key, $value); |
197 | } |
198 | |
199 | return $this; |
200 | } |
201 | |
202 | /** |
203 | * Set default for null and empty value. |
204 | * |
205 | * @param ?string $value |
206 | * @param string $default |
207 | * |
208 | * @return string |
209 | */ |
210 | protected function setNonEmptyVal($value, $default) |
211 | { |
212 | if ($value === null || $value == '') { |
213 | $value = $default; |
214 | } |
215 | |
216 | return $value; |
217 | } |
218 | |
219 | /** |
220 | * Set bool value. |
221 | * |
222 | * @param bool $value |
223 | * @param bool $default |
224 | * |
225 | * @return bool |
226 | */ |
227 | protected function setBoolVal($value, $default) |
228 | { |
229 | if (!is_bool($value)) { |
230 | $value = $default; |
231 | } |
232 | |
233 | return $value; |
234 | } |
235 | |
236 | /** |
237 | * Set numeric value. |
238 | * |
239 | * @param mixed $value |
240 | * @param null|float|int $default |
241 | * |
242 | * @return null|float|int |
243 | */ |
244 | protected function setNumericVal($value, $default = null) |
245 | { |
246 | if (!is_numeric($value)) { |
247 | $value = $default; |
248 | } |
249 | |
250 | return $value; |
251 | } |
252 | |
253 | /** |
254 | * Set integer value: Convert string that contains only numeric into integer. |
255 | * |
256 | * @param null|float|int|string $value |
257 | * @param null|int $default |
258 | * |
259 | * @return null|int |
260 | */ |
261 | protected function setIntVal($value, $default = null) |
262 | { |
263 | if (is_string($value) && (preg_match('/[^\d]/', $value) == 0)) { |
264 | $value = (int) $value; |
265 | } |
266 | if (!is_numeric($value)) { |
267 | $value = $default; |
268 | } else { |
269 | $value = (int) $value; |
270 | } |
271 | |
272 | return $value; |
273 | } |
274 | |
275 | /** |
276 | * Set float value: Convert string that contains only numeric into float. |
277 | * |
278 | * @param mixed $value |
279 | * @param null|float $default |
280 | * |
281 | * @return null|float |
282 | */ |
283 | protected function setFloatVal($value, $default = null) |
284 | { |
285 | if (is_string($value) && (preg_match('/[^\d\.\,]/', $value) == 0)) { |
286 | $value = (float) $value; |
287 | } |
288 | if (!is_numeric($value)) { |
289 | $value = $default; |
290 | } |
291 | |
292 | return $value; |
293 | } |
294 | |
295 | /** |
296 | * Set enum value. |
297 | * |
298 | * @param mixed $value |
299 | * @param array $enum |
300 | * @param mixed $default |
301 | * |
302 | * @return mixed |
303 | */ |
304 | protected function setEnumVal($value = null, $enum = [], $default = null) |
305 | { |
306 | if ($value != null && trim($value) != '' && !empty($enum) && !in_array($value, $enum)) { |
307 | throw new InvalidArgumentException("Invalid style value: {$value} Options:" . implode(',', $enum)); |
308 | } elseif ($value === null || trim($value) == '') { |
309 | $value = $default; |
310 | } |
311 | |
312 | return $value; |
313 | } |
314 | |
315 | /** |
316 | * Set object value. |
317 | * |
318 | * @param mixed $value |
319 | * @param string $styleName |
320 | * @param mixed &$style |
321 | * |
322 | * @return mixed |
323 | */ |
324 | protected function setObjectVal($value, $styleName, &$style) |
325 | { |
326 | $styleClass = substr(static::class, 0, strrpos(static::class, '\\')) . '\\' . $styleName; |
327 | if (is_array($value)) { |
328 | /** @var \PhpOffice\PhpWord\Style\AbstractStyle $style Type hint */ |
329 | if (!$style instanceof $styleClass) { |
330 | $style = new $styleClass(); |
331 | } |
332 | $style->setStyleByArray($value); |
333 | } else { |
334 | $style = $value; |
335 | } |
336 | |
337 | return $style; |
338 | } |
339 | |
340 | /** |
341 | * Set $property value and set $pairProperty = false when $value = true. |
342 | * |
343 | * @param bool &$property |
344 | * @param bool &$pairProperty |
345 | * @param bool $value |
346 | * |
347 | * @return self |
348 | */ |
349 | protected function setPairedVal(&$property, &$pairProperty, $value) |
350 | { |
351 | $property = $this->setBoolVal($value, $property); |
352 | if ($value === true) { |
353 | $pairProperty = false; |
354 | } |
355 | |
356 | return $this; |
357 | } |
358 | } |