Passed
Push — master ( d7dee9...483d68 )
by Jan
04:53 queued 10s
created

AbstractParameter::getValueMaxWithUnit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
32
use Symfony\Component\Validator\Constraints as Assert;
33
34
/**
35
 * @ORM\Entity()
36
 * @ORM\Table("parameters")
37
 * @ORM\InheritanceType("SINGLE_TABLE")
38
 * @ORM\DiscriminatorColumn(name="type", type="smallint")
39
 * @ORM\DiscriminatorMap({
40
 *      0 = "CategoryParameter",
41
 *      1 = "CurrencyParameter",
42
 *      2 = "DeviceParameter",
43
 *      3 = "FootprintParameter",
44
 *      4 = "GroupParameter",
45
 *      5 = "ManufacturerParameter",
46
 *      6 = "MeasurementUnitParameter",
47
 *      7 = "PartParameter",
48
 *      8 = "StorelocationParameter",
49
 *      9 = "SupplierParameter",
50
 *      10 = "AttachmentTypeParameter"
51
 * })
52
 */
53
abstract class AbstractParameter extends AbstractNamedDBElement
54
{
55
    /**
56
     * @var string The class of the element that can be passed to this attachment. Must be overridden in subclasses.
57
     */
58
    public const ALLOWED_ELEMENT_CLASS = '';
59
60
    /**
61
     * @var string The mathematical symbol for this specification. Can be rendered pretty later. Should be short
62
     * @Assert\Length(max=20)
63
     * @ORM\Column(type="string", nullable=false)
64
     */
65
    protected $symbol = '';
66
67
    /**
68
     * @var float|null The guaranteed minimum value of this property.
69
     * @Assert\Type({"float","null"})
70
     * @Assert\LessThanOrEqual(propertyPath="value_typical", message="parameters.validator.min_lesser_typical")
71
     * @Assert\LessThan(propertyPath="value_max", message="parameters.validator.min_lesser_max")
72
     * @ORM\Column(type="float", nullable=true)
73
     */
74
    protected $value_min;
75
76
    /**
77
     * @var float|null The typical value of this property.
78
     * @Assert\Type({"null", "float"})
79
     * @ORM\Column(type="float", nullable=true)
80
     */
81
    protected $value_typical;
82
83
    /**
84
     * @var float|null The maximum value of this property.
85
     * @Assert\Type({"float", "null"})
86
     * @Assert\GreaterThanOrEqual(propertyPath="value_typical", message="parameters.validator.max_greater_typical")
87
     * @ORM\Column(type="float", nullable=true)
88
     */
89
    protected $value_max;
90
91
    /**
92
     * @var string The unit in which the value values are given (e.g. V)
93
     * @Assert\Length(max=5)
94
     * @ORM\Column(type="string", nullable=false)
95
     */
96
    protected $unit = '';
97
98
    /**
99
     * @var string A text value for the given property.
100
     * @ORM\Column(type="string", nullable=false)
101
     */
102
    protected $value_text = '';
103
104
    /**
105
     * @var string The group this parameter belongs to.
106
     * @ORM\Column(type="string", nullable=false, name="param_group")
107
     */
108
    protected $group = '';
109
110
    /**
111
     * Mapping is done in sub classes.
112
     *
113
     * @var AbstractDBElement|null The element to which this parameter belongs to.
114
     */
115
    protected $element;
116
117
    public function __construct()
118
    {
119
        if ('' === static::ALLOWED_ELEMENT_CLASS) {
0 ignored issues
show
introduced by
The condition '' === static::ALLOWED_ELEMENT_CLASS is always true.
Loading history...
120
            throw new LogicException('An *Attachment class must override the ALLOWED_ELEMENT_CLASS const!');
121
        }
122
    }
123
124
    /**
125
     * Returns the name of the specification (e.g. "Collector-Base Voltage").
126
     *
127
     * @return string
128
     */
129
    public function getName(): string
130
    {
131
        return $this->name;
132
    }
133
134
    /**
135
     * Returns the element this parameter belongs to.
136
     *
137
     * @return AbstractDBElement|null
138
     */
139
    public function getElement(): ?AbstractDBElement
140
    {
141
        return $this->element;
142
    }
143
144
    /**
145
     * Return a formatted string version of the values of the string.
146
     * Based on the set values it can return something like this: 34 V (12 V ... 50 V) [Text].
147
     *
148
     * @return string
149
     */
150
    public function getFormattedValue(): string
151
    {
152
        //If we just only have text value, return early
153
        if (null === $this->value_typical && null === $this->value_min && null === $this->value_max) {
154
            return $this->value_text;
155
        }
156
157
        $str = '';
158
        $bracket_opened = false;
159
        if ($this->value_typical) {
160
            $str .= $this->getValueTypicalWithUnit();
161
            if ($this->value_min || $this->value_max) {
162
                $bracket_opened = true;
163
                $str .= ' (';
164
            }
165
        }
166
167
        if ($this->value_max && $this->value_min) {
168
            $str .= $this->getValueMinWithUnit().' ... '.$this->getValueMaxWithUnit();
169
        } elseif ($this->value_max) {
170
            $str .= 'max. '.$this->getValueMaxWithUnit();
171
        } elseif ($this->value_min) {
172
            $str .= 'min. '.$this->getValueMinWithUnit();
173
        }
174
175
        //Add closing bracket
176
        if ($bracket_opened) {
177
            $str .= ')';
178
        }
179
180
        if ($this->value_text) {
181
            $str .= ' ['.$this->value_text.']';
182
        }
183
184
        return $str;
185
    }
186
187
    /**
188
     * Sets the element to which this parameter belongs to.
189
     *
190
     * @return $this
191
     */
192
    public function setElement(AbstractDBElement $element): self
193
    {
194
        if (! is_a($element, static::ALLOWED_ELEMENT_CLASS)) {
195
            throw new InvalidArgumentException(sprintf('The element associated with a %s must be a %s!', static::class, static::ALLOWED_ELEMENT_CLASS));
196
        }
197
198
        $this->element = $element;
199
200
        return $this;
201
    }
202
203
    /**
204
     * Sets the name of the specification. This value is required.
205
     *
206
     * @return $this
207
     */
208
    public function setName(string $name): AbstractNamedDBElement
209
    {
210
        $this->name = $name;
211
212
        return $this;
213
    }
214
215
    /**
216
     * Returns the name of the group this parameter is associated to (e.g. Technical Parameters)
217
     * @return string
218
     */
219
    public function getGroup(): string
220
    {
221
        return $this->group;
222
    }
223
224
    /**
225
     * Sets the name of the group this parameter is associated to.
226
     * @param  string  $group
227
     * @return $this
228
     */
229
    public function setGroup(string $group): self
230
    {
231
        $this->group = $group;
232
        return $this;
233
    }
234
235
    /**
236
     * Returns the mathematical symbol for this specification (e.g. "V_CB").
237
     *
238
     * @return string
239
     */
240
    public function getSymbol(): string
241
    {
242
        return $this->symbol;
243
    }
244
245
    /**
246
     * Sets the mathematical symbol for this specification (e.g. "V_CB").
247
     *
248
     * @return $this
249
     */
250
    public function setSymbol(string $symbol): self
251
    {
252
        $this->symbol = $symbol;
253
254
        return $this;
255
    }
256
257
    /**
258
     * Returns The guaranteed minimum value of this property.
259
     *
260
     * @return float|null
261
     */
262
    public function getValueMin(): ?float
263
    {
264
        return $this->value_min;
265
    }
266
267
    /**
268
     * Sets the minimum value of this property.
269
     *
270
     * @return $this
271
     */
272
    public function setValueMin(?float $value_min): self
273
    {
274
        $this->value_min = $value_min;
275
276
        return $this;
277
    }
278
279
    /**
280
     * Returns the typical value of this property.
281
     *
282
     * @return float|null
283
     */
284
    public function getValueTypical(): ?float
285
    {
286
        return $this->value_typical;
287
    }
288
289
    /**
290
     * Return a formatted version with the minimum value with the unit of this parameter.
291
     *
292
     * @return string
293
     */
294
    public function getValueTypicalWithUnit(): string
295
    {
296
        return $this->formatWithUnit($this->value_typical);
0 ignored issues
show
Bug introduced by
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 ignore-type  annotation

296
        return $this->formatWithUnit(/** @scrutinizer ignore-type */ $this->value_typical);
Loading history...
297
    }
298
299
    /**
300
     * Return a formatted version with the maximum value with the unit of this parameter.
301
     *
302
     * @return string
303
     */
304
    public function getValueMaxWithUnit(): string
305
    {
306
        return $this->formatWithUnit($this->value_max);
0 ignored issues
show
Bug introduced by
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 ignore-type  annotation

306
        return $this->formatWithUnit(/** @scrutinizer ignore-type */ $this->value_max);
Loading history...
307
    }
308
309
    /**
310
     * Return a formatted version with the typical value with the unit of this parameter.
311
     *
312
     * @return string
313
     */
314
    public function getValueMinWithUnit(): string
315
    {
316
        return $this->formatWithUnit($this->value_min);
0 ignored issues
show
Bug introduced by
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 ignore-type  annotation

316
        return $this->formatWithUnit(/** @scrutinizer ignore-type */ $this->value_min);
Loading history...
317
    }
318
319
    /**
320
     * Sets the typical value of this property.
321
     *
322
     * @param float $value_typical
323
     *
324
     * @return $this
325
     */
326
    public function setValueTypical(?float $value_typical): self
327
    {
328
        $this->value_typical = $value_typical;
329
330
        return $this;
331
    }
332
333
    /**
334
     * Returns the guaranteed maximum value.
335
     *
336
     * @return float|null
337
     */
338
    public function getValueMax(): ?float
339
    {
340
        return $this->value_max;
341
    }
342
343
    /**
344
     * Sets the guaranteed maximum value.
345
     *
346
     * @return $this
347
     */
348
    public function setValueMax(?float $value_max): self
349
    {
350
        $this->value_max = $value_max;
351
352
        return $this;
353
    }
354
355
    /**
356
     * Returns the unit used by the value (e.g. "V").
357
     *
358
     * @return string
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
     * @return string
381
     */
382
    public function getValueText(): string
383
    {
384
        return $this->value_text;
385
    }
386
387
    /**
388
     * Sets the text value.
389
     *
390
     * @return $this
391
     */
392
    public function setValueText(string $value_text): self
393
    {
394
        $this->value_text = $value_text;
395
396
        return $this;
397
    }
398
399
    public function getIDString(): string
400
    {
401
        return 'PM'.sprintf('%09d', $this->getID());
402
    }
403
404
    /**
405
     * Return a string representation and (if possible) with its unit.
406
     *
407
     * @return string
408
     */
409
    protected function formatWithUnit(float $value, string $format = '%g'): string
410
    {
411
        $str = \sprintf($format, $value);
412
        if (! empty($this->unit)) {
413
            return $str.' '.$this->unit;
414
        }
415
416
        return $str;
417
    }
418
}
419