Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
94.12% |
16 / 17 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
Css | |
94.12% |
16 / 17 |
|
80.00% |
4 / 5 |
9.02 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
process | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
5.01 | |||
getStyles | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getStyle | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
sanitize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
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 | declare(strict_types=1); |
19 | |
20 | namespace PhpOffice\PhpWord\Shared; |
21 | |
22 | class Css |
23 | { |
24 | /** |
25 | * @var string |
26 | */ |
27 | private $cssContent; |
28 | |
29 | /** |
30 | * @var array<string, array<string, string>> |
31 | */ |
32 | private $styles = []; |
33 | |
34 | public function __construct(string $cssContent) |
35 | { |
36 | $this->cssContent = $cssContent; |
37 | } |
38 | |
39 | public function process(): void |
40 | { |
41 | $cssContent = str_replace(["\r", "\n"], '', $this->cssContent); |
42 | preg_match_all('/(.+?)\s?\{\s?(.+?)\s?\}/', $cssContent, $cssExtracted); |
43 | // Check if there are x selectors and x rules |
44 | if (count($cssExtracted[1]) != count($cssExtracted[2])) { |
45 | return; |
46 | } |
47 | |
48 | foreach ($cssExtracted[1] as $key => $selector) { |
49 | $rules = trim($cssExtracted[2][$key]); |
50 | $rules = explode(';', $rules); |
51 | foreach ($rules as $rule) { |
52 | if (empty($rule)) { |
53 | continue; |
54 | } |
55 | [$key, $value] = explode(':', trim($rule)); |
56 | $this->styles[$this->sanitize($selector)][$this->sanitize($key)] = $this->sanitize($value); |
57 | } |
58 | } |
59 | } |
60 | |
61 | public function getStyles(): array |
62 | { |
63 | return $this->styles; |
64 | } |
65 | |
66 | public function getStyle(string $selector): array |
67 | { |
68 | $selector = $this->sanitize($selector); |
69 | |
70 | return $this->styles[$selector] ?? []; |
71 | } |
72 | |
73 | private function sanitize(string $value): string |
74 | { |
75 | return addslashes(trim($value)); |
76 | } |
77 | } |