|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* NumericValue class. |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright YetiForce Sp. z o.o. |
|
9
|
|
|
* @license MIT |
|
10
|
|
|
* @author Rafal Pospiech <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace YetiForcePDF\Style; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class NumericValue. |
|
17
|
|
|
*/ |
|
18
|
|
|
class NumericValue |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Original css value 12px for example. |
|
22
|
|
|
* |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
private $original; |
|
26
|
|
|
/** |
|
27
|
|
|
* Numeric string representation of original value (without unit). |
|
28
|
|
|
* |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
private $value; |
|
32
|
|
|
/** |
|
33
|
|
|
* Unit of original value for '12px' it will be 'px'. |
|
34
|
|
|
* |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
private $unit; |
|
38
|
|
|
/** |
|
39
|
|
|
* Converted to pixel value from other unit (from 'em' for example). |
|
40
|
|
|
* |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
private $converted = '0'; |
|
44
|
|
|
/** |
|
45
|
|
|
* Is this numeric value for font size? |
|
46
|
|
|
* |
|
47
|
|
|
* @var bool |
|
48
|
|
|
*/ |
|
49
|
|
|
private $isFont; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Initialize numeric value. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $value css value 12px for example |
|
55
|
|
|
* @param bool $isFont |
|
56
|
|
|
* |
|
57
|
|
|
* @return self |
|
58
|
|
|
*/ |
|
59
|
|
|
public function init(string $value, bool $isFont = false) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->original = $value; |
|
62
|
|
|
$this->value = \YetiForcePDF\Style\Normalizer\Normalizer::getNumericValue($value); |
|
|
|
|
|
|
63
|
|
|
$this->unit = \YetiForcePDF\Style\Normalizer\Normalizer::getNumericUnit($value, 'px'); |
|
64
|
|
|
$this->isFont = $isFont; |
|
65
|
|
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Get numeric value out of string value or return original if it's not numeric. |
|
70
|
|
|
* |
|
71
|
|
|
* @param any|string $value |
|
|
|
|
|
|
72
|
|
|
* @param Style $style |
|
73
|
|
|
* @param bool $isFont |
|
74
|
|
|
* |
|
75
|
|
|
* @return any|NumericValue |
|
76
|
|
|
*/ |
|
77
|
|
|
public static function get($value, bool $isFont = false) |
|
78
|
|
|
{ |
|
79
|
|
|
if (!\is_string($value)) { |
|
80
|
|
|
return $value; |
|
81
|
|
|
} |
|
82
|
|
|
if (false !== \YetiForcePDF\Style\Normalizer\Normalizer::getNumericValue($value)) { |
|
83
|
|
|
return (new self())->init($value, $isFont); |
|
84
|
|
|
} |
|
85
|
|
|
return $value; |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Get the value of original. |
|
90
|
|
|
* |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getOriginal(): string |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->original; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Set the value of original. |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $original |
|
102
|
|
|
* |
|
103
|
|
|
* @return self |
|
104
|
|
|
*/ |
|
105
|
|
|
public function setOriginal(string $original) |
|
106
|
|
|
{ |
|
107
|
|
|
$this->original = $original; |
|
108
|
|
|
|
|
109
|
|
|
return $this; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Get the value of value. |
|
114
|
|
|
* |
|
115
|
|
|
* @return string |
|
116
|
|
|
*/ |
|
117
|
|
|
public function getValue(): string |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->value; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Set the value of value. |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $value |
|
126
|
|
|
* |
|
127
|
|
|
* @return self |
|
128
|
|
|
*/ |
|
129
|
|
|
public function setValue(string $value) |
|
130
|
|
|
{ |
|
131
|
|
|
$this->value = $value; |
|
132
|
|
|
|
|
133
|
|
|
return $this; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Get the value of unit. |
|
138
|
|
|
* |
|
139
|
|
|
* @return string |
|
140
|
|
|
*/ |
|
141
|
|
|
public function getUnit(): string |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->unit; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Set the value of unit. |
|
148
|
|
|
* |
|
149
|
|
|
* @param string $unit |
|
150
|
|
|
* |
|
151
|
|
|
* @return self |
|
152
|
|
|
*/ |
|
153
|
|
|
public function setUnit(string $unit) |
|
154
|
|
|
{ |
|
155
|
|
|
$this->unit = $unit; |
|
156
|
|
|
|
|
157
|
|
|
return $this; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Get the value of converted. |
|
162
|
|
|
* |
|
163
|
|
|
* @return string |
|
164
|
|
|
*/ |
|
165
|
|
|
public function getConverted(): string |
|
166
|
|
|
{ |
|
167
|
|
|
return $this->converted; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Set the value of converted. |
|
172
|
|
|
* |
|
173
|
|
|
* @param string $converted |
|
174
|
|
|
* |
|
175
|
|
|
* @return self |
|
176
|
|
|
*/ |
|
177
|
|
|
public function setConverted(string $converted) |
|
178
|
|
|
{ |
|
179
|
|
|
$this->converted = $converted; |
|
180
|
|
|
|
|
181
|
|
|
return $this; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Convert unit. |
|
186
|
|
|
* |
|
187
|
|
|
* @param Style $style |
|
188
|
|
|
* |
|
189
|
|
|
* @return self |
|
190
|
|
|
*/ |
|
191
|
|
|
public function convert(Style $style) |
|
192
|
|
|
{ |
|
193
|
|
|
$this->converted = $style->convertUnits($this->unit, $this->value, $this->isFont); |
|
194
|
|
|
return $this; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Get the value of isFont. |
|
199
|
|
|
* |
|
200
|
|
|
* @return bool |
|
201
|
|
|
*/ |
|
202
|
|
|
public function getIsFont(): bool |
|
203
|
|
|
{ |
|
204
|
|
|
return $this->isFont; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Set the value of isFont. |
|
209
|
|
|
* |
|
210
|
|
|
* @param bool $isFont |
|
211
|
|
|
* |
|
212
|
|
|
* @return self |
|
213
|
|
|
*/ |
|
214
|
|
|
public function setIsFont(bool $isFont) |
|
215
|
|
|
{ |
|
216
|
|
|
$this->isFont = $isFont; |
|
217
|
|
|
|
|
218
|
|
|
return $this; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
public function __toString() |
|
222
|
|
|
{ |
|
223
|
|
|
return $this->converted; |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.