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
|
9 |
|
public function __construct(array $sourceSizes, LengthFactoryInterface $lengthFactory) |
79
|
|
|
{ |
80
|
|
|
// Run through all source sizes |
81
|
9 |
|
foreach ($sourceSizes as $sourceSize) { |
82
|
|
|
// If the source size is invalid |
83
|
7 |
|
if (!($sourceSize instanceof SourceSize)) { |
84
|
1 |
|
throw new InvalidArgumentException( |
85
|
1 |
|
InvalidArgumentException::INVALID_SOURCE_SIZE_STR, |
86
|
7 |
|
InvalidArgumentException::INVALID_SOURCE_SIZE |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
8 |
|
usort($sourceSizes, [$this, 'sortSourceSizes']); |
92
|
8 |
|
parent::__construct($sourceSizes); |
93
|
|
|
|
94
|
8 |
|
$this->lengthFactory = $lengthFactory; |
95
|
8 |
|
} |
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
|
4 |
|
public function findImageCandidate( |
107
|
|
|
ImageCandidateSetInterface $imageCandidates, |
108
|
|
|
AbsoluteLengthInterface $breakpoint, |
109
|
|
|
float $density |
110
|
|
|
): ?SourceSizeImageCandidateMatch { |
111
|
|
|
|
112
|
|
|
// Run through the source sizes |
113
|
|
|
/** @var SourceSize $sourceSize */ |
114
|
4 |
|
$lastMinimumWidth = null; |
115
|
4 |
|
foreach ($this->getArrayCopy() as $sourceSize) { |
116
|
3 |
|
$mediaCondition = $sourceSize->getMediaCondition(); |
117
|
3 |
|
if ($mediaCondition->matches($breakpoint, $density)) { |
118
|
3 |
|
return $this->findImageCandidateForSourceSize( |
119
|
3 |
|
$sourceSize, |
120
|
3 |
|
$imageCandidates, |
121
|
3 |
|
$breakpoint, |
122
|
3 |
|
$this->getSourceSizeMaximumWidth($mediaCondition, $lastMinimumWidth) |
123
|
|
|
); |
124
|
|
|
} |
125
|
1 |
|
$lastMinimumWidth = $mediaCondition->getMinimumWidth(); |
126
|
|
|
} |
127
|
|
|
|
128
|
1 |
|
return null; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Find an image candidate for a particular source size |
133
|
|
|
* |
134
|
|
|
* @param SourceSize $sourceSize Matching source size |
135
|
|
|
* @param ImageCandidateSetInterface $imageCandidates Image candidates |
136
|
|
|
* @param AbsoluteLengthInterface $minWidth Minimum viewport width |
137
|
|
|
* @param AbsoluteLengthInterface|null $maxWidth Maximum viewport width |
138
|
|
|
* |
139
|
|
|
* @return SourceSizeImageCandidateMatch|null |
140
|
|
|
*/ |
141
|
3 |
|
protected function findImageCandidateForSourceSize( |
142
|
|
|
SourceSize $sourceSize, |
143
|
|
|
ImageCandidateSetInterface $imageCandidates, |
144
|
|
|
AbsoluteLengthInterface $minWidth, |
145
|
|
|
AbsoluteLengthInterface $maxWidth = null |
146
|
|
|
): ?SourceSizeImageCandidateMatch { |
147
|
|
|
// If there's no upper limit: Use the largest image candidate in any case |
148
|
3 |
|
if ($maxWidth === null) { |
149
|
2 |
|
return $this->createLargestImageCandidateMatch($sourceSize, $imageCandidates); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
// Run through all image candidates for the effective minimum image, current source size and current breakpoint |
153
|
2 |
|
return $this->findImageCandidateForMinImageWidth( |
154
|
2 |
|
$sourceSize, |
155
|
2 |
|
$imageCandidates, |
156
|
2 |
|
max($sourceSize->getValue()->getValue($minWidth), $sourceSize->getValue()->getValue($maxWidth)) |
|
|
|
|
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Create and return a match for the largest image candidate |
162
|
|
|
* |
163
|
|
|
* @param SourceSize $sourceSize Source size |
164
|
|
|
* @param ImageCandidateSetInterface $imageCandidates Image candidates |
165
|
|
|
* |
166
|
|
|
* @return SourceSizeImageCandidateMatch|null Largest image candidate match |
167
|
|
|
*/ |
168
|
2 |
|
protected function createLargestImageCandidateMatch( |
169
|
|
|
SourceSize $sourceSize, |
170
|
|
|
ImageCandidateSetInterface $imageCandidates |
171
|
|
|
): ?SourceSizeImageCandidateMatch { |
172
|
2 |
|
if (count($imageCandidates)) { |
173
|
1 |
|
$largestImageCandidate = $imageCandidates[count($imageCandidates) - 1]; |
174
|
1 |
|
return $this->createImageCandidateMatch($sourceSize, $largestImageCandidate); |
175
|
|
|
} |
176
|
|
|
|
177
|
1 |
|
return null; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Create a source size image candidate match |
182
|
|
|
* |
183
|
|
|
* @param SourceSize $sourceSize Matching source size |
184
|
|
|
* @param ImageCandidateInterface $imageCandidate Image candidata |
185
|
|
|
* |
186
|
|
|
* @return SourceSizeImageCandidateMatch Source size image candidate match |
187
|
|
|
*/ |
188
|
1 |
|
protected function createImageCandidateMatch( |
189
|
|
|
SourceSize $sourceSize, |
190
|
|
|
ImageCandidateInterface $imageCandidate |
191
|
|
|
): SourceSizeImageCandidateMatch { |
192
|
1 |
|
$mediaCondition = new MediaCondition('', $sourceSize->getMediaCondition()->getValue()); |
193
|
|
|
|
194
|
1 |
|
return new ImageCandidateMatch($mediaCondition, $imageCandidate); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Find an image candidate for a particular source size |
199
|
|
|
* |
200
|
|
|
* @param SourceSize $sourceSize Matching source size |
201
|
|
|
* @param ImageCandidateSetInterface $imageCandidates Image candidates |
202
|
|
|
* @param int $minImageWidth Minimum image width |
203
|
|
|
* |
204
|
|
|
* @return SourceSizeImageCandidateMatch|null Image candidate |
205
|
|
|
*/ |
206
|
2 |
|
protected function findImageCandidateForMinImageWidth( |
207
|
|
|
SourceSize $sourceSize, |
208
|
|
|
ImageCandidateSetInterface $imageCandidates, |
209
|
|
|
int $minImageWidth |
210
|
|
|
): ?SourceSizeImageCandidateMatch { |
211
|
|
|
// Run through all image candidates |
212
|
|
|
/** @var ImageCandidateInterface $imageCandidate */ |
213
|
2 |
|
foreach ($imageCandidates as $imageCandidate) { |
214
|
2 |
|
if ($imageCandidate->getValue() >= $minImageWidth) { |
215
|
2 |
|
return $this->createImageCandidateMatch($sourceSize, $imageCandidate); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
219
|
1 |
|
return null; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Get the maximum width for a source size |
224
|
|
|
* |
225
|
|
|
* @param SourceSizeMediaCondition $condition Source size media condition |
226
|
|
|
* @param float|null $lastMinimumWidth Last source size minimum width |
227
|
|
|
* |
228
|
|
|
* @return AbsoluteLengthInterface Maximum width |
229
|
|
|
*/ |
230
|
3 |
|
protected function getSourceSizeMaximumWidth( |
231
|
|
|
SourceSizeMediaCondition $condition, |
232
|
|
|
float $lastMinimumWidth = null |
233
|
|
|
): ?AbsoluteLengthInterface { |
234
|
3 |
|
$maximumWidth = $this->considerLastMinimumWidth($condition->getMaximumWidth(), $lastMinimumWidth); |
235
|
3 |
|
if ($maximumWidth === null) { |
236
|
2 |
|
return null; |
237
|
|
|
} |
238
|
2 |
|
if ($lastMinimumWidth !== null) { |
239
|
1 |
|
$maximumWidth = max(0, min($maximumWidth, $lastMinimumWidth - 1)); |
240
|
|
|
} |
241
|
2 |
|
return $this->lengthFactory->createAbsoluteLength($maximumWidth); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Consider the last minimum width in case the maximum width is undefined |
246
|
|
|
* |
247
|
|
|
* @param int|null $maximumWidth Maximum width |
248
|
|
|
* @param float|null $lastMinimumWidth Last minimum width |
249
|
|
|
* |
250
|
|
|
* @return float|null Maximum width |
251
|
|
|
*/ |
252
|
3 |
|
protected function considerLastMinimumWidth(int $maximumWidth = null, float $lastMinimumWidth = null): ?float |
253
|
|
|
{ |
254
|
3 |
|
if (($maximumWidth === null) && ($lastMinimumWidth !== null)) { |
255
|
1 |
|
$maximumWidth = max(0, $lastMinimumWidth - 1); |
256
|
|
|
} |
257
|
3 |
|
return $maximumWidth; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Compare and sort two source sizes against each other |
262
|
|
|
* |
263
|
|
|
* @param SourceSize $sourceSize1 Source size 1 |
264
|
|
|
* @param SourceSize $sourceSize2 Source size 2 |
265
|
|
|
* |
266
|
|
|
* @return int Sort order |
267
|
|
|
*/ |
268
|
2 |
|
protected function sortSourceSizes(SourceSize $sourceSize1, SourceSize $sourceSize2): int |
269
|
|
|
{ |
270
|
2 |
|
$hasConditions1 = $sourceSize1->hasConditions(); |
271
|
2 |
|
$hasConditions2 = $sourceSize2->hasConditions(); |
272
|
|
|
|
273
|
|
|
// If one of the sources sizes doesn't have conditions: Default source size (move to the end) |
274
|
2 |
|
if ($hasConditions1 !== $hasConditions2) { |
275
|
1 |
|
return $hasConditions1 ? -1 : 1; |
276
|
|
|
} |
277
|
|
|
|
278
|
2 |
|
return $hasConditions1 ? $this->sortSourceSizesByWidth($sourceSize1, $sourceSize2) : 0; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Compare and sort two source sizes by width |
283
|
|
|
* |
284
|
|
|
* @param SourceSize $sourceSize1 Source size 1 |
285
|
|
|
* @param SourceSize $sourceSize2 Source size 2 |
286
|
|
|
* |
287
|
|
|
* @return int Sort order |
288
|
|
|
*/ |
289
|
2 |
|
protected function sortSourceSizesByWidth(SourceSize $sourceSize1, SourceSize $sourceSize2): int |
290
|
|
|
{ |
291
|
|
|
// Sort by minimum width |
292
|
2 |
|
$minWidth1 = $sourceSize1->getMediaCondition()->getMinimumWidth(); |
293
|
2 |
|
$minWidth2 = $sourceSize2->getMediaCondition()->getMinimumWidth(); |
294
|
2 |
|
if ($minWidth1 !== $minWidth2) { |
295
|
1 |
|
return $this->sortSourceSizesByDifferingValues($minWidth1, $minWidth2); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
// Sort by maximum width |
299
|
2 |
|
$maxWidth1 = $sourceSize1->getMediaCondition()->getMaximumWidth(); |
300
|
2 |
|
$maxWidth2 = $sourceSize2->getMediaCondition()->getMaximumWidth(); |
301
|
2 |
|
if ($maxWidth1 !== $maxWidth2) { |
302
|
1 |
|
return $this->sortSourceSizesByDifferingValues($maxWidth1, $maxWidth2); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
// Sort by resolution |
306
|
1 |
|
return $this->sortSourceSizesByResolution($sourceSize1, $sourceSize2); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Sort by differing values |
311
|
|
|
* |
312
|
|
|
* @param float|null $value1 Value 1 |
313
|
|
|
* @param float|null $value2 Value 2 |
314
|
|
|
* |
315
|
|
|
* @return int Sort order |
316
|
|
|
*/ |
317
|
2 |
|
protected function sortSourceSizesByDifferingValues(float $value1 = null, float $value2 = null): int |
318
|
|
|
{ |
319
|
2 |
|
if ($value1 === null) { |
320
|
1 |
|
return -1; |
321
|
|
|
} |
322
|
2 |
|
if ($value2 === null) { |
323
|
1 |
|
return 1; |
324
|
|
|
} |
325
|
2 |
|
return ($value1 > $value2) ? -1 : 1; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Compare and sort two source sizes by resolution |
330
|
|
|
* |
331
|
|
|
* @param SourceSize $sourceSize1 Source size 1 |
332
|
|
|
* @param SourceSize $sourceSize2 Source size 2 |
333
|
|
|
* |
334
|
|
|
* @return int Sort order |
335
|
|
|
*/ |
336
|
1 |
|
protected function sortSourceSizesByResolution(SourceSize $sourceSize1, SourceSize $sourceSize2): int |
337
|
|
|
{ |
338
|
|
|
// Sort by minimum resolution |
339
|
1 |
|
$minResolution1 = $sourceSize1->getMediaCondition()->getMinimumResolution(); |
340
|
1 |
|
$minResolution2 = $sourceSize2->getMediaCondition()->getMinimumResolution(); |
341
|
1 |
|
if ($minResolution1 !== $minResolution2) { |
342
|
1 |
|
return $this->sortSourceSizesByDifferingValues($minResolution1, $minResolution2); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
// Sort by maximum resolution |
346
|
1 |
|
$maxResolution1 = $sourceSize1->getMediaCondition()->getMaximumResolution(); |
347
|
1 |
|
$maxResolution2 = $sourceSize2->getMediaCondition()->getMaximumResolution(); |
348
|
1 |
|
if ($maxResolution1 !== $maxResolution2) { |
349
|
1 |
|
return $this->sortSourceSizesByDifferingValues($maxResolution1, $maxResolution2); |
350
|
|
|
} |
351
|
|
|
|
352
|
1 |
|
return 0; |
353
|
|
|
} |
354
|
|
|
} |