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