1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* responsive-images-css |
5
|
|
|
* |
6
|
|
|
* @category Jkphl |
7
|
|
|
* @package Jkphl\Respimgcss |
8
|
|
|
* @subpackage Jkphl\Respimgcss\Application\Factory |
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\Factory; |
38
|
|
|
|
39
|
|
|
use ChrisKonnertz\StringCalc\Parser\Nodes\ContainerNode; |
40
|
|
|
use ChrisKonnertz\StringCalc\Tokenizer\Token; |
41
|
|
|
use Jkphl\Respimgcss\Application\Contract\UnitLengthInterface; |
42
|
|
|
use Jkphl\Respimgcss\Application\Exceptions\InvalidArgumentException; |
43
|
|
|
use Jkphl\Respimgcss\Application\Model\AbsoluteLength; |
44
|
|
|
use Jkphl\Respimgcss\Application\Model\StringCalculator; |
45
|
|
|
use Jkphl\Respimgcss\Application\Model\ViewportLength; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* AbstractLength factory for calc() based values |
49
|
|
|
* |
50
|
|
|
* @package Jkphl\Respimgcss |
51
|
|
|
* @subpackage Jkphl\Respimgcss\Application\Factory |
52
|
|
|
*/ |
53
|
|
|
class CalcLengthFactory extends AbstractLengthFactory |
54
|
|
|
{ |
55
|
|
|
/** |
56
|
|
|
* Create a unit length from a calc() size string |
57
|
|
|
* |
58
|
|
|
* @param string $calcString calc() size string |
59
|
|
|
* |
60
|
|
|
* @return UnitLengthInterface |
61
|
|
|
* @throws \ChrisKonnertz\StringCalc\Exceptions\ContainerException |
62
|
|
|
* @throws \ChrisKonnertz\StringCalc\Exceptions\InvalidIdentifierException |
63
|
|
|
* @throws \ChrisKonnertz\StringCalc\Exceptions\NotFoundException |
64
|
|
|
*/ |
65
|
|
|
public function createFromString(string $calcString): UnitLengthInterface |
66
|
|
|
{ |
67
|
|
|
// If the calc() string is ill-formatted |
68
|
|
|
if (!preg_match('/^calc\(.+\)$/', $calcString)) { |
69
|
|
|
throw new InvalidArgumentException( |
70
|
|
|
sprintf(InvalidArgumentException::ILL_FORMATTED_CALC_LENGTH_STRING_STR, $calcString), |
71
|
|
|
InvalidArgumentException::ILL_FORMATTED_CALC_LENGTH_STRING |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this->createCalculationContainerFromString($calcString); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Parse a calculation string and return a precompiled calculation node container |
80
|
|
|
* |
81
|
|
|
* @param string $calcString Calculation string |
82
|
|
|
* |
83
|
|
|
* @return ContainerNode Calculation node container |
84
|
|
|
* @throws \ChrisKonnertz\StringCalc\Exceptions\ContainerException |
85
|
|
|
* @throws \ChrisKonnertz\StringCalc\Exceptions\InvalidIdentifierException |
86
|
|
|
* @throws \ChrisKonnertz\StringCalc\Exceptions\NotFoundException |
87
|
|
|
*/ |
88
|
|
|
protected function createCalculationContainerFromString(string $calcString): UnitLengthInterface |
89
|
|
|
{ |
90
|
|
|
$stringCalc = new StringCalculator(); |
91
|
|
|
$calcTokens = $stringCalc->tokenize($calcString); |
92
|
|
|
$refinedTokens = $this->refineCalculationTokens($calcTokens); |
93
|
|
|
|
94
|
|
|
// If there's the viewport involved in the calculation: Create a relative calculated length |
95
|
|
|
/** @var Token $token */ |
96
|
|
|
foreach ($refinedTokens as $token) { |
97
|
|
|
if (($token->getType() == Token::TYPE_WORD) && ($token->getValue() === 'viewport')) { |
98
|
|
|
return new ViewportLength( |
|
|
|
|
99
|
|
|
$stringCalc->parse($refinedTokens), |
100
|
|
|
$this->lengthNormalizerService, |
101
|
|
|
$calcString |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// Create and return an absolute length |
107
|
|
|
return new AbsoluteLength( |
|
|
|
|
108
|
|
|
$stringCalc->calculate($refinedTokens), |
|
|
|
|
109
|
|
|
UnitLengthInterface::UNIT_PIXEL, |
110
|
|
|
$this->lengthNormalizerService |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Refine a list of symbol tokens |
116
|
|
|
* |
117
|
|
|
* @param Token[] $tokens Symbol tokens |
118
|
|
|
* |
119
|
|
|
* @return Token[] Refined symbol tokens |
120
|
|
|
* @throws \ChrisKonnertz\StringCalc\Exceptions\ContainerException |
121
|
|
|
* @throws \ChrisKonnertz\StringCalc\Exceptions\InvalidIdentifierException |
122
|
|
|
* @throws \ChrisKonnertz\StringCalc\Exceptions\NotFoundException |
123
|
|
|
*/ |
124
|
|
|
protected function refineCalculationTokens(array $tokens): array |
125
|
|
|
{ |
126
|
|
|
$refinedTokens = []; |
127
|
|
|
$previousToken = null; |
128
|
|
|
|
129
|
|
|
// Run through all tokens |
130
|
|
|
foreach ($tokens as $token) { |
131
|
|
|
$previousToken = $this->handleToken($refinedTokens, $token, $previousToken); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
// Add the last token |
135
|
|
|
if ($previousToken) { |
136
|
|
|
array_push($refinedTokens, $previousToken); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $refinedTokens; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Handle a particular token |
144
|
|
|
* |
145
|
|
|
* @param Token[] $refinedTokens Refined tokens |
146
|
|
|
* @param Token $token Token |
147
|
|
|
* @param Token|null $previousToken Previous token |
148
|
|
|
* |
149
|
|
|
* @return Token|null Stash token |
150
|
|
|
* @throws \ChrisKonnertz\StringCalc\Exceptions\ContainerException |
151
|
|
|
* @throws \ChrisKonnertz\StringCalc\Exceptions\InvalidIdentifierException |
152
|
|
|
* @throws \ChrisKonnertz\StringCalc\Exceptions\NotFoundException |
153
|
|
|
*/ |
154
|
|
|
protected function handleToken(array &$refinedTokens, Token $token, Token $previousToken = null): ?Token |
155
|
|
|
{ |
156
|
|
|
// If it's a word token: Handle individually |
157
|
|
|
if ($token->getType() == Token::TYPE_WORD) { |
158
|
|
|
return $this->handleWordToken($refinedTokens, $token, $previousToken); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
// In all other cases: Register the previou token (if any) |
162
|
|
|
if ($previousToken) { |
163
|
|
|
array_push($refinedTokens, $previousToken); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
// If it's a number token: Stash |
167
|
|
|
if ($token->getType() == Token::TYPE_NUMBER) { |
168
|
|
|
return $token; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
array_push($refinedTokens, $token); |
172
|
|
|
|
173
|
|
|
return null; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Handle a particular token |
178
|
|
|
* |
179
|
|
|
* The method returns a list of zero or more (possibly refined) tokens to preerve |
180
|
|
|
* |
181
|
|
|
* @param Token[] $refinedTokens Refined tokens |
182
|
|
|
* @param Token $token Token |
183
|
|
|
* @param Token|null $previousToken Previous token |
184
|
|
|
* |
185
|
|
|
* @return Token|null Stash token |
186
|
|
|
* @throws InvalidArgumentException If the word token is invalid |
187
|
|
|
* @throws \ChrisKonnertz\StringCalc\Exceptions\ContainerException |
188
|
|
|
* @throws \ChrisKonnertz\StringCalc\Exceptions\InvalidIdentifierException |
189
|
|
|
* @throws \ChrisKonnertz\StringCalc\Exceptions\NotFoundException |
190
|
|
|
*/ |
191
|
|
|
protected function handleWordToken(array &$refinedTokens, Token $token, Token $previousToken = null): ?Token |
192
|
|
|
{ |
193
|
|
|
// If it's a calc() function call: Add the previous token and skip the current one |
194
|
|
|
if ($token->getValue() == 'calc') { |
195
|
|
|
if ($previousToken) { |
196
|
|
|
array_push($refinedTokens, $previousToken); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return null; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
// If the previous token is a number: Try to generate a unit length |
203
|
|
|
if ($previousToken && ($previousToken->getType() == Token::TYPE_NUMBER)) { |
204
|
|
|
try { |
205
|
|
|
$unitLength = (new LengthFactory($this->emPixel))->createLengthFromString( |
206
|
|
|
$previousToken->getValue().$token->getValue() |
207
|
|
|
); |
208
|
|
|
$this->handleUnitLengthToken($refinedTokens, $unitLength); |
209
|
|
|
|
210
|
|
|
return null; |
211
|
|
|
} catch (InvalidArgumentException $e) { |
212
|
|
|
// Ignore |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
// Invalid word token |
217
|
|
|
throw new InvalidArgumentException( |
218
|
|
|
sprintf(InvalidArgumentException::INVALID_WORD_TOKEN_IN_SOURCE_SIZE_VALUE_STR, $token->getValue()), |
219
|
|
|
InvalidArgumentException::INVALID_WORD_TOKEN_IN_SOURCE_SIZE_VALUE |
220
|
|
|
); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Handle a unit length token |
225
|
|
|
* |
226
|
|
|
* @param Token[] $refinedTokens Refined tokens |
227
|
|
|
* @param UnitLengthInterface $unitLength Unit length |
228
|
|
|
*/ |
229
|
|
|
protected function handleUnitLengthToken( |
230
|
|
|
array &$refinedTokens, |
231
|
|
|
UnitLengthInterface $unitLength |
232
|
|
|
): void { |
233
|
|
|
// If it's an absolute value |
234
|
|
|
if ($unitLength->isAbsolute()) { |
235
|
|
|
array_push($refinedTokens, new Token(strval($unitLength->getValue()), Token::TYPE_NUMBER, 0)); |
|
|
|
|
236
|
|
|
|
237
|
|
|
return; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
// Else: Substitute with multiplied function expression |
241
|
|
|
array_push( |
242
|
|
|
$refinedTokens, |
243
|
|
|
new Token('(', Token::TYPE_CHARACTER, 0), |
244
|
|
|
new Token(strval($unitLength->getOriginalValue() / 100), Token::TYPE_NUMBER, 0), |
245
|
|
|
new Token('*', Token::TYPE_CHARACTER, 0), |
246
|
|
|
new Token('viewport', Token::TYPE_WORD, 0), |
247
|
|
|
new Token('(', Token::TYPE_CHARACTER, 0), |
248
|
|
|
new Token(')', Token::TYPE_CHARACTER, 0), |
249
|
|
|
new Token(')', Token::TYPE_CHARACTER, 0) |
250
|
|
|
); |
251
|
|
|
} |
252
|
|
|
} |