1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* responsive-images-css |
5
|
|
|
* |
6
|
|
|
* @category Jkphl |
7
|
|
|
* @package Jkphl\Respimgcss |
8
|
|
|
* @subpackage Jkphl\Respimgcss\Infrastructure |
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @copyright Copyright © 2018 Joschi Kuphal <[email protected]> / @jkphl |
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/*********************************************************************************** |
15
|
|
|
* The MIT License (MIT) |
16
|
|
|
* |
17
|
|
|
* Copyright © 2018 Joschi Kuphal <[email protected]> / @jkphl |
18
|
|
|
* |
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
21
|
|
|
* the Software without restriction, including without limitation the rights to |
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
24
|
|
|
* subject to the following conditions: |
25
|
|
|
* |
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
27
|
|
|
* copies or substantial portions of the Software. |
28
|
|
|
* |
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
35
|
|
|
***********************************************************************************/ |
36
|
|
|
|
37
|
|
|
namespace Jkphl\Respimgcss\Infrastructure; |
38
|
|
|
|
39
|
|
|
use Jkphl\Respimgcss\Domain\Contract\CssRuleInterface; |
40
|
|
|
use Jkphl\Respimgcss\Ports\InvalidArgumentException; |
41
|
|
|
use Sabberworm\CSS\CSSList\AtRuleBlockList; |
42
|
|
|
use Sabberworm\CSS\CSSList\Document; |
43
|
|
|
use Sabberworm\CSS\Renderable; |
44
|
|
|
use Sabberworm\CSS\Rule\Rule; |
45
|
|
|
use Sabberworm\CSS\RuleSet\DeclarationBlock; |
46
|
|
|
use Sabberworm\CSS\Value\CSSString; |
47
|
|
|
use Sabberworm\CSS\Value\URL; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* CSS rules serializer |
51
|
|
|
* |
52
|
|
|
* @package Jkphl\Respimgcss |
53
|
|
|
* @subpackage Jkphl\Respimgcss\Infrastructure |
54
|
|
|
*/ |
55
|
|
|
class CssRulesSerializer |
56
|
|
|
{ |
57
|
|
|
/** |
58
|
|
|
* CSS rules |
59
|
|
|
* |
60
|
|
|
* @var CssRuleInterface[] |
61
|
|
|
*/ |
62
|
|
|
protected $rules; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* CSS rules serializer constructor |
66
|
|
|
* |
67
|
|
|
* @param CssRuleInterface[] $rules CSS Rules |
68
|
|
|
*/ |
69
|
1 |
|
public function __construct(array $rules) |
70
|
|
|
{ |
71
|
1 |
|
$this->rules = $rules; |
72
|
1 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Return the registered rules as CSS |
76
|
|
|
* |
77
|
|
|
* @param string $selector CSS selector |
78
|
|
|
* |
79
|
|
|
* @return string Serialized CSS rules |
80
|
|
|
* @throws InvalidArgumentException If the CSS selector is invalid |
81
|
|
|
*/ |
82
|
1 |
|
public function toCss(string $selector): string |
83
|
|
|
{ |
84
|
|
|
// If the CSS selector is invalid |
85
|
1 |
|
$selector = trim($selector); |
86
|
1 |
|
if (!strlen($selector)) { |
87
|
|
|
throw new InvalidArgumentException( |
88
|
|
|
sprintf(InvalidArgumentException::INVALID_CSS_SELECTOR_STR, $selector), |
89
|
|
|
InvalidArgumentException::INVALID_CSS_SELECTOR |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
$cssDocument = new Document(); |
94
|
|
|
|
95
|
|
|
/** @var CssRuleInterface $rule */ |
96
|
1 |
|
foreach ($this->rules as $rule) { |
97
|
1 |
|
$cssDocument->append($this->exportCssRule($rule, $selector)); |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
return $cssDocument->render(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Export a single CSS rule |
105
|
|
|
* |
106
|
|
|
* @param CssRuleInterface $rule CSS rule |
107
|
|
|
* @param string $selector CSS selector |
108
|
|
|
* |
109
|
|
|
* @return Renderable |
110
|
|
|
*/ |
111
|
1 |
|
protected function exportCssRule(CssRuleInterface $rule, string $selector): Renderable |
112
|
|
|
{ |
113
|
|
|
// If the rule has conditions: Render as an @-rule block |
114
|
1 |
|
if (count($rule)) { |
115
|
1 |
|
return $this->exportCssRuleAtBlock($rule, $selector); |
116
|
|
|
} |
117
|
|
|
|
118
|
1 |
|
return $this->exportCssRuleDeclarationBlock($rule, $selector); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Export a CSS rule as @-media block |
123
|
|
|
* |
124
|
|
|
* @param CssRuleInterface $rule CSS rule |
125
|
|
|
* @param string $selector CSS selector |
126
|
|
|
* |
127
|
|
|
* @return AtRuleBlockList @-media block |
128
|
|
|
*/ |
129
|
1 |
|
protected function exportCssRuleAtBlock(CssRuleInterface $rule, string $selector): AtRuleBlockList |
130
|
|
|
{ |
131
|
1 |
|
$atRuleMediaConditions = $this->exportCssRuleMediaConditions($rule); |
132
|
1 |
|
$atRuleBlockList = new AtRuleBlockList('media', $atRuleMediaConditions); |
133
|
1 |
|
$atRuleBlockList->append($this->exportCssRuleDeclarationBlock($rule, $selector)); |
134
|
|
|
|
135
|
1 |
|
return $atRuleBlockList; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Export the media conditions associated with a CSS rule |
140
|
|
|
* |
141
|
|
|
* @param CssRuleInterface $rule CSS rule |
142
|
|
|
* |
143
|
|
|
* @return string Media conditions |
144
|
|
|
*/ |
145
|
1 |
|
protected function exportCssRuleMediaConditions(CssRuleInterface $rule): string |
|
|
|
|
146
|
|
|
{ |
147
|
1 |
|
return 'screen'; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Export a CSS rule as declaration block (without media query) |
152
|
|
|
* |
153
|
|
|
* @param CssRuleInterface $rule CSS rule |
154
|
|
|
* @param string $selector CSS selector |
155
|
|
|
* |
156
|
|
|
* @return DeclarationBlock Declaration block |
157
|
|
|
*/ |
158
|
1 |
|
protected function exportCssRuleDeclarationBlock(CssRuleInterface $rule, string $selector): DeclarationBlock |
159
|
|
|
{ |
160
|
1 |
|
$declarationBlock = new DeclarationBlock(); |
161
|
1 |
|
$declarationBlock->setSelectors([$selector]); |
162
|
1 |
|
$declarationBlock->addRule($this->exportCssRuleRule($rule)); |
163
|
|
|
|
164
|
1 |
|
return $declarationBlock; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Export the CSS rule property and value |
169
|
|
|
* |
170
|
|
|
* @param CssRuleInterface $rule CSS rule |
171
|
|
|
* |
172
|
|
|
* @return Rule Export CSS rule |
173
|
|
|
*/ |
174
|
1 |
|
protected function exportCssRuleRule(CssRuleInterface $rule): Rule |
175
|
|
|
{ |
176
|
1 |
|
$imageCandidateFile = $rule->getImageCandidate()->getFile(); |
177
|
1 |
|
$cssRule = new Rule('background-image'); |
178
|
1 |
|
$cssRule->setValue(new URL(new CSSString($imageCandidateFile))); |
179
|
|
|
|
180
|
1 |
|
return $cssRule; |
181
|
|
|
} |
182
|
|
|
} |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.