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\Domain\Contract\AbsoluteLengthInterface; |
43
|
|
|
use Jkphl\Respimgcss\Domain\Contract\ImageCandidateInterface; |
44
|
|
|
use Jkphl\Respimgcss\Domain\Contract\ImageCandidateSetInterface; |
45
|
|
|
use Jkphl\Respimgcss\Domain\Contract\SourceSizeImageCandidateMatch; |
46
|
|
|
use Jkphl\Respimgcss\Domain\Model\Css\MediaCondition; |
47
|
|
|
use Jkphl\Respimgcss\Domain\Model\ImageCandidateMatch; |
48
|
|
|
use Jkphl\Respimgcss\Ports\InvalidArgumentException; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Sizes list |
52
|
|
|
* |
53
|
|
|
* @package Jkphl\Respimgcss |
54
|
|
|
* @subpackage Jkphl\Respimgcss\Infrastructure |
55
|
|
|
* @see http://w3c.github.io/html/semantics-embedded-content.html#ref-for-viewport-based-selection%E2%91%A0 |
56
|
|
|
* @see http://w3c.github.io/html/semantics-embedded-content.html#valid-source-size-list |
57
|
|
|
* @see http://w3c.github.io/html/semantics-embedded-content.html#parse-a-sizes-attribute |
58
|
|
|
*/ |
59
|
|
|
class SourceSizeList extends \ArrayObject implements SourceSizeListInterface |
60
|
|
|
{ |
61
|
|
|
/** |
62
|
|
|
* Length factory |
63
|
|
|
* |
64
|
|
|
* @var LengthFactoryInterface |
65
|
|
|
*/ |
66
|
|
|
protected $lengthFactory; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Source size list constructor |
70
|
|
|
* |
71
|
|
|
* @param SourceSize[] $sourceSizes Source sizes |
72
|
|
|
* |
73
|
|
|
* @throws InvalidArgumentException If the source size is invalid |
74
|
|
|
*/ |
75
|
5 |
|
public function __construct(array $sourceSizes) |
76
|
|
|
{ |
77
|
|
|
// Run through all source sizes |
78
|
5 |
|
foreach ($sourceSizes as $sourceSize) { |
79
|
|
|
// If the source size is invalid |
80
|
4 |
|
if (!($sourceSize instanceof SourceSize)) { |
81
|
1 |
|
throw new InvalidArgumentException( |
82
|
1 |
|
InvalidArgumentException::INVALID_SOURCE_SIZE_STR, |
83
|
4 |
|
InvalidArgumentException::INVALID_SOURCE_SIZE |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
} |
87
|
4 |
|
parent::__construct($this->sortSourceSizes($sourceSizes)); |
88
|
4 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Sort the list of source sizes |
92
|
|
|
* |
93
|
|
|
* @param SourceSize[] $sourceSizes Source sizes |
94
|
|
|
* |
95
|
|
|
* @return SourceSize[] Sorted source sizes |
96
|
|
|
*/ |
97
|
4 |
|
protected function sortSourceSizes(array $sourceSizes): array |
98
|
|
|
{ |
99
|
4 |
|
$sourceSizesCount = count($sourceSizes); |
100
|
4 |
|
if ($sourceSizesCount) { |
101
|
3 |
|
$defaultSize = strlen($sourceSizes[$sourceSizesCount - 1]->getMediaCondition()->getValue()) ? |
102
|
3 |
|
null : array_pop($sourceSizes); |
103
|
3 |
|
$sourceSizes = array_reverse($sourceSizes); |
104
|
3 |
|
if ($defaultSize) { |
105
|
1 |
|
array_push($sourceSizes, $defaultSize); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
4 |
|
return $sourceSizes; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Find the optimum image candidate for a particular breakpoint |
114
|
|
|
* |
115
|
|
|
* @param ImageCandidateSetInterface $imageCandidates Image candidates |
116
|
|
|
* @param AbsoluteLengthInterface $breakpoint Breakpoint |
117
|
|
|
* @param float $density Density |
118
|
|
|
* |
119
|
|
|
* @return SourceSizeImageCandidateMatch|null Image candidate match |
120
|
|
|
*/ |
121
|
1 |
|
public function findImageCandidate( |
122
|
|
|
ImageCandidateSetInterface $imageCandidates, |
123
|
|
|
AbsoluteLengthInterface $breakpoint, |
124
|
|
|
float $density |
125
|
|
|
): ?SourceSizeImageCandidateMatch { |
126
|
|
|
|
127
|
|
|
// Run through the source sizes |
128
|
|
|
/** @var SourceSize $sourceSize */ |
129
|
1 |
|
foreach ($this->getArrayCopy() as $sourceSize) { |
130
|
1 |
|
if ($sourceSize->getMediaCondition()->matches($breakpoint, $density)) { |
131
|
1 |
|
return $this->findImageCandidateForSourceSize($sourceSize, $breakpoint, $imageCandidates); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return null; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Find an image candidate for a particular source size |
140
|
|
|
* |
141
|
|
|
* @param SourceSize $sourceSize Matching source size |
142
|
|
|
* @param AbsoluteLengthInterface $breakpoint Breakpoint |
143
|
|
|
* @param ImageCandidateSetInterface $imageCandidates Image candidates |
144
|
|
|
* |
145
|
|
|
* @return SourceSizeImageCandidateMatch|null |
146
|
|
|
*/ |
147
|
1 |
|
protected function findImageCandidateForSourceSize( |
148
|
|
|
SourceSize $sourceSize, |
149
|
|
|
AbsoluteLengthInterface $breakpoint, |
150
|
|
|
ImageCandidateSetInterface $imageCandidates |
151
|
|
|
): ?SourceSizeImageCandidateMatch { |
152
|
|
|
// Calculate the effective image width for the current source size and breakpoint |
153
|
1 |
|
$minImageWidth = $sourceSize->getValue()->getValue($breakpoint); |
|
|
|
|
154
|
|
|
|
155
|
|
|
// Run through all image candidates |
156
|
|
|
/** @var ImageCandidateInterface $imageCandidate */ |
157
|
1 |
|
foreach ($imageCandidates as $imageCandidate) { |
158
|
1 |
|
if ($imageCandidate->getValue() >= $minImageWidth) { |
159
|
1 |
|
return $this->createImageCandidateMatch($sourceSize, $imageCandidate); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return null; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Create a source size image candidate match |
168
|
|
|
* |
169
|
|
|
* @param SourceSize $sourceSize Matching source size |
170
|
|
|
* @param ImageCandidateInterface $imageCandidate Image candidata |
171
|
|
|
* |
172
|
|
|
* @return SourceSizeImageCandidateMatch Source size image candidate match |
173
|
|
|
*/ |
174
|
1 |
|
protected function createImageCandidateMatch( |
175
|
|
|
SourceSize $sourceSize, |
176
|
|
|
ImageCandidateInterface $imageCandidate |
177
|
|
|
): SourceSizeImageCandidateMatch { |
178
|
1 |
|
$mediaCondition = new MediaCondition('', $sourceSize->getMediaCondition()->getValue()); |
179
|
|
|
|
180
|
1 |
|
return new ImageCandidateMatch($mediaCondition, $imageCandidate); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Inject a length factory |
185
|
|
|
* |
186
|
|
|
* @param LengthFactoryInterface $lengthFactory Length factory |
187
|
|
|
*/ |
188
|
1 |
|
public function setLengthFactory(LengthFactoryInterface $lengthFactory): void |
189
|
|
|
{ |
190
|
1 |
|
$this->lengthFactory = $lengthFactory; |
191
|
|
|
} |
192
|
|
|
} |