Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
84 / 84 |
|
100.00% |
21 / 21 |
CRAP | |
100.00% |
1 / 1 |
| Temperature | |
100.00% |
84 / 84 |
|
100.00% |
21 / 21 |
26 | |
100.00% |
1 / 1 |
| label | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| fullNamePtBr | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| symbol | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| isMetric | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isImperial | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isAbsolute | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| absoluteZero | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| boilingPointOfWater | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| freezingPointOfWater | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| convertTo | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| toCelsius | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| fromCelsius | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| format | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| labelList | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toArray | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| toArrayWithLabels | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| toArrayPtBr | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| toArrayWithSymbols | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| metric | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| imperial | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| absolute | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace DevToolbelt\Enums\Measurement; |
| 6 | |
| 7 | use DevToolbelt\Enums\EnumInterface; |
| 8 | |
| 9 | enum Temperature: string implements EnumInterface |
| 10 | { |
| 11 | case CELSIUS = 'C'; |
| 12 | case FAHRENHEIT = 'F'; |
| 13 | case KELVIN = 'K'; |
| 14 | case RANKINE = 'R'; |
| 15 | case REAUMUR = 'Re'; |
| 16 | |
| 17 | public function label(): string |
| 18 | { |
| 19 | return match ($this) { |
| 20 | self::CELSIUS => 'Celsius', |
| 21 | self::FAHRENHEIT => 'Fahrenheit', |
| 22 | self::KELVIN => 'Kelvin', |
| 23 | self::RANKINE => 'Rankine', |
| 24 | self::REAUMUR => 'Réaumur', |
| 25 | }; |
| 26 | } |
| 27 | |
| 28 | public function fullNamePtBr(): string |
| 29 | { |
| 30 | return match ($this) { |
| 31 | self::CELSIUS => 'Celsius', |
| 32 | self::FAHRENHEIT => 'Fahrenheit', |
| 33 | self::KELVIN => 'Kelvin', |
| 34 | self::RANKINE => 'Rankine', |
| 35 | self::REAUMUR => 'Réaumur', |
| 36 | }; |
| 37 | } |
| 38 | |
| 39 | public function symbol(): string |
| 40 | { |
| 41 | return match ($this) { |
| 42 | self::CELSIUS => '°C', |
| 43 | self::FAHRENHEIT => '°F', |
| 44 | self::KELVIN => 'K', |
| 45 | self::RANKINE => '°R', |
| 46 | self::REAUMUR => '°Ré', |
| 47 | }; |
| 48 | } |
| 49 | |
| 50 | public function isMetric(): bool |
| 51 | { |
| 52 | return in_array($this, [self::CELSIUS, self::KELVIN], true); |
| 53 | } |
| 54 | |
| 55 | public function isImperial(): bool |
| 56 | { |
| 57 | return in_array($this, [self::FAHRENHEIT, self::RANKINE], true); |
| 58 | } |
| 59 | |
| 60 | public function isAbsolute(): bool |
| 61 | { |
| 62 | return in_array($this, [self::KELVIN, self::RANKINE], true); |
| 63 | } |
| 64 | |
| 65 | public function absoluteZero(): float |
| 66 | { |
| 67 | return match ($this) { |
| 68 | self::CELSIUS => -273.15, |
| 69 | self::FAHRENHEIT => -459.67, |
| 70 | self::KELVIN => 0.0, |
| 71 | self::RANKINE => 0.0, |
| 72 | self::REAUMUR => -218.52, |
| 73 | }; |
| 74 | } |
| 75 | |
| 76 | public function boilingPointOfWater(): float |
| 77 | { |
| 78 | return match ($this) { |
| 79 | self::CELSIUS => 100.0, |
| 80 | self::FAHRENHEIT => 212.0, |
| 81 | self::KELVIN => 373.15, |
| 82 | self::RANKINE => 671.67, |
| 83 | self::REAUMUR => 80.0, |
| 84 | }; |
| 85 | } |
| 86 | |
| 87 | public function freezingPointOfWater(): float |
| 88 | { |
| 89 | return match ($this) { |
| 90 | self::CELSIUS => 0.0, |
| 91 | self::FAHRENHEIT => 32.0, |
| 92 | self::KELVIN => 273.15, |
| 93 | self::RANKINE => 491.67, |
| 94 | self::REAUMUR => 0.0, |
| 95 | }; |
| 96 | } |
| 97 | |
| 98 | public function convertTo(float $value, self $target): float |
| 99 | { |
| 100 | if ($this === $target) { |
| 101 | return $value; |
| 102 | } |
| 103 | |
| 104 | $celsius = $this->toCelsius($value); |
| 105 | |
| 106 | return $target->fromCelsius($celsius); |
| 107 | } |
| 108 | |
| 109 | public function toCelsius(float $value): float |
| 110 | { |
| 111 | return match ($this) { |
| 112 | self::CELSIUS => $value, |
| 113 | self::FAHRENHEIT => ($value - 32) * 5 / 9, |
| 114 | self::KELVIN => $value - 273.15, |
| 115 | self::RANKINE => ($value - 491.67) * 5 / 9, |
| 116 | self::REAUMUR => $value * 5 / 4, |
| 117 | }; |
| 118 | } |
| 119 | |
| 120 | public function fromCelsius(float $celsius): float |
| 121 | { |
| 122 | return match ($this) { |
| 123 | self::CELSIUS => $celsius, |
| 124 | self::FAHRENHEIT => $celsius * 9 / 5 + 32, |
| 125 | self::KELVIN => $celsius + 273.15, |
| 126 | self::RANKINE => ($celsius + 273.15) * 9 / 5, |
| 127 | self::REAUMUR => $celsius * 4 / 5, |
| 128 | }; |
| 129 | } |
| 130 | |
| 131 | public function format(float $value, int $decimals = 2): string |
| 132 | { |
| 133 | return number_format($value, $decimals) . ' ' . $this->symbol(); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * @return string[] |
| 138 | */ |
| 139 | public function labelList(): array |
| 140 | { |
| 141 | return array_map(fn (self $case) => $case->label(), self::cases()); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * @return array<string, string> |
| 146 | */ |
| 147 | public static function toArray(): array |
| 148 | { |
| 149 | $result = []; |
| 150 | |
| 151 | foreach (self::cases() as $temperature) { |
| 152 | $result[$temperature->value] = $temperature->value; |
| 153 | } |
| 154 | |
| 155 | return $result; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * @return array<string, string> |
| 160 | */ |
| 161 | public static function toArrayWithLabels(): array |
| 162 | { |
| 163 | $result = []; |
| 164 | |
| 165 | foreach (self::cases() as $temperature) { |
| 166 | $result[$temperature->value] = $temperature->label(); |
| 167 | } |
| 168 | |
| 169 | return $result; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * @return array<string, string> |
| 174 | */ |
| 175 | public static function toArrayPtBr(): array |
| 176 | { |
| 177 | $result = []; |
| 178 | |
| 179 | foreach (self::cases() as $temperature) { |
| 180 | $result[$temperature->value] = $temperature->fullNamePtBr(); |
| 181 | } |
| 182 | |
| 183 | return $result; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * @return array<string, string> |
| 188 | */ |
| 189 | public static function toArrayWithSymbols(): array |
| 190 | { |
| 191 | $result = []; |
| 192 | |
| 193 | foreach (self::cases() as $temperature) { |
| 194 | $result[$temperature->value] = $temperature->symbol(); |
| 195 | } |
| 196 | |
| 197 | return $result; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * @return self[] |
| 202 | */ |
| 203 | public static function metric(): array |
| 204 | { |
| 205 | return array_filter(self::cases(), fn (self $temp) => $temp->isMetric()); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * @return self[] |
| 210 | */ |
| 211 | public static function imperial(): array |
| 212 | { |
| 213 | return array_filter(self::cases(), fn (self $temp) => $temp->isImperial()); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * @return self[] |
| 218 | */ |
| 219 | public static function absolute(): array |
| 220 | { |
| 221 | return array_filter(self::cases(), fn (self $temp) => $temp->isAbsolute()); |
| 222 | } |
| 223 | } |