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\Infrastructure; |
38
|
|
|
|
39
|
|
|
use ChrisKonnertz\StringCalc\Container\ContainerInterface; |
40
|
|
|
use ChrisKonnertz\StringCalc\StringCalc; |
41
|
|
|
use ChrisKonnertz\StringCalc\Tokenizer\Token; |
42
|
|
|
use Jkphl\Respimgcss\Application\Contract\CalculatorServiceInterface; |
43
|
|
|
use Jkphl\Respimgcss\Application\Contract\UnitLengthInterface; |
44
|
|
|
use Jkphl\Respimgcss\Application\Exceptions\InvalidArgumentException as ApplicationInvalidArgumentException; |
45
|
|
|
use Jkphl\Respimgcss\Application\Factory\LengthFactory; |
46
|
|
|
use Jkphl\Respimgcss\Application\Model\AbsoluteLength; |
47
|
|
|
use Jkphl\Respimgcss\Domain\Contract\AbsoluteLengthInterface; |
48
|
|
|
use Jkphl\Respimgcss\Ports\InvalidArgumentException; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Custom string calculator |
52
|
|
|
* |
53
|
|
|
* @package Jkphl\Respimgcss |
54
|
|
|
* @subpackage Jkphl\Respimgcss\Application\Model |
55
|
|
|
*/ |
56
|
|
|
class ViewportCalculatorService extends StringCalc implements CalculatorServiceInterface |
57
|
|
|
{ |
58
|
|
|
/** |
59
|
|
|
* Custom string calculator constructor |
60
|
|
|
* |
61
|
|
|
* @param AbsoluteLengthInterface $viewport Viewport width |
62
|
|
|
* @param ContainerInterface $container Container |
63
|
|
|
* |
64
|
|
|
* @throws \ChrisKonnertz\StringCalc\Exceptions\ContainerException |
65
|
|
|
* @throws \ChrisKonnertz\StringCalc\Exceptions\InvalidIdentifierException |
66
|
|
|
* @throws \ChrisKonnertz\StringCalc\Exceptions\NotFoundException |
67
|
|
|
*/ |
68
|
16 |
|
public function __construct(AbsoluteLengthInterface $viewport = null) |
69
|
|
|
{ |
70
|
16 |
|
parent::__construct(); |
71
|
16 |
|
$stringHelper = $this->getContainer()->get('stringcalc_stringhelper'); |
72
|
16 |
|
$viewportFunction = new ViewportFunction( |
73
|
16 |
|
$stringHelper, |
74
|
16 |
|
$viewport ?: (new LengthFactory(new ViewportCalculatorServiceFactory(), 16))->createAbsoluteLength(0) |
75
|
|
|
); |
76
|
16 |
|
$this->symbolContainer->add($viewportFunction); |
77
|
16 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Evaluate calculation tokens |
81
|
|
|
* |
82
|
|
|
* @param Token[] $tokens Calculation tokens |
83
|
|
|
* |
84
|
|
|
* @return float Result |
85
|
|
|
* @throws \ChrisKonnertz\StringCalc\Exceptions\ContainerException |
86
|
|
|
* @throws \ChrisKonnertz\StringCalc\Exceptions\NotFoundException |
87
|
|
|
*/ |
88
|
6 |
|
public function evaluate(array $tokens): float |
89
|
|
|
{ |
90
|
6 |
|
$calculationRootNode = $this->parse($tokens); |
91
|
|
|
|
92
|
5 |
|
return $this->container->get('stringcalc_calculator')->calculate($calculationRootNode); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Refine a list of calculation tokens |
97
|
|
|
* |
98
|
|
|
* @param array $tokens Calculation tokens |
99
|
|
|
* @param int $emPixel EM to pixel ratio |
100
|
|
|
* |
101
|
|
|
* @return array Refined Calculation tokens |
102
|
|
|
*/ |
103
|
10 |
|
public function refineCalculationTokens(array $tokens, int $emPixel): array |
104
|
|
|
{ |
105
|
10 |
|
$refinedTokens = []; |
106
|
10 |
|
$previousToken = null; |
107
|
|
|
|
108
|
|
|
// Run through all tokens |
109
|
10 |
|
foreach ($tokens as $token) { |
110
|
10 |
|
$previousToken = $this->handleToken($refinedTokens, $emPixel, $token, $previousToken); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// Add the last token |
114
|
9 |
|
if ($previousToken) { |
115
|
1 |
|
array_push($refinedTokens, $previousToken); |
116
|
|
|
} |
117
|
|
|
|
118
|
9 |
|
return $refinedTokens; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Handle a particular token |
123
|
|
|
* |
124
|
|
|
* @param Token[] $refinedTokens Refined tokens |
125
|
|
|
* @param int $emPixel EM to pixel ratio |
126
|
|
|
* @param Token $token Token |
127
|
|
|
* @param Token|null $previousToken Previous token |
128
|
|
|
* |
129
|
|
|
* @return Token|null Stash token |
130
|
|
|
*/ |
131
|
10 |
|
protected function handleToken( |
132
|
|
|
array &$refinedTokens, |
133
|
|
|
int $emPixel, |
134
|
|
|
Token $token, |
135
|
|
|
Token $previousToken = null |
136
|
|
|
): ?Token { |
137
|
|
|
// If it's a word token: Handle individually |
138
|
10 |
|
if ($token->getType() == Token::TYPE_WORD) { |
139
|
9 |
|
return $this->handleWordToken($refinedTokens, $emPixel, $token, $previousToken); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
// Handle as simple token |
143
|
10 |
|
return $this->handleSimpleToken($refinedTokens, $token, $previousToken); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Handle a simple token |
148
|
|
|
* |
149
|
|
|
* @param Token[] $refinedTokens Refined tokens |
150
|
|
|
* @param Token $token Token |
151
|
|
|
* @param Token|null $previousToken Previous token |
152
|
|
|
* |
153
|
|
|
* @return Token|null Stash token |
154
|
|
|
*/ |
155
|
10 |
|
protected function handleSimpleToken( |
156
|
|
|
array &$refinedTokens, |
157
|
|
|
Token $token, |
158
|
|
|
Token $previousToken = null |
159
|
|
|
): ?Token { |
160
|
|
|
// In all other cases: Register the previous token (if any) |
161
|
10 |
|
if ($previousToken) { |
162
|
2 |
|
array_push($refinedTokens, $previousToken); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
// If it's a number token: Stash |
166
|
10 |
|
if ($token->getType() == Token::TYPE_NUMBER) { |
167
|
10 |
|
return $token; |
168
|
|
|
} |
169
|
|
|
|
170
|
9 |
|
array_push($refinedTokens, $token); |
171
|
|
|
|
172
|
9 |
|
return null; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Handle a particular token |
177
|
|
|
* |
178
|
|
|
* The method returns a list of zero or more (possibly refined) tokens to preerve |
179
|
|
|
* |
180
|
|
|
* @param Token[] $refinedTokens Refined tokens |
181
|
|
|
* @param int $emPixel EM to pixel ratio |
182
|
|
|
* @param Token $token Token |
183
|
|
|
* @param Token|null $previousToken Previous token |
184
|
|
|
* |
185
|
|
|
* @return Token|null Stash token |
186
|
|
|
*/ |
187
|
9 |
|
protected function handleWordToken( |
188
|
|
|
array &$refinedTokens, |
189
|
|
|
int $emPixel, |
190
|
|
|
Token $token, |
191
|
|
|
Token $previousToken = null |
192
|
|
|
): ?Token { |
193
|
|
|
// If it's a calc() function call: Add the previous token and skip the current one |
194
|
9 |
|
if ($token->getValue() == 'calc') { |
195
|
8 |
|
if ($previousToken) { |
196
|
1 |
|
array_push($refinedTokens, $previousToken); |
197
|
|
|
} |
198
|
|
|
|
199
|
8 |
|
return null; |
200
|
|
|
} |
201
|
|
|
|
202
|
8 |
|
$this->handleUnitWordToken($refinedTokens, $emPixel, $token, $previousToken); |
203
|
|
|
|
204
|
7 |
|
return null; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Handle a unit word token |
209
|
|
|
* |
210
|
|
|
* The method returns a list of zero or more (possibly refined) tokens to preerve |
211
|
|
|
* |
212
|
|
|
* @param Token[] $refinedTokens Refined tokens |
213
|
|
|
* @param int $emPixel EM to pixel ratio |
214
|
|
|
* @param Token $token Token |
215
|
|
|
* @param Token|null $previousToken Previous token |
216
|
|
|
* |
217
|
|
|
* @throws InvalidArgumentException If the word token is invalid |
218
|
|
|
*/ |
219
|
8 |
|
protected function handleUnitWordToken( |
220
|
|
|
array &$refinedTokens, |
221
|
|
|
int $emPixel, |
222
|
|
|
Token $token, |
223
|
|
|
Token $previousToken = null |
224
|
|
|
): void { |
225
|
|
|
// If the previous token is a number: Try to generate a unit length |
226
|
8 |
|
if ($previousToken && ($previousToken->getType() == Token::TYPE_NUMBER)) { |
227
|
|
|
try { |
228
|
8 |
|
$this->createAndHandleUnitLengthToken($refinedTokens, $emPixel, $token, $previousToken); |
229
|
|
|
|
230
|
7 |
|
return; |
231
|
1 |
|
} catch (ApplicationInvalidArgumentException $e) { |
232
|
|
|
// Ignore |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
// Invalid word token |
237
|
1 |
|
throw new InvalidArgumentException( |
238
|
1 |
|
sprintf(InvalidArgumentException::INVALID_WORD_TOKEN_IN_SOURCE_SIZE_VALUE_STR, $token->getValue()), |
239
|
1 |
|
InvalidArgumentException::INVALID_WORD_TOKEN_IN_SOURCE_SIZE_VALUE |
240
|
|
|
); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Create and handle a unit length token |
246
|
|
|
* |
247
|
|
|
* @param Token[] $refinedTokens Refined tokens |
248
|
|
|
* @param int $emPixel EM to pixel ratio |
249
|
|
|
* @param Token $token Token |
250
|
|
|
* @param Token $previousToken Previous token |
251
|
|
|
*/ |
252
|
8 |
|
protected function createAndHandleUnitLengthToken( |
253
|
|
|
array &$refinedTokens, |
254
|
|
|
int $emPixel, |
255
|
|
|
Token $token, |
256
|
|
|
Token $previousToken |
257
|
|
|
): void { |
258
|
8 |
|
$unitLength = (new LengthFactory(new ViewportCalculatorServiceFactory(), $emPixel)) |
259
|
8 |
|
->createLengthFromString($previousToken->getValue().$token->getValue()); |
260
|
|
|
|
261
|
|
|
// If it's an absolute length value |
262
|
7 |
|
if ($unitLength instanceof AbsoluteLength) { |
263
|
7 |
|
$lengthValue = strval($unitLength->/** @scrutinizer ignore-call */ |
264
|
7 |
|
getValue()); |
265
|
7 |
|
array_push($refinedTokens, new Token($lengthValue, Token::TYPE_NUMBER, 0)); |
266
|
|
|
|
267
|
7 |
|
return; |
268
|
|
|
} |
269
|
|
|
|
270
|
4 |
|
$this->handleUnitLengthToken($refinedTokens, $unitLength); |
271
|
4 |
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Handle a unit length token |
275
|
|
|
* |
276
|
|
|
* @param Token[] $refinedTokens Refined tokens |
277
|
|
|
* @param UnitLengthInterface $unitLength Unit length |
278
|
|
|
*/ |
279
|
4 |
|
protected function handleUnitLengthToken( |
280
|
|
|
array &$refinedTokens, |
281
|
|
|
UnitLengthInterface $unitLength |
282
|
|
|
): void { |
283
|
4 |
|
array_push( |
284
|
4 |
|
$refinedTokens, |
285
|
4 |
|
new Token('(', Token::TYPE_CHARACTER, 0), |
286
|
4 |
|
new Token(strval($unitLength->getOriginalValue() / 100), Token::TYPE_NUMBER, 0), |
287
|
4 |
|
new Token('*', Token::TYPE_CHARACTER, 0), |
288
|
4 |
|
new Token('viewport', Token::TYPE_WORD, 0), |
289
|
4 |
|
new Token('(', Token::TYPE_CHARACTER, 0), |
290
|
4 |
|
new Token(')', Token::TYPE_CHARACTER, 0), |
291
|
4 |
|
new Token(')', Token::TYPE_CHARACTER, 0) |
292
|
|
|
); |
293
|
4 |
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Test whether a calculation token is a viewport token |
297
|
|
|
* |
298
|
|
|
* @param mixed $token Calculation token |
299
|
|
|
* |
300
|
|
|
* @return bool Is viewport token |
301
|
|
|
*/ |
302
|
7 |
|
public function isViewportToken($token): bool |
303
|
|
|
{ |
304
|
7 |
|
return ($token instanceof Token) |
305
|
7 |
|
&& ($token->getType() == Token::TYPE_WORD) |
306
|
7 |
|
&& ($token->getValue() === 'viewport'); |
307
|
|
|
} |
308
|
|
|
} |