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
|
|
|
// width | min-width | max-width |
186
|
|
|
// device-width | min-device-width | max-device-width |
187
|
8 |
|
preg_match_all( |
188
|
8 |
|
'/((?:min|max)\-)?(?:device\-)?width\s*\:\s*/', |
189
|
8 |
|
$mediaConditionString, |
190
|
8 |
|
$widthConditionMatches, |
191
|
8 |
|
PREG_OFFSET_CAPTURE |
192
|
|
|
); |
193
|
|
|
|
194
|
|
|
// Run through all width condition matches |
195
|
8 |
|
foreach ($widthConditionMatches[0] as $widthConditionIndex => $widthConditionMatch) { |
196
|
|
|
try { |
197
|
8 |
|
$matchLength = strlen($widthConditionMatch[0]) + $widthConditionMatch[1]; |
198
|
8 |
|
$widthMediaConditionValue = $this->parseWidthMediaConditionValue( |
199
|
8 |
|
substr($mediaConditionString, $matchLength) |
200
|
|
|
); |
201
|
6 |
|
$widthMediaConditions[] = new WidthMediaCondition( |
202
|
6 |
|
$widthMediaConditionValue, |
203
|
6 |
|
$widthConditionMatches[1][$widthConditionIndex][0] |
204
|
|
|
); |
205
|
2 |
|
} catch (\Exception $e) { |
206
|
8 |
|
continue; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
8 |
|
return $widthMediaConditions; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Parse a width media condition value |
215
|
|
|
* |
216
|
|
|
* @param string $widthMediaConditionStr Width media condition string |
217
|
|
|
* |
218
|
|
|
* @return UnitLengthInterface Width media condition value |
219
|
|
|
*/ |
220
|
8 |
|
protected function parseWidthMediaConditionValue(string $widthMediaConditionStr): UnitLengthInterface |
221
|
|
|
{ |
222
|
8 |
|
$widthMediaConditionValueStr = $this->shiftMediaConditionValue($widthMediaConditionStr); |
223
|
|
|
|
224
|
|
|
// Try to parse as simple unit length |
225
|
|
|
try { |
226
|
8 |
|
return (new LengthFactory($this->calculatorServiceFactory, $this->emPixel)) |
227
|
8 |
|
->createLengthFromString($widthMediaConditionValueStr); |
228
|
3 |
|
} catch (InvalidArgumentException $e) { |
229
|
|
|
// Skip |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
// Try to parse as calc() length |
233
|
3 |
|
return (new CalcLengthFactory($this->calculatorServiceFactory, $this->emPixel)) |
234
|
3 |
|
->createLengthFromString($widthMediaConditionValueStr); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Shift a media condition value off the beginning of a media condition string |
239
|
|
|
* |
240
|
|
|
* @param string $mediaConditionValueStr Media condition string |
241
|
|
|
* |
242
|
|
|
* @return string Media condition value string |
243
|
|
|
*/ |
244
|
8 |
|
protected function shiftMediaConditionValue(string $mediaConditionValueStr): string |
245
|
|
|
{ |
246
|
8 |
|
$stringLength = strlen($mediaConditionValueStr); |
247
|
8 |
|
$balance = 1; |
248
|
8 |
|
for ($char = 0; $char < $stringLength; ++$char) { |
249
|
8 |
|
$balance += $this->getCharacterBalanceModifier($mediaConditionValueStr[$char]); |
250
|
8 |
|
if ($balance == 0) { |
251
|
7 |
|
return substr($mediaConditionValueStr, 0, $char); |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
255
|
1 |
|
return $mediaConditionValueStr; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Return the balance modifier for a particular character |
260
|
|
|
* |
261
|
|
|
* @param string $char Character |
262
|
|
|
* |
263
|
|
|
* @return int Balance modifier |
264
|
|
|
*/ |
265
|
8 |
|
protected function getCharacterBalanceModifier(string $char): int |
266
|
|
|
{ |
267
|
8 |
|
return ($char == ')') ? -1 : (($char == '(') ? 1 : 0); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Extract resolution media conditions |
272
|
|
|
* |
273
|
|
|
* @param string $mediaConditionString Media condition string |
274
|
|
|
* |
275
|
|
|
* @return ResolutionMediaCondition[] Resolution media conditions |
276
|
|
|
*/ |
277
|
8 |
|
protected function parseResolutionMediaConditions(string $mediaConditionString): array |
278
|
|
|
{ |
279
|
8 |
|
$resolutionMediaConditions = []; |
280
|
8 |
|
$resolutionConditionMatches = $this->matchMediaConditions( |
281
|
8 |
|
$mediaConditionString, |
282
|
|
|
// resolution | min-resolution | max-resolution |
283
|
8 |
|
'/((?:min|max)\-)?resolution\s*\:\s*/' |
284
|
|
|
); |
285
|
|
|
|
286
|
|
|
// Run through all width condition matches |
287
|
8 |
|
foreach ($resolutionConditionMatches[0] as $resolutionConditionIndex => $resolutionConditionMatch) { |
288
|
|
|
try { |
289
|
2 |
|
$matchLength = strlen($resolutionConditionMatch[0]) + $resolutionConditionMatch[1]; |
290
|
2 |
|
$resolutionMediaConditionValue = $this->parseResolutionMediaConditionValue( |
291
|
2 |
|
substr($mediaConditionString, $matchLength) |
292
|
|
|
); |
293
|
1 |
|
$resolutionMediaConditions [] = new ResolutionMediaCondition( |
294
|
1 |
|
$resolutionMediaConditionValue, |
295
|
1 |
|
$resolutionConditionMatches[1][$resolutionConditionIndex][0] |
296
|
|
|
); |
297
|
2 |
|
} catch (\Exception $e) { |
298
|
2 |
|
continue; |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
|
302
|
8 |
|
return $resolutionMediaConditions; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Parse a resolution media condition value |
307
|
|
|
* |
308
|
|
|
* @param string $resolutionMediaConditionStr Resolution media condition string |
309
|
|
|
* |
310
|
|
|
* @return LengthInterface Resolution media condition value |
311
|
|
|
*/ |
312
|
2 |
|
protected function parseResolutionMediaConditionValue(string $resolutionMediaConditionStr): LengthInterface |
313
|
|
|
{ |
314
|
2 |
|
$resolutionMediaConditionValueStr = $this->shiftMediaConditionValue($resolutionMediaConditionStr); |
315
|
|
|
|
316
|
2 |
|
return $this->createAbsoluteLength($resolutionMediaConditionValueStr); |
|
|
|
|
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Match a media condition string |
321
|
|
|
* |
322
|
|
|
* @param string $mediaConditionString Media condition string |
323
|
|
|
* @param string $pattern PCRE pattern |
324
|
|
|
* |
325
|
|
|
* @return array Matches |
326
|
|
|
*/ |
327
|
8 |
|
protected function matchMediaConditions(string $mediaConditionString, string $pattern): array |
328
|
|
|
{ |
329
|
8 |
|
preg_match_all($pattern, $mediaConditionString, $matches, PREG_OFFSET_CAPTURE); |
330
|
|
|
|
331
|
8 |
|
return $matches; |
332
|
|
|
} |
333
|
|
|
} |