You are here

final class TemperatureUnit in Physical Fields 8

Provides common temperature units.

Hierarchy

Expanded class hierarchy of TemperatureUnit

File

src/TemperatureUnit.php, line 8

Namespace

Drupal\physical
View source
final class TemperatureUnit implements UnitInterface {
  const KELVIN = 'K';
  const CELSIUS = 'C';
  const FAHRENHEIT = 'F';

  /**
   * {@inheritdoc}
   */
  public static function getLabels() {
    return [
      self::KELVIN => t('K'),
      self::CELSIUS => t('°C'),
      self::FAHRENHEIT => t('°F'),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public static function getBaseUnit() {
    return self::CELSIUS;
  }

  /**
   * {@inheritdoc}
   */
  public static function getBaseFactor($unit) {
    self::assertExists($unit);

    // Not used, see Temperature::convert().
    $factors = [
      self::KELVIN => '274.15',
      self::CELSIUS => '1',
      self::FAHRENHEIT => '33.8',
    ];
    return $factors[$unit];
  }

  /**
   * {@inheritdoc}
   */
  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));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TemperatureUnit::assertExists public static function Asserts that the given unit exists. Overrides UnitInterface::assertExists
TemperatureUnit::CELSIUS constant
TemperatureUnit::FAHRENHEIT constant
TemperatureUnit::getBaseFactor public static function Gets the base factor for the given unit. Overrides UnitInterface::getBaseFactor
TemperatureUnit::getBaseUnit public static function Gets the base unit. Overrides UnitInterface::getBaseUnit
TemperatureUnit::getLabels public static function Gets the labels for the defined units. Overrides UnitInterface::getLabels
TemperatureUnit::KELVIN constant