1 | <?php |
||||
2 | /* |
||||
3 | * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
||||
4 | * |
||||
5 | * Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics) |
||||
6 | * |
||||
7 | * This program is free software: you can redistribute it and/or modify |
||||
8 | * it under the terms of the GNU Affero General Public License as published |
||||
9 | * by the Free Software Foundation, either version 3 of the License, or |
||||
10 | * (at your option) any later version. |
||||
11 | * |
||||
12 | * This program is distributed in the hope that it will be useful, |
||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
15 | * GNU Affero General Public License for more details. |
||||
16 | * |
||||
17 | * You should have received a copy of the GNU Affero General Public License |
||||
18 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
||||
19 | */ |
||||
20 | |||||
21 | declare(strict_types=1); |
||||
22 | |||||
23 | /** |
||||
24 | * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
||||
25 | * |
||||
26 | * Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics) |
||||
27 | * |
||||
28 | * This program is free software: you can redistribute it and/or modify |
||||
29 | * it under the terms of the GNU Affero General Public License as published |
||||
30 | * by the Free Software Foundation, either version 3 of the License, or |
||||
31 | * (at your option) any later version. |
||||
32 | * |
||||
33 | * This program is distributed in the hope that it will be useful, |
||||
34 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
35 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
36 | * GNU Affero General Public License for more details. |
||||
37 | * |
||||
38 | * You should have received a copy of the GNU Affero General Public License |
||||
39 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
||||
40 | */ |
||||
41 | |||||
42 | namespace App\Entity\Parameters; |
||||
43 | |||||
44 | use App\Entity\Base\AbstractDBElement; |
||||
45 | use App\Entity\Base\AbstractNamedDBElement; |
||||
46 | use Doctrine\ORM\Mapping as ORM; |
||||
47 | use InvalidArgumentException; |
||||
48 | use LogicException; |
||||
49 | use Symfony\Component\Validator\Constraints as Assert; |
||||
50 | |||||
51 | use function sprintf; |
||||
52 | |||||
53 | /** |
||||
54 | * @ORM\Entity(repositoryClass="App\Repository\ParameterRepository") |
||||
55 | * @ORM\Table("parameters", indexes={ |
||||
56 | * @ORM\Index(name="parameter_name_idx", columns={"name"}), |
||||
57 | * @ORM\Index(name="parameter_group_idx", columns={"param_group"}), |
||||
58 | * @ORM\Index(name="parameter_type_element_idx", columns={"type", "element_id"}) |
||||
59 | * }) |
||||
60 | * @ORM\InheritanceType("SINGLE_TABLE") |
||||
61 | * @ORM\DiscriminatorColumn(name="type", type="smallint") |
||||
62 | * @ORM\DiscriminatorMap({ |
||||
63 | * 0 = "CategoryParameter", |
||||
64 | * 1 = "CurrencyParameter", |
||||
65 | * 2 = "ProjectParameter", |
||||
66 | * 3 = "FootprintParameter", |
||||
67 | * 4 = "GroupParameter", |
||||
68 | * 5 = "ManufacturerParameter", |
||||
69 | * 6 = "MeasurementUnitParameter", |
||||
70 | * 7 = "PartParameter", |
||||
71 | * 8 = "StorelocationParameter", |
||||
72 | * 9 = "SupplierParameter", |
||||
73 | * 10 = "AttachmentTypeParameter" |
||||
74 | * }) |
||||
75 | */ |
||||
76 | abstract class AbstractParameter extends AbstractNamedDBElement |
||||
77 | { |
||||
78 | /** |
||||
79 | * @var string The class of the element that can be passed to this attachment. Must be overridden in subclasses. |
||||
80 | */ |
||||
81 | public const ALLOWED_ELEMENT_CLASS = ''; |
||||
82 | |||||
83 | /** |
||||
84 | * @var string The mathematical symbol for this specification. Can be rendered pretty later. Should be short |
||||
85 | * @Assert\Length(max=20) |
||||
86 | * @ORM\Column(type="string", nullable=false) |
||||
87 | */ |
||||
88 | protected string $symbol = ''; |
||||
89 | |||||
90 | /** |
||||
91 | * @var float|null the guaranteed minimum value of this property |
||||
92 | * @Assert\Type({"float","null"}) |
||||
93 | * @Assert\LessThanOrEqual(propertyPath="value_typical", message="parameters.validator.min_lesser_typical") |
||||
94 | * @Assert\LessThan(propertyPath="value_max", message="parameters.validator.min_lesser_max") |
||||
95 | * @ORM\Column(type="float", nullable=true) |
||||
96 | */ |
||||
97 | protected ?float $value_min = null; |
||||
98 | |||||
99 | /** |
||||
100 | * @var float|null the typical value of this property |
||||
101 | * @Assert\Type({"null", "float"}) |
||||
102 | * @ORM\Column(type="float", nullable=true) |
||||
103 | */ |
||||
104 | protected ?float $value_typical = null; |
||||
105 | |||||
106 | /** |
||||
107 | * @var float|null the maximum value of this property |
||||
108 | * @Assert\Type({"float", "null"}) |
||||
109 | * @Assert\GreaterThanOrEqual(propertyPath="value_typical", message="parameters.validator.max_greater_typical") |
||||
110 | * @ORM\Column(type="float", nullable=true) |
||||
111 | */ |
||||
112 | protected ?float $value_max = null; |
||||
113 | |||||
114 | /** |
||||
115 | * @var string The unit in which the value values are given (e.g. V) |
||||
116 | * @ORM\Column(type="string", nullable=false) |
||||
117 | */ |
||||
118 | protected string $unit = ''; |
||||
119 | |||||
120 | /** |
||||
121 | * @var string a text value for the given property |
||||
122 | * @ORM\Column(type="string", nullable=false) |
||||
123 | */ |
||||
124 | protected string $value_text = ''; |
||||
125 | |||||
126 | /** |
||||
127 | * @var string the group this parameter belongs to |
||||
128 | * @ORM\Column(type="string", nullable=false, name="param_group") |
||||
129 | */ |
||||
130 | protected string $group = ''; |
||||
131 | |||||
132 | /** |
||||
133 | * Mapping is done in sub classes. |
||||
134 | * |
||||
135 | * @var AbstractDBElement|null the element to which this parameter belongs to |
||||
136 | */ |
||||
137 | protected $element; |
||||
138 | |||||
139 | public function __construct() |
||||
140 | { |
||||
141 | if ('' === static::ALLOWED_ELEMENT_CLASS) { |
||||
0 ignored issues
–
show
introduced
by
![]() |
|||||
142 | throw new LogicException('An *Attachment class must override the ALLOWED_ELEMENT_CLASS const!'); |
||||
143 | } |
||||
144 | } |
||||
145 | |||||
146 | public function updateTimestamps(): void |
||||
147 | { |
||||
148 | parent::updateTimestamps(); |
||||
149 | if ($this->element instanceof AbstractNamedDBElement) { |
||||
150 | $this->element->updateTimestamps(); |
||||
151 | } |
||||
152 | } |
||||
153 | |||||
154 | /** |
||||
155 | * Returns the element this parameter belongs to. |
||||
156 | */ |
||||
157 | public function getElement(): ?AbstractDBElement |
||||
158 | { |
||||
159 | return $this->element; |
||||
160 | } |
||||
161 | |||||
162 | /** |
||||
163 | * Return a formatted string version of the values of the string. |
||||
164 | * Based on the set values it can return something like this: 34 V (12 V ... 50 V) [Text]. |
||||
165 | */ |
||||
166 | public function getFormattedValue(): string |
||||
167 | { |
||||
168 | //If we just only have text value, return early |
||||
169 | if (null === $this->value_typical && null === $this->value_min && null === $this->value_max) { |
||||
170 | return $this->value_text; |
||||
171 | } |
||||
172 | |||||
173 | $str = ''; |
||||
174 | $bracket_opened = false; |
||||
175 | if ($this->value_typical) { |
||||
176 | $str .= $this->getValueTypicalWithUnit(); |
||||
177 | if ($this->value_min || $this->value_max) { |
||||
178 | $bracket_opened = true; |
||||
179 | $str .= ' ('; |
||||
180 | } |
||||
181 | } |
||||
182 | |||||
183 | if ($this->value_max && $this->value_min) { |
||||
184 | $str .= $this->getValueMinWithUnit().' ... '.$this->getValueMaxWithUnit(); |
||||
185 | } elseif ($this->value_max) { |
||||
186 | $str .= 'max. '.$this->getValueMaxWithUnit(); |
||||
187 | } elseif ($this->value_min) { |
||||
188 | $str .= 'min. '.$this->getValueMinWithUnit(); |
||||
189 | } |
||||
190 | |||||
191 | //Add closing bracket |
||||
192 | if ($bracket_opened) { |
||||
193 | $str .= ')'; |
||||
194 | } |
||||
195 | |||||
196 | if ($this->value_text) { |
||||
197 | $str .= ' ['.$this->value_text.']'; |
||||
198 | } |
||||
199 | |||||
200 | return $str; |
||||
201 | } |
||||
202 | |||||
203 | /** |
||||
204 | * Sets the element to which this parameter belongs to. |
||||
205 | * |
||||
206 | * @return $this |
||||
207 | */ |
||||
208 | public function setElement(AbstractDBElement $element): self |
||||
209 | { |
||||
210 | if (!is_a($element, static::ALLOWED_ELEMENT_CLASS)) { |
||||
211 | throw new InvalidArgumentException(sprintf('The element associated with a %s must be a %s!', static::class, static::ALLOWED_ELEMENT_CLASS)); |
||||
212 | } |
||||
213 | |||||
214 | $this->element = $element; |
||||
215 | |||||
216 | return $this; |
||||
217 | } |
||||
218 | |||||
219 | /** |
||||
220 | * Sets the name of the specification. This value is required. |
||||
221 | * |
||||
222 | * @return $this |
||||
223 | */ |
||||
224 | public function setName(string $name): AbstractNamedDBElement |
||||
225 | { |
||||
226 | $this->name = $name; |
||||
227 | |||||
228 | return $this; |
||||
229 | } |
||||
230 | |||||
231 | /** |
||||
232 | * Returns the name of the group this parameter is associated to (e.g. Technical Parameters). |
||||
233 | */ |
||||
234 | public function getGroup(): string |
||||
235 | { |
||||
236 | return $this->group; |
||||
237 | } |
||||
238 | |||||
239 | /** |
||||
240 | * Sets the name of the group this parameter is associated to. |
||||
241 | * |
||||
242 | * @return $this |
||||
243 | */ |
||||
244 | public function setGroup(string $group): self |
||||
245 | { |
||||
246 | $this->group = $group; |
||||
247 | |||||
248 | return $this; |
||||
249 | } |
||||
250 | |||||
251 | /** |
||||
252 | * Returns the mathematical symbol for this specification (e.g. "V_CB"). |
||||
253 | */ |
||||
254 | public function getSymbol(): string |
||||
255 | { |
||||
256 | return $this->symbol; |
||||
257 | } |
||||
258 | |||||
259 | /** |
||||
260 | * Sets the mathematical symbol for this specification (e.g. "V_CB"). |
||||
261 | * |
||||
262 | * @return $this |
||||
263 | */ |
||||
264 | public function setSymbol(string $symbol): self |
||||
265 | { |
||||
266 | $this->symbol = $symbol; |
||||
267 | |||||
268 | return $this; |
||||
269 | } |
||||
270 | |||||
271 | /** |
||||
272 | * Returns The guaranteed minimum value of this property. |
||||
273 | */ |
||||
274 | public function getValueMin(): ?float |
||||
275 | { |
||||
276 | return $this->value_min; |
||||
277 | } |
||||
278 | |||||
279 | /** |
||||
280 | * Sets the minimum value of this property. |
||||
281 | * |
||||
282 | * @return $this |
||||
283 | */ |
||||
284 | public function setValueMin(?float $value_min): self |
||||
285 | { |
||||
286 | $this->value_min = $value_min; |
||||
287 | |||||
288 | return $this; |
||||
289 | } |
||||
290 | |||||
291 | /** |
||||
292 | * Returns the typical value of this property. |
||||
293 | */ |
||||
294 | public function getValueTypical(): ?float |
||||
295 | { |
||||
296 | return $this->value_typical; |
||||
297 | } |
||||
298 | |||||
299 | /** |
||||
300 | * Return a formatted version with the minimum value with the unit of this parameter. |
||||
301 | */ |
||||
302 | public function getValueTypicalWithUnit(): string |
||||
303 | { |
||||
304 | return $this->formatWithUnit($this->value_typical); |
||||
0 ignored issues
–
show
It seems like
$this->value_typical can also be of type null ; however, parameter $value of App\Entity\Parameters\Ab...meter::formatWithUnit() does only seem to accept double , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
305 | } |
||||
306 | |||||
307 | /** |
||||
308 | * Return a formatted version with the maximum value with the unit of this parameter. |
||||
309 | */ |
||||
310 | public function getValueMaxWithUnit(): string |
||||
311 | { |
||||
312 | return $this->formatWithUnit($this->value_max); |
||||
0 ignored issues
–
show
It seems like
$this->value_max can also be of type null ; however, parameter $value of App\Entity\Parameters\Ab...meter::formatWithUnit() does only seem to accept double , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
313 | } |
||||
314 | |||||
315 | /** |
||||
316 | * Return a formatted version with the typical value with the unit of this parameter. |
||||
317 | */ |
||||
318 | public function getValueMinWithUnit(): string |
||||
319 | { |
||||
320 | return $this->formatWithUnit($this->value_min); |
||||
0 ignored issues
–
show
It seems like
$this->value_min can also be of type null ; however, parameter $value of App\Entity\Parameters\Ab...meter::formatWithUnit() does only seem to accept double , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
321 | } |
||||
322 | |||||
323 | /** |
||||
324 | * Sets the typical value of this property. |
||||
325 | * |
||||
326 | * @param float|null $value_typical |
||||
327 | * |
||||
328 | * @return $this |
||||
329 | */ |
||||
330 | public function setValueTypical(?float $value_typical): self |
||||
331 | { |
||||
332 | $this->value_typical = $value_typical; |
||||
333 | |||||
334 | return $this; |
||||
335 | } |
||||
336 | |||||
337 | /** |
||||
338 | * Returns the guaranteed maximum value. |
||||
339 | */ |
||||
340 | public function getValueMax(): ?float |
||||
341 | { |
||||
342 | return $this->value_max; |
||||
343 | } |
||||
344 | |||||
345 | /** |
||||
346 | * Sets the guaranteed maximum value. |
||||
347 | * |
||||
348 | * @return $this |
||||
349 | */ |
||||
350 | public function setValueMax(?float $value_max): self |
||||
351 | { |
||||
352 | $this->value_max = $value_max; |
||||
353 | |||||
354 | return $this; |
||||
355 | } |
||||
356 | |||||
357 | /** |
||||
358 | * Returns the unit used by the value (e.g. "V"). |
||||
359 | */ |
||||
360 | public function getUnit(): string |
||||
361 | { |
||||
362 | return $this->unit; |
||||
363 | } |
||||
364 | |||||
365 | /** |
||||
366 | * Sets the unit used by the value. |
||||
367 | * |
||||
368 | * @return $this |
||||
369 | */ |
||||
370 | public function setUnit(string $unit): self |
||||
371 | { |
||||
372 | $this->unit = $unit; |
||||
373 | |||||
374 | return $this; |
||||
375 | } |
||||
376 | |||||
377 | /** |
||||
378 | * Returns the text value. |
||||
379 | */ |
||||
380 | public function getValueText(): string |
||||
381 | { |
||||
382 | return $this->value_text; |
||||
383 | } |
||||
384 | |||||
385 | /** |
||||
386 | * Sets the text value. |
||||
387 | * |
||||
388 | * @return $this |
||||
389 | */ |
||||
390 | public function setValueText(string $value_text): self |
||||
391 | { |
||||
392 | $this->value_text = $value_text; |
||||
393 | |||||
394 | return $this; |
||||
395 | } |
||||
396 | |||||
397 | /** |
||||
398 | * Return a string representation and (if possible) with its unit. |
||||
399 | */ |
||||
400 | protected function formatWithUnit(float $value, string $format = '%g'): string |
||||
401 | { |
||||
402 | $str = sprintf($format, $value); |
||||
403 | if (!empty($this->unit)) { |
||||
404 | return $str.' '.$this->unit; |
||||
405 | } |
||||
406 | |||||
407 | return $str; |
||||
408 | } |
||||
409 | } |
||||
410 |