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