Completed
Pull Request — master (#192)
by
unknown
01:50
created

Processor   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 1
dl 0
loc 79
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getRules() 0 8 1
A getMediaQueries() 0 7 2
A getCssFromStyleTags() 0 15 3
A doCleanup() 0 16 1
1
<?php
2
3
namespace TijsVerkoyen\CssToInlineStyles\Css;
4
5
use TijsVerkoyen\CssToInlineStyles\Css\Rule\Processor as RuleProcessor;
6
use TijsVerkoyen\CssToInlineStyles\Css\Rule\Rule;
7
8
class Processor
9
{
10
    /**
11
     * Get the rules from a given CSS-string
12
     *
13
     * @param string $css
14
     * @param Rule[] $existingRules
15
     *
16
     * @return Rule[]
17
     */
18
    public function getRules($css, $existingRules = array())
19
    {
20
        $css = $this->doCleanup($css);
21
        $rulesProcessor = new RuleProcessor();
22
        $rules = $rulesProcessor->splitIntoSeparateRules($css);
23
24
        return $rulesProcessor->convertArrayToObjects($rules, $existingRules);
25
    }
26
27
    /**
28
     * Get all media queries from the given css string.
29
     *
30
     * @param $css
31
     * @param array $existingMediaQueries
32
     * @return array
33
     */
34
    public function getMediaQueries(string $css, array $existingMediaQueries = array()) {
35
        $matches = [];
36
        if ( preg_match_all( '/@media [^{]*+{([^{}]++|{[^{}]*+})*+}/', $css, $matches ) ) {
37
            return array_merge($existingMediaQueries, $matches[0]);
38
        }
39
        return $existingMediaQueries;
40
    }
41
42
    /**
43
     * Get the CSS from the style-tags in the given HTML-string
44
     *
45
     * @param string $html
46
     *
47
     * @return string
48
     */
49
    public function getCssFromStyleTags($html)
50
    {
51
        $css = '';
52
        $matches = array();
53
        $htmlNoComments = preg_replace('|<!--.*?-->|s', '', $html);
54
        preg_match_all('|<style(?:\s.*)?>(.*)</style>|isU', $htmlNoComments, $matches);
55
56
        if (!empty($matches[1])) {
57
            foreach ($matches[1] as $match) {
58
                $css .= trim($match) . "\n";
59
            }
60
        }
61
62
        return $css;
63
    }
64
65
    /**
66
     * @param string $css
67
     *
68
     * @return string
69
     */
70
    private function doCleanup($css)
71
    {
72
        // remove charset
73
        $css = preg_replace('/@charset "[^"]++";/', '', $css);
74
        // remove media queries
75
        $css = preg_replace('/@media [^{]*+{([^{}]++|{[^{}]*+})*+}/', '', $css);
76
77
        $css = str_replace(array("\r", "\n"), '', $css);
78
        $css = str_replace(array("\t"), ' ', $css);
79
        $css = str_replace('"', '\'', $css);
80
        $css = preg_replace('|/\*.*?\*/|', '', $css);
81
        $css = preg_replace('/\s\s++/', ' ', $css);
82
        $css = trim($css);
83
84
        return $css;
85
    }
86
}
87