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 seperate properties |
11
|
|
|
* |
12
|
|
|
* @param string $propertiesString |
13
|
|
|
* @return array |
14
|
|
|
*/ |
15
|
|
|
public function splitIntoSeparateProperties($propertiesString) |
16
|
|
|
{ |
17
|
|
|
$propertiesString = $this->cleanup($propertiesString); |
18
|
|
|
|
19
|
|
|
$properties = (array) explode(';', $propertiesString); |
20
|
|
|
$keysToRemove = array(); |
21
|
|
|
$numberOfProperties = count($properties); |
22
|
|
|
|
23
|
|
|
for ($i = 0; $i < $numberOfProperties; $i++) { |
24
|
|
|
$properties[$i] = trim($properties[$i]); |
25
|
|
|
|
26
|
|
|
// if the new property begins with base64 it is part of the current property |
27
|
|
|
if (isset($properties[$i + 1]) && strpos(trim($properties[$i + 1]), 'base64,') === 0) { |
28
|
|
|
$properties[$i] .= ';' . trim($properties[$i + 1]); |
29
|
|
|
$keysToRemove[] = $i + 1; |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if (!empty($keysToRemove)) { |
34
|
|
|
foreach ($keysToRemove as $key) { |
35
|
|
|
unset($properties[$key]); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return array_values($properties); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param $string |
44
|
|
|
* @return mixed|string |
45
|
|
|
*/ |
46
|
|
View Code Duplication |
private function cleanup($string) |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
$string = str_replace(array("\r", "\n"), '', $string); |
49
|
|
|
$string = str_replace(array("\t"), ' ', $string); |
50
|
|
|
$string = str_replace('"', '\'', $string); |
51
|
|
|
$string = preg_replace('|/\*.*?\*/|', '', $string); |
52
|
|
|
$string = preg_replace('/\s\s+/', ' ', $string); |
53
|
|
|
|
54
|
|
|
$string = trim($string); |
55
|
|
|
$string = rtrim($string, ';'); |
56
|
|
|
|
57
|
|
|
return $string; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Convert a property-string into an object |
62
|
|
|
* |
63
|
|
|
* @param string $property |
64
|
|
|
* @return Property|null |
65
|
|
|
*/ |
66
|
|
|
public function convertToObject($property, Specificity $specificity = null) |
67
|
|
|
{ |
68
|
|
|
if (strpos($property, ':') === false) { |
69
|
|
|
return null; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
list($name, $value) = explode(':', $property, 2); |
73
|
|
|
|
74
|
|
|
$name = trim($name); |
75
|
|
|
$value = trim($value); |
76
|
|
|
|
77
|
|
|
if ($value === '') { |
78
|
|
|
return null; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return new Property($name, $value, $specificity); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Convert an array of property-strings into objects |
86
|
|
|
* |
87
|
|
|
* @param array $properties |
88
|
|
|
* @return Property[] |
89
|
|
|
*/ |
90
|
|
|
public function convertArrayToObjects(array $properties, Specificity $specificity = null) |
91
|
|
|
{ |
92
|
|
|
$objects = array(); |
93
|
|
|
|
94
|
|
|
foreach ($properties as $property) { |
95
|
|
|
$object = $this->convertToObject($property, $specificity); |
96
|
|
|
if ($object === null) { |
97
|
|
|
continue; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$objects[] = $object; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $objects; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Build the property-string for multiple properties |
108
|
|
|
* |
109
|
|
|
* @param array $properties |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function buildPropertiesString(array $properties) |
113
|
|
|
{ |
114
|
|
|
$chunks = array(); |
115
|
|
|
|
116
|
|
|
foreach ($properties as $property) { |
117
|
|
|
$chunks[] = $property->toString(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return implode(' ', $chunks); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.