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