TemperatureUnit.php in Physical Fields 8
File
src/TemperatureUnit.php
View source
<?php
namespace Drupal\physical;
final class TemperatureUnit implements UnitInterface {
const KELVIN = 'K';
const CELSIUS = 'C';
const FAHRENHEIT = 'F';
public static function getLabels() {
return [
self::KELVIN => t('K'),
self::CELSIUS => t('°C'),
self::FAHRENHEIT => t('°F'),
];
}
public static function getBaseUnit() {
return self::CELSIUS;
}
public static function getBaseFactor($unit) {
self::assertExists($unit);
$factors = [
self::KELVIN => '274.15',
self::CELSIUS => '1',
self::FAHRENHEIT => '33.8',
];
return $factors[$unit];
}
public static function assertExists($unit) {
$allowed_units = [
self::KELVIN,
self::CELSIUS,
self::FAHRENHEIT,
];
if (!in_array($unit, $allowed_units)) {
throw new \InvalidArgumentException(sprintf('Invalid temperature unit "%s" provided.', $unit));
}
}
}