Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace TijsVerkoyen\CssToInlineStyles\Css\Property;
4
 
5
use Symfony\Component\CssSelector\Node\Specificity;
6
 
7
class Processor
8
{
9
    /**
10
     * Split a string into separate properties
11
     *
12
     * @param string $propertiesString
13
     *
14
     * @return string[]
15
     */
16
    public function splitIntoSeparateProperties($propertiesString)
17
    {
18
        $propertiesString = $this->cleanup($propertiesString);
19
 
20
        $properties = (array) explode(';', $propertiesString);
21
        $keysToRemove = array();
22
        $numberOfProperties = count($properties);
23
 
24
        for ($i = 0; $i < $numberOfProperties; $i++) {
25
            $properties[$i] = trim($properties[$i]);
26
 
27
            // if the new property begins with base64 it is part of the current property
28
            if (isset($properties[$i + 1]) && strpos(trim($properties[$i + 1]), 'base64,') === 0) {
29
                $properties[$i] .= ';' . trim($properties[$i + 1]);
30
                $keysToRemove[] = $i + 1;
31
            }
32
        }
33
 
34
        if (!empty($keysToRemove)) {
35
            foreach ($keysToRemove as $key) {
36
                unset($properties[$key]);
37
            }
38
        }
39
 
40
        return array_values($properties);
41
    }
42
 
43
    /**
44
     * @param string $string
45
     *
46
     * @return string
47
     */
48
    private function cleanup($string)
49
    {
50
        $string = str_replace(array("\r", "\n"), '', $string);
51
        $string = str_replace(array("\t"), ' ', $string);
52
        $string = str_replace('"', '\'', $string);
53
        $string = preg_replace('|/\*.*?\*/|', '', $string);
54
        $string = preg_replace('/\s\s+/', ' ', $string);
55
 
56
        $string = trim($string);
57
        $string = rtrim($string, ';');
58
 
59
        return $string;
60
    }
61
 
62
    /**
63
     * Converts a property-string into an object
64
     *
65
     * @param string $property
66
     *
67
     * @return Property|null
68
     */
69
    public function convertToObject($property, Specificity $specificity = null)
70
    {
71
        if (strpos($property, ':') === false) {
72
            return null;
73
        }
74
 
75
        list($name, $value) = explode(':', $property, 2);
76
 
77
        $name = trim($name);
78
        $value = trim($value);
79
 
80
        if ($value === '') {
81
            return null;
82
        }
83
 
84
        return new Property($name, $value, $specificity);
85
    }
86
 
87
    /**
88
     * Converts an array of property-strings into objects
89
     *
90
     * @param string[] $properties
91
     *
92
     * @return Property[]
93
     */
94
    public function convertArrayToObjects(array $properties, Specificity $specificity = null)
95
    {
96
        $objects = array();
97
 
98
        foreach ($properties as $property) {
99
            $object = $this->convertToObject($property, $specificity);
100
            if ($object === null) {
101
                continue;
102
            }
103
 
104
            $objects[] = $object;
105
        }
106
 
107
        return $objects;
108
    }
109
 
110
    /**
111
     * Build the property-string for multiple properties
112
     *
113
     * @param Property[] $properties
114
     *
115
     * @return string
116
     */
117
    public function buildPropertiesString(array $properties)
118
    {
119
        $chunks = array();
120
 
121
        foreach ($properties as $property) {
122
            $chunks[] = $property->toString();
123
        }
124
 
125
        return implode(' ', $chunks);
126
    }
127
}