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