Lines
90.90%
10 / 11
Functions and Methods
66.66%
2 / 3
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| NumberConfig | 90.90% | 10 / 11 | 66.66% | 2 / 3 | 11.09 | 0.00% | 0 / 1 | |||
| __construct | 100.00% | 9 / 9 | 100.00% | 1 / 1 | 7 | |||||
| getPlatform | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | |||||
| formatState | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 3 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Contracts\Exposure; | |
| 6 | ||
| 7 | use Stewart\Contracts\Exception\ExposureException; | |
| 8 | ||
| 9 | final readonly class NumberConfig extends ExposedEntityConfig | |
| 10 | { | |
| 11 | /** @throws ExposureException */ | |
| 12 | public function __construct( | |
| 13 | public int|float $min, | |
| 14 | public int|float $max, | |
| 15 | public int|float $step = 1, | |
| 16 | public NumberMode $mode = NumberMode::Auto, | |
| 17 | public ?NumberDeviceClass $deviceClass = null, | |
| 18 | public ?string $unit = null, | |
| 19 | ?string $name = null, | |
| 20 | ?string $icon = null, | |
| 21 | ?EntityCategory $entityCategory = null, | |
| 22 | bool $enabledByDefault = true, | |
| 23 | ) { | |
| 24 | parent::__construct($name, $icon, $entityCategory, $enabledByDefault); | |
| 25 | ||
| 26 | if (!is_finite($min) || !is_finite($max) || !is_finite($step)) { | |
| 27 | throw ExposureException::configInvalid('The number min, max and step must be finite.'); | |
| 28 | } | |
| 29 | ||
| 30 | if ($min > $max) { | |
| 31 | throw ExposureException::configInvalid('The number min must not be greater than its max.'); | |
| 32 | } | |
| 33 | ||
| 34 | if ($step <= 0) { | |
| 35 | throw ExposureException::configInvalid('The number step must be greater than 0.'); | |
| 36 | } | |
| 37 | ||
| 38 | if ($unit === '') { | |
| 39 | throw ExposureException::configInvalid('The number unit must not be empty; leave it null for none.'); | |
| 40 | } | |
| 41 | } | |
| 42 | ||
| 43 | public function getPlatform(): ExposedPlatform | |
| 44 | { | |
| 45 | return ExposedPlatform::Number; | |
| 46 | } | |
| 47 | ||
| 48 | /** @throws ExposureException */ | |
| 49 | public function formatState(int|float|null $value): int|float|null | |
| 50 | { | |
| 51 | return \is_float($value) && !is_finite($value) ? throw ExposureException::stateInvalid('A number state must be finite.') : $value; | |
| 52 | } | |
| 53 | } |