|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ReliqArts\StyleImporter\CSS\Rule; |
|
6
|
|
|
|
|
7
|
|
|
use ReliqArts\StyleImporter\CSS\Rule as RuleContract; |
|
8
|
|
|
|
|
9
|
|
|
final class Rule implements RuleContract |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var string |
|
13
|
|
|
*/ |
|
14
|
|
|
private $selector; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
private $properties; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
private $mediaQuery; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Rule constructor. |
|
28
|
|
|
* |
|
29
|
|
|
* @param string $selector |
|
30
|
|
|
* @param string $properties |
|
31
|
|
|
* @param string $mediaQuery |
|
32
|
|
|
*/ |
|
33
|
|
|
private function __construct(string $selector, string $properties, string $mediaQuery) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->selector = $selector; |
|
36
|
|
|
$this->properties = $properties; |
|
37
|
|
|
$this->mediaQuery = $mediaQuery; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return string |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __toString(): string |
|
44
|
|
|
{ |
|
45
|
|
|
$rule = sprintf('%s {%s}', $this->selector, $this->properties); |
|
46
|
|
|
|
|
47
|
|
|
if (empty($this->mediaQuery)) { |
|
48
|
|
|
return $rule; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return sprintf('%s {%s}', $this->mediaQuery, $rule); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param string $selector |
|
56
|
|
|
* @param string $properties |
|
57
|
|
|
* @param string $mediaQuery |
|
58
|
|
|
* |
|
59
|
|
|
* @return Rule |
|
60
|
|
|
*/ |
|
61
|
|
|
public static function createNormalized(string $selector, string $properties, string $mediaQuery = ''): Rule |
|
62
|
|
|
{ |
|
63
|
|
|
return new self( |
|
64
|
|
|
self::normalizeSelector($selector), |
|
65
|
|
|
self::normalizeProperties($properties), |
|
66
|
|
|
trim($mediaQuery) |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param string $selector |
|
72
|
|
|
* |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
|
|
private static function normalizeSelector(string $selector): string |
|
76
|
|
|
{ |
|
77
|
|
|
return trim(current(explode(',', $selector))); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param string $properties |
|
82
|
|
|
* |
|
83
|
|
|
* @return string |
|
84
|
|
|
*/ |
|
85
|
|
|
private static function normalizeProperties(string $properties): string |
|
86
|
|
|
{ |
|
87
|
|
|
return trim(str_replace("\n", ' ', $properties)); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|