Passed
Push — master ( 0fe83c...a28e81 )
by Jan
04:32
created

AbstractParameter::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 4
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\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) {
0 ignored issues
show
introduced by
The condition '' === static::ALLOWED_ELEMENT_CLASS is always true.
Loading history...
119
            throw new LogicException('An *Attachment class must override the ALLOWED_ELEMENT_CLASS const!');
120
        }
121
    }
122
123
    /**
124
     * Returns the element this parameter belongs to.
125
     *
126
     * @return AbstractDBElement|null
127
     */
128
    public function getElement(): ?AbstractDBElement
129
    {
130
        return $this->element;
131
    }
132
133
    /**
134
     * Return a formatted string version of the values of the string.
135
     * Based on the set values it can return something like this: 34 V (12 V ... 50 V) [Text].
136
     *
137
     * @return string
138
     */
139
    public function getFormattedValue(): string
140
    {
141
        //If we just only have text value, return early
142
        if (null === $this->value_typical && null === $this->value_min && null === $this->value_max) {
143
            return $this->value_text;
144
        }
145
146
        $str = '';
147
        $bracket_opened = false;
148
        if ($this->value_typical) {
149
            $str .= $this->getValueTypicalWithUnit();
150
            if ($this->value_min || $this->value_max) {
151
                $bracket_opened = true;
152
                $str .= ' (';
153
            }
154
        }
155
156
        if ($this->value_max && $this->value_min) {
157
            $str .= $this->getValueMinWithUnit().' ... '.$this->getValueMaxWithUnit();
158
        } elseif ($this->value_max) {
159
            $str .= 'max. '.$this->getValueMaxWithUnit();
160
        } elseif ($this->value_min) {
161
            $str .= 'min. '.$this->getValueMinWithUnit();
162
        }
163
164
        //Add closing bracket
165
        if ($bracket_opened) {
166
            $str .= ')';
167
        }
168
169
        if ($this->value_text) {
170
            $str .= ' ['.$this->value_text.']';
171
        }
172
173
        return $str;
174
    }
175
176
    /**
177
     * Sets the element to which this parameter belongs to.
178
     *
179
     * @return $this
180
     */
181
    public function setElement(AbstractDBElement $element): self
182
    {
183
        if (! is_a($element, static::ALLOWED_ELEMENT_CLASS)) {
184
            throw new InvalidArgumentException(sprintf('The element associated with a %s must be a %s!', static::class, static::ALLOWED_ELEMENT_CLASS));
185
        }
186
187
        $this->element = $element;
188
189
        return $this;
190
    }
191
192
    /**
193
     * Sets the name of the specification. This value is required.
194
     *
195
     * @return $this
196
     */
197
    public function setName(string $name): AbstractNamedDBElement
198
    {
199
        $this->name = $name;
200
201
        return $this;
202
    }
203
204
    /**
205
     * Returns the name of the group this parameter is associated to (e.g. Technical Parameters).
206
     *
207
     * @return string
208
     */
209
    public function getGroup(): string
210
    {
211
        return $this->group;
212
    }
213
214
    /**
215
     * Sets the name of the group this parameter is associated to.
216
     *
217
     * @return $this
218
     */
219
    public function setGroup(string $group): self
220
    {
221
        $this->group = $group;
222
223
        return $this;
224
    }
225
226
    /**
227
     * Returns the mathematical symbol for this specification (e.g. "V_CB").
228
     *
229
     * @return string
230
     */
231
    public function getSymbol(): string
232
    {
233
        return $this->symbol;
234
    }
235
236
    /**
237
     * Sets the mathematical symbol for this specification (e.g. "V_CB").
238
     *
239
     * @return $this
240
     */
241
    public function setSymbol(string $symbol): self
242
    {
243
        $this->symbol = $symbol;
244
245
        return $this;
246
    }
247
248
    /**
249
     * Returns The guaranteed minimum value of this property.
250
     *
251
     * @return float|null
252
     */
253
    public function getValueMin(): ?float
254
    {
255
        return $this->value_min;
256
    }
257
258
    /**
259
     * Sets the minimum value of this property.
260
     *
261
     * @return $this
262
     */
263
    public function setValueMin(?float $value_min): self
264
    {
265
        $this->value_min = $value_min;
266
267
        return $this;
268
    }
269
270
    /**
271
     * Returns the typical value of this property.
272
     *
273
     * @return float|null
274
     */
275
    public function getValueTypical(): ?float
276
    {
277
        return $this->value_typical;
278
    }
279
280
    /**
281
     * Return a formatted version with the minimum value with the unit of this parameter.
282
     *
283
     * @return string
284
     */
285
    public function getValueTypicalWithUnit(): string
286
    {
287
        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

287
        return $this->formatWithUnit(/** @scrutinizer ignore-type */ $this->value_typical);
Loading history...
288
    }
289
290
    /**
291
     * Return a formatted version with the maximum value with the unit of this parameter.
292
     *
293
     * @return string
294
     */
295
    public function getValueMaxWithUnit(): string
296
    {
297
        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

297
        return $this->formatWithUnit(/** @scrutinizer ignore-type */ $this->value_max);
Loading history...
298
    }
299
300
    /**
301
     * Return a formatted version with the typical value with the unit of this parameter.
302
     *
303
     * @return string
304
     */
305
    public function getValueMinWithUnit(): string
306
    {
307
        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

307
        return $this->formatWithUnit(/** @scrutinizer ignore-type */ $this->value_min);
Loading history...
308
    }
309
310
    /**
311
     * Sets the typical value of this property.
312
     *
313
     * @param float $value_typical
314
     *
315
     * @return $this
316
     */
317
    public function setValueTypical(?float $value_typical): self
318
    {
319
        $this->value_typical = $value_typical;
320
321
        return $this;
322
    }
323
324
    /**
325
     * Returns the guaranteed maximum value.
326
     *
327
     * @return float|null
328
     */
329
    public function getValueMax(): ?float
330
    {
331
        return $this->value_max;
332
    }
333
334
    /**
335
     * Sets the guaranteed maximum value.
336
     *
337
     * @return $this
338
     */
339
    public function setValueMax(?float $value_max): self
340
    {
341
        $this->value_max = $value_max;
342
343
        return $this;
344
    }
345
346
    /**
347
     * Returns the unit used by the value (e.g. "V").
348
     *
349
     * @return string
350
     */
351
    public function getUnit(): string
352
    {
353
        return $this->unit;
354
    }
355
356
    /**
357
     * Sets the unit used by the value.
358
     *
359
     * @return $this
360
     */
361
    public function setUnit(string $unit): self
362
    {
363
        $this->unit = $unit;
364
365
        return $this;
366
    }
367
368
    /**
369
     * Returns the text value.
370
     *
371
     * @return string
372
     */
373
    public function getValueText(): string
374
    {
375
        return $this->value_text;
376
    }
377
378
    /**
379
     * Sets the text value.
380
     *
381
     * @return $this
382
     */
383
    public function setValueText(string $value_text): self
384
    {
385
        $this->value_text = $value_text;
386
387
        return $this;
388
    }
389
390
    public function getIDString(): string
391
    {
392
        return 'PM'.sprintf('%09d', $this->getID());
393
    }
394
395
    /**
396
     * Return a string representation and (if possible) with its unit.
397
     *
398
     * @return string
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