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\Tokenizer\Token; |
40
|
|
|
use Jkphl\Respimgcss\Application\Contract\UnitLengthInterface; |
41
|
|
|
use Jkphl\Respimgcss\Application\Exceptions\InvalidArgumentException; |
42
|
|
|
use Jkphl\Respimgcss\Application\Model\AbsoluteLength; |
43
|
|
|
use Jkphl\Respimgcss\Application\Model\PercentageLength; |
44
|
|
|
use Jkphl\Respimgcss\Application\Model\StringCalculator; |
45
|
|
|
use Jkphl\Respimgcss\Application\Model\ViewportLength; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* AbstractLength Factory |
49
|
|
|
* |
50
|
|
|
* @package Jkphl\Respimgcss |
51
|
|
|
* @subpackage Jkphl\Respimgcss\Application\Factory |
52
|
|
|
*/ |
53
|
|
|
class LengthFactory extends AbstractLengthFactory |
54
|
|
|
{ |
55
|
|
|
/** |
56
|
|
|
* Absolute units |
57
|
|
|
* |
58
|
|
|
* @var array |
59
|
|
|
*/ |
60
|
|
|
const UNITS_ABSOLUTE = [ |
61
|
|
|
UnitLengthInterface::UNIT_PIXEL, |
62
|
|
|
UnitLengthInterface::UNIT_EM, |
63
|
|
|
UnitLengthInterface::UNIT_REM, |
64
|
|
|
UnitLengthInterface::UNIT_CM, |
65
|
|
|
UnitLengthInterface::UNIT_MM, |
66
|
|
|
UnitLengthInterface::UNIT_IN, |
67
|
|
|
UnitLengthInterface::UNIT_PC, |
68
|
|
|
UnitLengthInterface::UNIT_PT, |
69
|
|
|
]; |
70
|
|
|
/** |
71
|
|
|
* Relative units |
72
|
|
|
* |
73
|
|
|
* @var array |
74
|
|
|
*/ |
75
|
|
|
const UNITS_RELATIVE = [ |
76
|
|
|
UnitLengthInterface::UNIT_PERCENT, |
77
|
|
|
UnitLengthInterface::UNIT_VW, |
78
|
|
|
]; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Parse a length string and return a length with unit instance |
82
|
|
|
* |
83
|
|
|
* @param string $length AbstractLength string |
84
|
|
|
* |
85
|
|
|
* @return UnitLengthInterface AbstractLength with unit |
86
|
|
|
* @throws \ChrisKonnertz\StringCalc\Exceptions\ContainerException |
87
|
|
|
* @throws \ChrisKonnertz\StringCalc\Exceptions\InvalidIdentifierException |
88
|
|
|
* @throws \ChrisKonnertz\StringCalc\Exceptions\NotFoundException |
89
|
|
|
*/ |
90
|
13 |
|
public function createLengthFromString(string $length): UnitLengthInterface |
91
|
|
|
{ |
92
|
13 |
|
$valueAndUnit = $this->matchValueAndUnit($length); |
93
|
|
|
|
94
|
12 |
|
return $this->makeInstance($valueAndUnit[1], $valueAndUnit[2]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Match the value and unit of a length descriptor |
99
|
|
|
* |
100
|
|
|
* @param string $length AbstractLength descriptor |
101
|
|
|
* |
102
|
|
|
* @return array Value and unit (PCRE match) |
103
|
|
|
* @throws InvalidArgumentException If the length string is invalid |
104
|
|
|
*/ |
105
|
13 |
|
protected function matchValueAndUnit($length): array |
106
|
|
|
{ |
107
|
13 |
|
$length = trim(strtolower($length)); |
108
|
13 |
|
if (!strlen($length) || !preg_match('/^(\d+(?:\.\d+)?)([pxremcintvw%]+)$/i', $length, $valueAndUnit)) { |
109
|
1 |
|
throw new InvalidArgumentException( |
110
|
1 |
|
sprintf(InvalidArgumentException::INVALID_LENGTH_STR, $length), |
111
|
1 |
|
InvalidArgumentException::INVALID_LENGTH |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
12 |
|
return $valueAndUnit; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Create a value with unit instance |
120
|
|
|
* |
121
|
|
|
* @param string $value Value |
122
|
|
|
* @param string $unit Unit |
123
|
|
|
* |
124
|
|
|
* @return UnitLengthInterface AbstractLength with unit |
125
|
|
|
* @throws InvalidArgumentException If the unit is invalid |
126
|
|
|
* @throws \ChrisKonnertz\StringCalc\Exceptions\ContainerException |
127
|
|
|
* @throws \ChrisKonnertz\StringCalc\Exceptions\InvalidIdentifierException |
128
|
|
|
* @throws \ChrisKonnertz\StringCalc\Exceptions\NotFoundException |
129
|
|
|
*/ |
130
|
12 |
|
protected function makeInstance(string $value, string $unit): UnitLengthInterface |
131
|
|
|
{ |
132
|
|
|
// If it's an absolute unit |
133
|
12 |
|
if (in_array($unit, self::UNITS_ABSOLUTE)) { |
134
|
10 |
|
return new AbsoluteLength(floatval($value), $unit, $this->lengthNormalizerService); |
135
|
|
|
} |
136
|
|
|
|
137
|
3 |
|
switch ($unit) { |
138
|
|
|
case UnitLengthInterface::UNIT_VW: // Viewport unit |
139
|
1 |
|
$stringCalc = new StringCalculator(); |
140
|
|
|
|
141
|
1 |
|
return new ViewportLength( |
142
|
1 |
|
$stringCalc->parse( |
143
|
|
|
[ |
144
|
1 |
|
new Token(strval($value / 100), Token::TYPE_NUMBER, 0), |
145
|
1 |
|
new Token('*', Token::TYPE_CHARACTER, 0), |
146
|
1 |
|
new Token('viewport', Token::TYPE_WORD, 0), |
147
|
1 |
|
new Token('(', Token::TYPE_CHARACTER, 0), |
148
|
1 |
|
new Token(')', Token::TYPE_CHARACTER, 0), |
149
|
|
|
] |
150
|
|
|
), |
151
|
1 |
|
$this->lengthNormalizerService, |
152
|
1 |
|
$value |
153
|
|
|
); |
154
|
|
|
|
155
|
|
|
case UnitLengthInterface::UNIT_PERCENT: // Percentages |
156
|
1 |
|
return new PercentageLength(floatval($value)); |
157
|
|
|
|
158
|
|
|
default: // Invalid unit |
159
|
1 |
|
throw new InvalidArgumentException( |
160
|
1 |
|
sprintf(InvalidArgumentException::INVALID_UNIT_STR, $unit), |
161
|
1 |
|
InvalidArgumentException::INVALID_UNIT |
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
} |