1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* responsive-images-css |
5
|
|
|
* |
6
|
|
|
* @category Jkphl |
7
|
|
|
* @package Jkphl\Respimgcss |
8
|
|
|
* @subpackage Jkphl\Respimgcss\Application\Model |
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\Model; |
38
|
|
|
|
39
|
|
|
use Jkphl\Respimgcss\Domain\Contract\AbsoluteLengthInterface; |
40
|
|
|
use Jkphl\Respimgcss\Domain\Contract\CssMinMaxMediaConditionInterface; |
41
|
|
|
use Jkphl\Respimgcss\Domain\Model\Css\ResolutionMediaCondition; |
42
|
|
|
use Jkphl\Respimgcss\Domain\Model\Css\WidthMediaCondition; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Source size media condition |
46
|
|
|
* |
47
|
|
|
* @package Jkphl\Respimgcss |
48
|
|
|
* @subpackage Jkphl\Respimgcss\Application\Model |
49
|
|
|
*/ |
50
|
|
|
class SourceSizeMediaCondition |
51
|
|
|
{ |
52
|
|
|
/** |
53
|
|
|
* Condition properties |
54
|
|
|
* |
55
|
|
|
* @var array |
56
|
|
|
*/ |
57
|
|
|
const CONDITION_PROPERTIES = [ |
58
|
|
|
WidthMediaCondition::class => ['minimumWidth', 'maximumWidth'], |
59
|
|
|
ResolutionMediaCondition::class => ['minimumResolution', 'maximumResolution'], |
60
|
|
|
]; |
61
|
|
|
/** |
62
|
|
|
* Media condition string |
63
|
|
|
* |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
protected $value; |
67
|
|
|
/** |
68
|
|
|
* Size and resolution conditions |
69
|
|
|
* |
70
|
|
|
* @var CssMinMaxMediaConditionInterface[] |
71
|
|
|
*/ |
72
|
|
|
protected $conditions; |
73
|
|
|
/** |
74
|
|
|
* Minimum width |
75
|
|
|
* |
76
|
|
|
* @var int|null |
77
|
|
|
*/ |
78
|
|
|
protected $minimumWidth = null; |
79
|
|
|
/** |
80
|
|
|
* Maximum width |
81
|
|
|
* |
82
|
|
|
* @var int|null |
83
|
|
|
*/ |
84
|
|
|
protected $maximumWidth = null; |
85
|
|
|
/** |
86
|
|
|
* Minimum resolution |
87
|
|
|
* |
88
|
|
|
* @var float|null |
89
|
|
|
*/ |
90
|
|
|
protected $minimumResolution = null; |
91
|
|
|
/** |
92
|
|
|
* Maximum resolution |
93
|
|
|
* |
94
|
|
|
* @var float|null |
95
|
|
|
*/ |
96
|
|
|
protected $maximumResolution = null; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Source size media condition constructor |
100
|
|
|
* |
101
|
|
|
* @param string $value Media condition string |
102
|
|
|
* @param CssMinMaxMediaConditionInterface[] $conditions Size and resolution conditions |
103
|
|
|
*/ |
104
|
17 |
|
public function __construct(string $value, array $conditions = []) |
105
|
|
|
{ |
106
|
17 |
|
$this->value = $value; |
107
|
17 |
|
$this->conditions = $conditions; |
108
|
17 |
|
$this->initialize(); |
109
|
17 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Initialize the minimum / maximum values |
113
|
|
|
*/ |
114
|
17 |
|
protected function initialize(): void |
115
|
|
|
{ |
116
|
|
|
/** @var CssMinMaxMediaConditionInterface $condition */ |
117
|
17 |
|
foreach ($this->conditions as $condition) { |
118
|
14 |
|
if (($condition instanceof CssMinMaxMediaConditionInterface) |
119
|
14 |
|
&& array_key_exists(get_class($condition), self::CONDITION_PROPERTIES) |
120
|
|
|
) { |
121
|
13 |
|
$conditionProperties = self::CONDITION_PROPERTIES[get_class($condition)]; |
122
|
14 |
|
call_user_func([$this, 'initializeCondition'], $condition, ...$conditionProperties); |
123
|
|
|
} |
124
|
|
|
} |
125
|
17 |
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Return the media condition string |
129
|
|
|
* |
130
|
|
|
* @return string Media condition string |
131
|
|
|
*/ |
132
|
4 |
|
public function getValue(): string |
133
|
|
|
{ |
134
|
4 |
|
return $this->value; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Return the size and resolution conditions |
139
|
|
|
* |
140
|
|
|
* @return CssMinMaxMediaConditionInterface[] Size and resolution conditions |
141
|
|
|
*/ |
142
|
9 |
|
public function getConditions(): array |
143
|
|
|
{ |
144
|
9 |
|
return $this->conditions; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Test if this source size condition matches a particular width and density |
149
|
|
|
* |
150
|
|
|
* @param AbsoluteLengthInterface $width Width |
151
|
|
|
* @param float $density Density |
152
|
|
|
* @param int|null $lastMinimumWidth Minimum width of the next higher breakpoint |
153
|
|
|
* |
154
|
|
|
* @return bool This source size condition matches |
155
|
|
|
*/ |
156
|
7 |
|
public function matches(AbsoluteLengthInterface $width, float $density, int $lastMinimumWidth = null): bool |
157
|
|
|
{ |
158
|
7 |
|
$match = true; |
159
|
|
|
|
160
|
|
|
// Run through all conditions |
161
|
|
|
/** @var CssMinMaxMediaConditionInterface $condition */ |
162
|
7 |
|
foreach ($this->conditions as $condition) { |
163
|
7 |
|
if (!($match = $this->testCondition($condition, $width, $density, $lastMinimumWidth))) { |
164
|
7 |
|
break; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
7 |
|
return $match; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Test whether a media condition matches a breakpoint range |
173
|
|
|
* |
174
|
|
|
* @param CssMinMaxMediaConditionInterface $condition Media condition |
175
|
|
|
* @param AbsoluteLengthInterface $width Width |
176
|
|
|
* @param float $density Density |
177
|
|
|
* @param int|null $lastMinimumWidth Minimum width of the next higher breakpoint |
178
|
|
|
* |
179
|
|
|
* @return bool Media condition matches |
180
|
|
|
*/ |
181
|
7 |
|
protected function testCondition( |
182
|
|
|
CssMinMaxMediaConditionInterface $condition, |
183
|
|
|
AbsoluteLengthInterface $width, |
184
|
|
|
float $density, |
185
|
|
|
int $lastMinimumWidth = null |
186
|
|
|
): bool { |
187
|
7 |
|
$rangeLower = ($condition instanceof WidthMediaCondition) ? ($width->getValue() * $density) : $density; |
188
|
7 |
|
$rangeUpper = ($lastMinimumWidth === null) ? $rangeLower : ($lastMinimumWidth * $density - 1); |
189
|
7 |
|
return $condition->matches($rangeLower) && $condition->matches($rangeUpper); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Return the minimum width |
194
|
|
|
* |
195
|
|
|
* @return int|null Minimum width |
196
|
|
|
*/ |
197
|
6 |
|
public function getMinimumWidth(): ?int |
198
|
|
|
{ |
199
|
6 |
|
return $this->minimumWidth; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Return the maximum width |
204
|
|
|
* |
205
|
|
|
* @return int|null Maximum width |
206
|
|
|
*/ |
207
|
8 |
|
public function getMaximumWidth(): ?int |
208
|
|
|
{ |
209
|
8 |
|
return $this->maximumWidth; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Return the minimum resolution |
214
|
|
|
* |
215
|
|
|
* @return float Minimum resolution |
216
|
|
|
*/ |
217
|
3 |
|
public function getMinimumResolution(): ?float |
218
|
|
|
{ |
219
|
3 |
|
return $this->minimumResolution; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Return the maximum resolution |
224
|
|
|
* |
225
|
|
|
* @return float Maximum resolution |
226
|
|
|
*/ |
227
|
3 |
|
public function getMaximumResolution(): ?float |
228
|
|
|
{ |
229
|
3 |
|
return $this->maximumResolution; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Initialize a media condition |
234
|
|
|
* |
235
|
|
|
* @param CssMinMaxMediaConditionInterface $condition Media condition |
236
|
|
|
* @param string $minProperty Minimum property name |
237
|
|
|
* @param string $maxProperty Maximum property name |
238
|
|
|
*/ |
239
|
13 |
|
protected function initializeCondition( |
240
|
|
|
CssMinMaxMediaConditionInterface $condition, |
241
|
|
|
string $minProperty, |
242
|
|
|
string $maxProperty |
243
|
|
|
): void { |
244
|
13 |
|
$modifier = $condition->getModifier(); |
245
|
13 |
|
$value = $condition->getValue()->getValue(); |
246
|
|
|
|
247
|
|
|
// Minimum value |
248
|
13 |
|
if ($modifier == CssMinMaxMediaConditionInterface::MIN) { |
249
|
10 |
|
$this->initializeMinProperty($minProperty, $value); |
250
|
|
|
|
251
|
10 |
|
return; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
// Maximum value |
255
|
7 |
|
if ($modifier == CssMinMaxMediaConditionInterface::MAX) { |
256
|
6 |
|
$this->initializeMaxProperty($maxProperty, $value); |
257
|
|
|
|
258
|
6 |
|
return; |
259
|
|
|
} |
260
|
|
|
|
261
|
3 |
|
$this->$minProperty = $this->$maxProperty = $value; |
262
|
3 |
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Initialize a minimum property |
266
|
|
|
* |
267
|
|
|
* @param string $minProperty Property name |
268
|
|
|
* @param float $value Property Value |
269
|
|
|
*/ |
270
|
10 |
|
protected function initializeMinProperty(string $minProperty, float $value): void |
271
|
|
|
{ |
272
|
10 |
|
$this->$minProperty = ($this->$minProperty === null) ? $value : (min($value, $this->$minProperty)); |
273
|
10 |
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Initialize a maximum property |
277
|
|
|
* |
278
|
|
|
* @param string $maxProperty Property name |
279
|
|
|
* @param float $value Property Value |
280
|
|
|
*/ |
281
|
6 |
|
protected function initializeMaxProperty(string $maxProperty, float $value): void |
282
|
|
|
{ |
283
|
6 |
|
$this->$maxProperty = ($this->$maxProperty === null) ? $value : (max($value, $this->$maxProperty)); |
284
|
6 |
|
} |
285
|
|
|
} |
286
|
|
|
|