1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* responsive-images-css |
5
|
|
|
* |
6
|
|
|
* @category Jkphl |
7
|
|
|
* @package Jkphl\Respimgcss |
8
|
|
|
* @subpackage Jkphl\Respimgcss\Application\Factory |
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\Application\Factory; |
38
|
|
|
|
39
|
|
|
use Jkphl\Respimgcss\Application\Contract\UnitLengthInterface; |
40
|
|
|
use Jkphl\Respimgcss\Application\Exceptions\InvalidArgumentException; |
41
|
|
|
use Jkphl\Respimgcss\Application\Model\SourceSize; |
42
|
|
|
use Jkphl\Respimgcss\Application\Model\SourceSizeMediaCondition; |
43
|
|
|
use Jkphl\Respimgcss\Domain\Contract\LengthInterface; |
44
|
|
|
use Jkphl\Respimgcss\Domain\Model\Css\ResolutionMediaCondition; |
45
|
|
|
use Jkphl\Respimgcss\Domain\Model\Css\WidthMediaCondition; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Source size factory |
49
|
|
|
* |
50
|
|
|
* @package Jkphl\Respimgcss |
51
|
|
|
* @subpackage Jkphl\Respimgcss\Application\Factory |
52
|
|
|
*/ |
53
|
|
|
class SourceSizeFactory extends AbstractLengthFactory |
54
|
|
|
{ |
55
|
|
|
/** |
56
|
|
|
* Create a source size value from a source size string |
57
|
|
|
* |
58
|
|
|
* @param string $sourceSizeStr Source size string |
59
|
|
|
* |
60
|
|
|
* @return SourceSize Source size |
61
|
|
|
*/ |
62
|
11 |
|
public function createFromSourceSizeStr(string $sourceSizeStr): SourceSize |
63
|
|
|
{ |
64
|
|
|
// Determine the source size value |
65
|
11 |
|
$sourceSizeValue = $this->parseSourceSizeValue($sourceSizeStr); |
66
|
|
|
|
67
|
|
|
// Determine the associated media condition (if any) |
68
|
8 |
|
$mediaCondition = $this->parseMediaCondition($sourceSizeStr); |
69
|
|
|
|
70
|
|
|
// Return a source size instance |
71
|
8 |
|
return new SourceSize($sourceSizeValue, $mediaCondition); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Parse the length component |
76
|
|
|
* |
77
|
|
|
* @param string $sourceSizeStr Source size string |
78
|
|
|
* |
79
|
|
|
* @return UnitLengthInterface AbstractLength component |
80
|
|
|
* @throws InvalidArgumentException If the source size string is ill-formatted |
81
|
|
|
*/ |
82
|
11 |
|
protected function parseSourceSizeValue(string &$sourceSizeStr): UnitLengthInterface |
83
|
|
|
{ |
84
|
|
|
// If the source size string ends with a parenthesis: Try to parse a calc() base length |
85
|
11 |
|
if (substr($sourceSizeStr, -1) === ')') { |
86
|
3 |
|
return $this->parseSourceSizeCalculatedValue($sourceSizeStr); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// If the source size string is ill-formatted |
90
|
8 |
|
if (!preg_match('/^(.*\s+)?([^\s]+)$/', $sourceSizeStr, $sourceSizeStrMatch)) { |
91
|
1 |
|
throw new InvalidArgumentException( |
92
|
1 |
|
sprintf(InvalidArgumentException::ILL_FORMATTED_SOURCE_SIZE_STRING_STR, $sourceSizeStr), |
93
|
1 |
|
InvalidArgumentException::ILL_FORMATTED_SOURCE_SIZE_STRING |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// Post-process the remaining string |
98
|
7 |
|
$sourceSizeStr = trim($sourceSizeStrMatch[1]); |
99
|
|
|
|
100
|
|
|
// Return the parsed length |
101
|
7 |
|
return (new LengthFactory($this->calculatorServiceFactory, $this->emPixel)) |
102
|
7 |
|
->createLengthFromString($sourceSizeStrMatch[2]); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Parse a calc() based length value |
107
|
|
|
* |
108
|
|
|
* @param string $sourceSizeStr Source size string |
109
|
|
|
* |
110
|
|
|
* @return UnitLengthInterface AbstractLength component |
111
|
|
|
* @throws InvalidArgumentException If the source size string is ill-formatted |
112
|
|
|
*/ |
113
|
3 |
|
protected function parseSourceSizeCalculatedValue(string &$sourceSizeStr): UnitLengthInterface |
114
|
|
|
{ |
115
|
|
|
// Reverse-consume the source size string |
116
|
3 |
|
$sourceSizeRev = strrev($sourceSizeStr); |
117
|
3 |
|
$balance = null; |
118
|
3 |
|
for ($pos = 0; $pos < strlen($sourceSizeStr); ++$pos) { |
119
|
3 |
|
$balance += $this->getCharacterBalance($sourceSizeRev[$pos]); |
120
|
3 |
|
if ($balance === 0) { |
121
|
1 |
|
$length = (new CalcLengthFactory($this->calculatorServiceFactory, $this->emPixel)) |
122
|
1 |
|
->createLengthFromString(substr($sourceSizeStr, -($pos + 5))); |
123
|
1 |
|
$sourceSizeStr = trim(substr($sourceSizeStr, 0, -($pos + 5))); |
124
|
|
|
|
125
|
1 |
|
return $length; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// Else: The source size string is ill-formatted |
130
|
2 |
|
throw new InvalidArgumentException( |
131
|
2 |
|
sprintf(InvalidArgumentException::ILL_FORMATTED_SOURCE_SIZE_STRING_STR, $sourceSizeStr), |
132
|
2 |
|
InvalidArgumentException::ILL_FORMATTED_SOURCE_SIZE_STRING |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Return the balance value for a particular value |
138
|
|
|
* |
139
|
|
|
* @param string $char Character |
140
|
|
|
* |
141
|
|
|
* @return int Balance value |
142
|
|
|
*/ |
143
|
3 |
|
protected function getCharacterBalance($char): string |
144
|
|
|
{ |
145
|
3 |
|
return ($char === '(') ? 1 : (($char === ')') ? -1 : 0); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Parse and instantiate a source size media condition |
150
|
|
|
* |
151
|
|
|
* @param string $mediaConditionString |
152
|
|
|
* |
153
|
|
|
* @return SourceSizeMediaCondition|null Source size media condition |
154
|
|
|
* @see https://drafts.csswg.org/mediaqueries-4/#typedef-media-condition |
155
|
|
|
* @see https://developer.mozilla.org/de/docs/Web/CSS/Media_Queries/Using_media_queries#Pseudo-BNF_(for_those_of_you_that_like_that_kind_of_thing) |
156
|
|
|
*/ |
157
|
8 |
|
protected function parseMediaCondition(string $mediaConditionString): ?SourceSizeMediaCondition |
158
|
|
|
{ |
159
|
8 |
|
return new SourceSizeMediaCondition( |
160
|
8 |
|
$mediaConditionString, |
161
|
8 |
|
array_merge( |
162
|
8 |
|
$this->parseWidthMediaConditions($mediaConditionString), |
163
|
8 |
|
$this->parseResolutionMediaConditions($mediaConditionString) |
164
|
|
|
) |
165
|
|
|
); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Extract width media conditions |
170
|
|
|
* |
171
|
|
|
* @param string $mediaConditionString Media condition string |
172
|
|
|
* |
173
|
|
|
* @return WidthMediaCondition[] Width media conditions |
174
|
|
|
*/ |
175
|
8 |
|
protected function parseWidthMediaConditions(string $mediaConditionString): array |
176
|
|
|
{ |
177
|
8 |
|
$widthMediaConditions = []; |
178
|
8 |
|
$widthConditionMatches = $this->matchMediaConditions( |
179
|
8 |
|
$mediaConditionString, |
180
|
|
|
// width | min-width | max-width |
181
|
|
|
// device-width | min-device-width | max-device-width |
182
|
8 |
|
'/((?:min|max)\-)?(?:device\-)?width\s*\:\s*/' |
183
|
|
|
); |
184
|
|
|
|
185
|
8 |
|
foreach ($widthConditionMatches as $match) { |
186
|
|
|
try { |
187
|
8 |
|
$widthMediaConditions[] = new WidthMediaCondition( |
188
|
8 |
|
$this->parseWidthMediaConditionValue($match[0]), |
189
|
6 |
|
$match[1] |
190
|
|
|
); |
191
|
2 |
|
} catch (\Exception $e) { |
192
|
8 |
|
continue; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
8 |
|
return $widthMediaConditions; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Parse a width media condition value |
201
|
|
|
* |
202
|
|
|
* @param string $widthMediaConditionStr Width media condition string |
203
|
|
|
* |
204
|
|
|
* @return UnitLengthInterface Width media condition value |
205
|
|
|
*/ |
206
|
8 |
|
protected function parseWidthMediaConditionValue(string $widthMediaConditionStr): UnitLengthInterface |
207
|
|
|
{ |
208
|
8 |
|
$widthMediaConditionValueStr = $this->shiftMediaConditionValue($widthMediaConditionStr); |
209
|
|
|
|
210
|
|
|
// Try to parse as simple unit length |
211
|
|
|
try { |
212
|
8 |
|
return (new LengthFactory($this->calculatorServiceFactory, $this->emPixel)) |
213
|
8 |
|
->createLengthFromString($widthMediaConditionValueStr); |
214
|
3 |
|
} catch (InvalidArgumentException $e) { |
215
|
|
|
// Skip |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
// Try to parse as calc() length |
219
|
3 |
|
return (new CalcLengthFactory($this->calculatorServiceFactory, $this->emPixel)) |
220
|
3 |
|
->createLengthFromString($widthMediaConditionValueStr); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Shift a media condition value off the beginning of a media condition string |
225
|
|
|
* |
226
|
|
|
* @param string $mediaConditionValueStr Media condition string |
227
|
|
|
* |
228
|
|
|
* @return string Media condition value string |
229
|
|
|
*/ |
230
|
8 |
|
protected function shiftMediaConditionValue(string $mediaConditionValueStr): string |
231
|
|
|
{ |
232
|
8 |
|
$stringLength = strlen($mediaConditionValueStr); |
233
|
8 |
|
$balance = 1; |
234
|
8 |
|
for ($char = 0; $char < $stringLength; ++$char) { |
235
|
8 |
|
$balance += $this->getCharacterBalanceModifier($mediaConditionValueStr[$char]); |
236
|
8 |
|
if ($balance == 0) { |
237
|
7 |
|
return substr($mediaConditionValueStr, 0, $char); |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
241
|
1 |
|
return $mediaConditionValueStr; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Return the balance modifier for a particular character |
246
|
|
|
* |
247
|
|
|
* @param string $char Character |
248
|
|
|
* |
249
|
|
|
* @return int Balance modifier |
250
|
|
|
*/ |
251
|
8 |
|
protected function getCharacterBalanceModifier(string $char): int |
252
|
|
|
{ |
253
|
8 |
|
return ($char == ')') ? -1 : (($char == '(') ? 1 : 0); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Extract resolution media conditions |
258
|
|
|
* |
259
|
|
|
* @param string $mediaConditionString Media condition string |
260
|
|
|
* |
261
|
|
|
* @return ResolutionMediaCondition[] Resolution media conditions |
262
|
|
|
*/ |
263
|
8 |
|
protected function parseResolutionMediaConditions(string $mediaConditionString): array |
264
|
|
|
{ |
265
|
8 |
|
$resolutionMediaConditions = []; |
266
|
8 |
|
$resolutionConditionMatches = $this->matchMediaConditions( |
267
|
8 |
|
$mediaConditionString, |
268
|
|
|
// resolution | min-resolution | max-resolution |
269
|
8 |
|
'/((?:min|max)\-)?resolution\s*\:\s*/' |
270
|
|
|
); |
271
|
|
|
|
272
|
8 |
|
foreach ($resolutionConditionMatches as $match) { |
273
|
|
|
try { |
274
|
2 |
|
$resolutionMediaConditions[] = new ResolutionMediaCondition( |
275
|
2 |
|
$this->parseResolutionMediaConditionValue($match[0]), |
276
|
1 |
|
$match[1] |
277
|
|
|
); |
278
|
1 |
|
} catch (\Exception $e) { |
279
|
2 |
|
continue; |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|
283
|
8 |
|
return $resolutionMediaConditions; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Match a media condition string |
288
|
|
|
* |
289
|
|
|
* @param string $mediaConditionString Media condition string |
290
|
|
|
* @param string $pattern PCRE pattern |
291
|
|
|
* |
292
|
|
|
* @return array Matches |
293
|
|
|
*/ |
294
|
8 |
|
protected function matchMediaConditions(string $mediaConditionString, string $pattern): array |
295
|
|
|
{ |
296
|
8 |
|
preg_match_all($pattern, $mediaConditionString, $matches, PREG_OFFSET_CAPTURE); |
297
|
8 |
|
$refinedMatches = []; |
298
|
8 |
|
foreach ($matches[0] as $index => $match) { |
299
|
8 |
|
$matchLength = strlen($match[0]) + $match[1]; |
300
|
8 |
|
$matchModifier = is_array($matches[1][$index]) ? $matches[1][$index][0] : ''; |
301
|
8 |
|
$refinedMatches[] = [substr($mediaConditionString, $matchLength), $matchModifier]; |
302
|
|
|
} |
303
|
|
|
|
304
|
8 |
|
return $refinedMatches; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Parse a resolution media condition value |
309
|
|
|
* |
310
|
|
|
* @param string $resolutionMediaConditionStr Resolution media condition string |
311
|
|
|
* |
312
|
|
|
* @return LengthInterface Resolution media condition value |
313
|
|
|
*/ |
314
|
2 |
|
protected function parseResolutionMediaConditionValue(string $resolutionMediaConditionStr): LengthInterface |
315
|
|
|
{ |
316
|
2 |
|
$resolutionMediaConditionValueStr = $this->shiftMediaConditionValue($resolutionMediaConditionStr); |
317
|
|
|
|
318
|
2 |
|
return $this->createAbsoluteLength($resolutionMediaConditionValueStr); |
|
|
|
|
319
|
|
|
} |
320
|
|
|
} |