|
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\Application\Contract\LengthFactoryInterface; |
|
40
|
|
|
use Jkphl\Respimgcss\Application\Contract\SourceSizeListInterface; |
|
41
|
|
|
use Jkphl\Respimgcss\Application\Model\SourceSize; |
|
42
|
|
|
use Jkphl\Respimgcss\Application\Model\SourceSizeMediaCondition; |
|
43
|
|
|
use Jkphl\Respimgcss\Domain\Contract\AbsoluteLengthInterface; |
|
44
|
|
|
use Jkphl\Respimgcss\Domain\Contract\ImageCandidateInterface; |
|
45
|
|
|
use Jkphl\Respimgcss\Domain\Contract\ImageCandidateSetInterface; |
|
46
|
|
|
use Jkphl\Respimgcss\Domain\Contract\SourceSizeImageCandidateMatch; |
|
47
|
|
|
use Jkphl\Respimgcss\Domain\Model\Css\MediaCondition; |
|
48
|
|
|
use Jkphl\Respimgcss\Domain\Model\ImageCandidateMatch; |
|
49
|
|
|
use Jkphl\Respimgcss\Ports\InvalidArgumentException; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Sizes list |
|
53
|
|
|
* |
|
54
|
|
|
* @package Jkphl\Respimgcss |
|
55
|
|
|
* @subpackage Jkphl\Respimgcss\Infrastructure |
|
56
|
|
|
* @see http://w3c.github.io/html/semantics-embedded-content.html#ref-for-viewport-based-selection%E2%91%A0 |
|
57
|
|
|
* @see http://w3c.github.io/html/semantics-embedded-content.html#valid-source-size-list |
|
58
|
|
|
* @see http://w3c.github.io/html/semantics-embedded-content.html#parse-a-sizes-attribute |
|
59
|
|
|
* @see https://www.sitepoint.com/community/t/pixels-or-percentages-for-media-queries/37487 |
|
60
|
|
|
*/ |
|
61
|
|
|
class SourceSizeList extends \ArrayObject implements SourceSizeListInterface |
|
62
|
|
|
{ |
|
63
|
|
|
/** |
|
64
|
|
|
* Length factory |
|
65
|
|
|
* |
|
66
|
|
|
* @var LengthFactoryInterface |
|
67
|
|
|
*/ |
|
68
|
|
|
protected $lengthFactory; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Source size list constructor |
|
72
|
|
|
* |
|
73
|
|
|
* @param SourceSize[] $sourceSizes Source sizes |
|
74
|
|
|
* @param LengthFactoryInterface $lengthFactory Length factory |
|
75
|
|
|
* |
|
76
|
|
|
* @throws InvalidArgumentException If the source size is invalid |
|
77
|
|
|
*/ |
|
78
|
11 |
|
public function __construct(array $sourceSizes, LengthFactoryInterface $lengthFactory) |
|
79
|
|
|
{ |
|
80
|
|
|
// Run through all source sizes |
|
81
|
11 |
|
foreach ($sourceSizes as $sourceSize) { |
|
82
|
|
|
// If the source size is invalid |
|
83
|
9 |
|
if (!($sourceSize instanceof SourceSize)) { |
|
84
|
1 |
|
throw new InvalidArgumentException( |
|
85
|
1 |
|
InvalidArgumentException::INVALID_SOURCE_SIZE_STR, |
|
86
|
9 |
|
InvalidArgumentException::INVALID_SOURCE_SIZE |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
10 |
|
usort($sourceSizes, [$this, 'sortSourceSizes']); |
|
92
|
10 |
|
parent::__construct($sourceSizes); |
|
93
|
|
|
|
|
94
|
10 |
|
$this->lengthFactory = $lengthFactory; |
|
95
|
10 |
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Find the optimum image candidate for a particular breakpoint |
|
99
|
|
|
* |
|
100
|
|
|
* @param ImageCandidateSetInterface $imageCandidates Image candidates |
|
101
|
|
|
* @param AbsoluteLengthInterface $breakpoint Breakpoint |
|
102
|
|
|
* @param float $density Density |
|
103
|
|
|
* |
|
104
|
|
|
* @return SourceSizeImageCandidateMatch|null Image candidate match |
|
105
|
|
|
*/ |
|
106
|
6 |
|
public function findImageCandidate( |
|
107
|
|
|
ImageCandidateSetInterface $imageCandidates, |
|
108
|
|
|
AbsoluteLengthInterface $breakpoint, |
|
109
|
|
|
float $density |
|
110
|
|
|
): ?SourceSizeImageCandidateMatch { |
|
111
|
|
|
// Run through the source sizes (from biggest to smallest) |
|
112
|
|
|
/** @var SourceSize $sourceSize */ |
|
113
|
6 |
|
$lastMinimumWidth = null; |
|
114
|
6 |
|
foreach ($this->getArrayCopy() as $sourceSize) { |
|
115
|
5 |
|
$mediaCondition = $sourceSize->getMediaCondition(); |
|
116
|
|
|
|
|
117
|
|
|
// If the current breakpoint and density matches the source size condition |
|
118
|
5 |
|
if ($mediaCondition->matches($breakpoint, $density, $lastMinimumWidth)) { |
|
119
|
5 |
|
$maximum = $this->getSourceSizeMaximumWidth($mediaCondition, $lastMinimumWidth); |
|
120
|
5 |
|
return $this->findImageCandidateForSourceSize( |
|
121
|
5 |
|
$sourceSize, $imageCandidates, $density, $breakpoint, $maximum |
|
122
|
|
|
); |
|
123
|
|
|
} |
|
124
|
3 |
|
$lastMinimumWidth = $mediaCondition->getMinimumWidth(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
1 |
|
return null; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Get the maximum width for a source size |
|
132
|
|
|
* |
|
133
|
|
|
* @param SourceSizeMediaCondition $condition Source size media condition |
|
134
|
|
|
* @param float|null $lastMinimumWidth Last source size minimum width |
|
135
|
|
|
* |
|
136
|
|
|
* @return AbsoluteLengthInterface Maximum width |
|
137
|
|
|
*/ |
|
138
|
5 |
|
protected function getSourceSizeMaximumWidth( |
|
139
|
|
|
SourceSizeMediaCondition $condition, |
|
140
|
|
|
float $lastMinimumWidth = null |
|
141
|
|
|
): ?AbsoluteLengthInterface { |
|
142
|
5 |
|
$maximumWidth = $this->considerLastMinimumWidth($condition->getMaximumWidth(), $lastMinimumWidth); |
|
143
|
5 |
|
if ($maximumWidth === null) { |
|
144
|
4 |
|
return null; |
|
145
|
|
|
} |
|
146
|
4 |
|
if ($lastMinimumWidth !== null) { |
|
147
|
3 |
|
$maximumWidth = max(0, min($maximumWidth, $lastMinimumWidth - 1)); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
4 |
|
return $this->lengthFactory->createAbsoluteLength($maximumWidth); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Consider the last minimum width in case the maximum width is undefined |
|
155
|
|
|
* |
|
156
|
|
|
* @param int|null $maximumWidth Maximum width |
|
157
|
|
|
* @param float|null $lastMinimumWidth Last minimum width |
|
158
|
|
|
* |
|
159
|
|
|
* @return float|null Maximum width |
|
160
|
|
|
*/ |
|
161
|
5 |
|
protected function considerLastMinimumWidth(int $maximumWidth = null, float $lastMinimumWidth = null): ?float |
|
162
|
|
|
{ |
|
163
|
5 |
|
if (($maximumWidth === null) && ($lastMinimumWidth !== null)) { |
|
164
|
3 |
|
$maximumWidth = max(0, $lastMinimumWidth - 1); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
5 |
|
return $maximumWidth; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Find an image candidate for a particular source size |
|
172
|
|
|
* |
|
173
|
|
|
* @param SourceSize $sourceSize Matching source size |
|
174
|
|
|
* @param ImageCandidateSetInterface $imageCandidates Image candidates |
|
175
|
|
|
* @param float $density Density |
|
176
|
|
|
* @param AbsoluteLengthInterface $minWidth Minimum viewport width |
|
177
|
|
|
* @param AbsoluteLengthInterface|null $maxWidth Maximum viewport width |
|
178
|
|
|
* |
|
179
|
|
|
* @return SourceSizeImageCandidateMatch|null |
|
180
|
|
|
*/ |
|
181
|
5 |
|
protected function findImageCandidateForSourceSize( |
|
182
|
|
|
SourceSize $sourceSize, |
|
183
|
|
|
ImageCandidateSetInterface $imageCandidates, |
|
184
|
|
|
float $density, |
|
185
|
|
|
AbsoluteLengthInterface $minWidth, |
|
186
|
|
|
AbsoluteLengthInterface $maxWidth = null |
|
187
|
|
|
): ?SourceSizeImageCandidateMatch { |
|
188
|
|
|
// If there's no upper limit: Use the largest image candidate in any case |
|
189
|
5 |
|
if ($maxWidth === null) { |
|
190
|
4 |
|
return $this->createLargestImageCandidateMatch($sourceSize, $imageCandidates); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
// Run through all image candidates for the effective minimum image, current source size and current breakpoint |
|
194
|
4 |
|
return $this->findImageCandidateForMinImageWidth( |
|
195
|
4 |
|
$sourceSize, |
|
196
|
4 |
|
$imageCandidates, |
|
197
|
4 |
|
round( |
|
|
|
|
|
|
198
|
4 |
|
max( |
|
199
|
4 |
|
$sourceSize->getValue()->getValue($minWidth), |
|
|
|
|
|
|
200
|
4 |
|
$sourceSize->getValue()->getValue($maxWidth) |
|
201
|
4 |
|
) * $density |
|
202
|
|
|
) |
|
203
|
|
|
); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Create and return a match for the largest image candidate |
|
208
|
|
|
* |
|
209
|
|
|
* @param SourceSize $sourceSize Source size |
|
210
|
|
|
* @param ImageCandidateSetInterface $imageCandidates Image candidates |
|
211
|
|
|
* |
|
212
|
|
|
* @return SourceSizeImageCandidateMatch|null Largest image candidate match |
|
213
|
|
|
*/ |
|
214
|
4 |
|
protected function createLargestImageCandidateMatch( |
|
215
|
|
|
SourceSize $sourceSize, |
|
216
|
|
|
ImageCandidateSetInterface $imageCandidates |
|
217
|
|
|
): ?SourceSizeImageCandidateMatch { |
|
218
|
4 |
|
if (count($imageCandidates)) { |
|
219
|
3 |
|
$largestImageCandidate = $imageCandidates[count($imageCandidates) - 1]; |
|
220
|
|
|
|
|
221
|
3 |
|
return $this->createImageCandidateMatch($sourceSize, $largestImageCandidate); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
1 |
|
return null; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Create a source size image candidate match |
|
229
|
|
|
* |
|
230
|
|
|
* @param SourceSize $sourceSize Matching source size |
|
231
|
|
|
* @param ImageCandidateInterface $imageCandidate Image candidata |
|
232
|
|
|
* |
|
233
|
|
|
* @return SourceSizeImageCandidateMatch Source size image candidate match |
|
234
|
|
|
*/ |
|
235
|
3 |
|
protected function createImageCandidateMatch( |
|
236
|
|
|
SourceSize $sourceSize, |
|
237
|
|
|
ImageCandidateInterface $imageCandidate |
|
238
|
|
|
): SourceSizeImageCandidateMatch { |
|
239
|
3 |
|
$mediaCondition = new MediaCondition('', $sourceSize->getMediaCondition()->getValue()); |
|
240
|
|
|
|
|
241
|
3 |
|
return new ImageCandidateMatch($mediaCondition, $imageCandidate); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Find an image candidate for a particular source size |
|
246
|
|
|
* |
|
247
|
|
|
* @param SourceSize $sourceSize Matching source size |
|
248
|
|
|
* @param ImageCandidateSetInterface $imageCandidates Image candidates |
|
249
|
|
|
* @param int $minImageWidth Minimum image width |
|
250
|
|
|
* |
|
251
|
|
|
* @return SourceSizeImageCandidateMatch|null Image candidate |
|
252
|
|
|
*/ |
|
253
|
4 |
|
protected function findImageCandidateForMinImageWidth( |
|
254
|
|
|
SourceSize $sourceSize, |
|
255
|
|
|
ImageCandidateSetInterface $imageCandidates, |
|
256
|
|
|
int $minImageWidth |
|
257
|
|
|
): ?SourceSizeImageCandidateMatch { |
|
258
|
|
|
// Run through all image candidates |
|
259
|
|
|
/** @var ImageCandidateInterface $imageCandidate */ |
|
260
|
4 |
|
foreach ($imageCandidates as $imageCandidate) { |
|
261
|
4 |
|
if ($imageCandidate->getValue() >= $minImageWidth) { |
|
262
|
4 |
|
return $this->createImageCandidateMatch($sourceSize, $imageCandidate); |
|
263
|
|
|
} |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
1 |
|
return null; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* Compare and sort two source sizes against each other |
|
271
|
|
|
* |
|
272
|
|
|
* @param SourceSize $sourceSize1 Source size 1 |
|
273
|
|
|
* @param SourceSize $sourceSize2 Source size 2 |
|
274
|
|
|
* |
|
275
|
|
|
* @return int Sort order |
|
276
|
|
|
*/ |
|
277
|
4 |
|
protected function sortSourceSizes(SourceSize $sourceSize1, SourceSize $sourceSize2): int |
|
278
|
|
|
{ |
|
279
|
4 |
|
$hasConditions1 = $sourceSize1->hasConditions(); |
|
280
|
4 |
|
$hasConditions2 = $sourceSize2->hasConditions(); |
|
281
|
|
|
|
|
282
|
|
|
// If one of the sources sizes doesn't have conditions: Default source size (move to the end) |
|
283
|
4 |
|
if ($hasConditions1 !== $hasConditions2) { |
|
284
|
3 |
|
return $hasConditions1 ? -1 : 1; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
4 |
|
return $hasConditions1 ? $this->sortSourceSizesByWidth($sourceSize1, $sourceSize2) : 0; |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* Compare and sort two source sizes by width |
|
292
|
|
|
* |
|
293
|
|
|
* @param SourceSize $sourceSize1 Source size 1 |
|
294
|
|
|
* @param SourceSize $sourceSize2 Source size 2 |
|
295
|
|
|
* |
|
296
|
|
|
* @return int Sort order |
|
297
|
|
|
*/ |
|
298
|
4 |
|
protected function sortSourceSizesByWidth(SourceSize $sourceSize1, SourceSize $sourceSize2): int |
|
299
|
|
|
{ |
|
300
|
|
|
// Sort by minimum width |
|
301
|
4 |
|
$minWidth1 = $sourceSize1->getMediaCondition()->getMinimumWidth(); |
|
302
|
4 |
|
$minWidth2 = $sourceSize2->getMediaCondition()->getMinimumWidth(); |
|
303
|
4 |
|
if ($minWidth1 !== $minWidth2) { |
|
304
|
3 |
|
return $this->sortSourceSizesByDifferingValues($minWidth1, $minWidth2); |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
// Sort by maximum width |
|
308
|
2 |
|
$maxWidth1 = $sourceSize1->getMediaCondition()->getMaximumWidth(); |
|
309
|
2 |
|
$maxWidth2 = $sourceSize2->getMediaCondition()->getMaximumWidth(); |
|
310
|
2 |
|
if ($maxWidth1 !== $maxWidth2) { |
|
311
|
1 |
|
return $this->sortSourceSizesByDifferingValues($maxWidth1, $maxWidth2); |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
// Sort by resolution |
|
315
|
1 |
|
return $this->sortSourceSizesByResolution($sourceSize1, $sourceSize2); |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
/** |
|
319
|
|
|
* Sort by differing values |
|
320
|
|
|
* |
|
321
|
|
|
* @param float|null $value1 Value 1 |
|
322
|
|
|
* @param float|null $value2 Value 2 |
|
323
|
|
|
* |
|
324
|
|
|
* @return int Sort order |
|
325
|
|
|
*/ |
|
326
|
4 |
|
protected function sortSourceSizesByDifferingValues(float $value1 = null, float $value2 = null): int |
|
327
|
|
|
{ |
|
328
|
4 |
|
if ($value1 === null) { |
|
329
|
1 |
|
return -1; |
|
330
|
|
|
} |
|
331
|
4 |
|
if ($value2 === null) { |
|
332
|
1 |
|
return 1; |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
4 |
|
return ($value1 > $value2) ? -1 : 1; |
|
336
|
|
|
} |
|
337
|
|
|
|
|
338
|
|
|
/** |
|
339
|
|
|
* Compare and sort two source sizes by resolution |
|
340
|
|
|
* |
|
341
|
|
|
* @param SourceSize $sourceSize1 Source size 1 |
|
342
|
|
|
* @param SourceSize $sourceSize2 Source size 2 |
|
343
|
|
|
* |
|
344
|
|
|
* @return int Sort order |
|
345
|
|
|
*/ |
|
346
|
1 |
|
protected function sortSourceSizesByResolution(SourceSize $sourceSize1, SourceSize $sourceSize2): int |
|
347
|
|
|
{ |
|
348
|
|
|
// Sort by minimum resolution |
|
349
|
1 |
|
$minResolution1 = $sourceSize1->getMediaCondition()->getMinimumResolution(); |
|
350
|
1 |
|
$minResolution2 = $sourceSize2->getMediaCondition()->getMinimumResolution(); |
|
351
|
1 |
|
if ($minResolution1 !== $minResolution2) { |
|
352
|
1 |
|
return $this->sortSourceSizesByDifferingValues($minResolution1, $minResolution2); |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
|
// Sort by maximum resolution |
|
356
|
1 |
|
$maxResolution1 = $sourceSize1->getMediaCondition()->getMaximumResolution(); |
|
357
|
1 |
|
$maxResolution2 = $sourceSize2->getMediaCondition()->getMaximumResolution(); |
|
358
|
1 |
|
if ($maxResolution1 !== $maxResolution2) { |
|
359
|
1 |
|
return $this->sortSourceSizesByDifferingValues($maxResolution1, $maxResolution2); |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
1 |
|
return 0; |
|
363
|
|
|
} |
|
364
|
|
|
} |
|
365
|
|
|
|