You are here

final class Temperature in Physical Fields 8

Provides a value object for temperature amounts.

Usage example:

$temperature = new Temperature('98.6', TemperatureUnit::FAHRENHEIT);

Hierarchy

Expanded class hierarchy of Temperature

1 file declares its use of Temperature
TemperatureTest.php in tests/src/Unit/TemperatureTest.php
1 string reference to 'Temperature'
MeasurementType::getLabels in src/MeasurementType.php
Gets the labels for the defined measurement types.

File

src/Temperature.php, line 13

Namespace

Drupal\physical
View source
final class Temperature extends Measurement {

  /**
   * The measurement type.
   *
   * @var string
   */
  protected $type = MeasurementType::TEMPERATURE;

  /**
   * Converts the current temperature measurement to a new unit.
   *
   * @param string $new_unit
   *   The new unit.
   *
   * @return static
   *   The resulting temperature.
   */
  public function convert($new_unit) {
    TemperatureUnit::assertExists($new_unit);

    // The base factors are not used because the formulas change depending
    // on the units being converted.
    $new_number = '0';
    switch ($this->unit) {
      case TemperatureUnit::KELVIN:
        if ($new_unit == TemperatureUnit::CELSIUS) {

          // http://www.rapidtables.com/convert/temperature/kelvin-to-celsius.htm
          $new_number = Calculator::subtract($this->number, '273.15');
        }
        else {

          // @see http://www.rapidtables.com/convert/temperature/kelvin-to-fahrenheit.htm
          $new_number = Calculator::subtract(Calculator::multiply($this->number, '1.8'), '459.67');
        }
        break;
      case TemperatureUnit::CELSIUS:
        if ($new_unit == TemperatureUnit::FAHRENHEIT) {

          // http://www.rapidtables.com/convert/temperature/how-celsius-to-fahrenheit.htm
          $new_number = Calculator::add(Calculator::multiply($this->number, '1.8'), '32');
        }
        else {

          // http://www.rapidtables.com/convert/temperature/celsius-to-kelvin.htm
          $new_number = Calculator::add($this->number, '273.15');
        }
        break;
      case TemperatureUnit::FAHRENHEIT:
        if ($new_unit == TemperatureUnit::CELSIUS) {

          // @see http://www.rapidtables.com/convert/temperature/fahrenheit-to-celsius.htm
          $new_number = Calculator::divide(Calculator::subtract($this->number, '32'), '1.8');
        }
        else {

          // http://www.rapidtables.com/convert/temperature/fahrenheit-to-kelvin.htm
          $new_number = Calculator::divide(Calculator::add($this->number, '459.67'), '1.8');
        }
        break;
    }
    return new static($new_number, $new_unit);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Measurement::$number protected property The number.
Measurement::$unit protected property The unit.
Measurement::add public function Adds the given measurement to the current one.
Measurement::compareTo public function Compares the current measurement with the given one.
Measurement::divide public function Divides the current measurement by the given number.
Measurement::equals public function Gets whether the current measurement is equivalent to the given one.
Measurement::getNumber public function Gets the number.
Measurement::getUnit public function Gets the unit.
Measurement::greaterThan public function Gets whether the current measurement is greater than the given one.
Measurement::greaterThanOrEqual public function Gets whether the current measurement is greater than or equal to the given one.
Measurement::isZero public function Gets whether the current measurement is zero.
Measurement::lessThan public function Gets whether the current measurement is lesser than the given measurement.
Measurement::lessThanOrEqual public function Gets whether the current measurement is lesser than or equal to the given measurement.
Measurement::multiply public function Multiplies the current measurement by the given number.
Measurement::round public function Rounds the current measurement.
Measurement::subtract public function Subtracts the given measurement from the current one.
Measurement::toArray public function Gets the array representation of the measurement.
Measurement::__construct public function Constructs a new Measurement object.
Measurement::__toString public function Gets the string representation of the measurement.
Temperature::$type protected property The measurement type. Overrides Measurement::$type
Temperature::convert public function Converts the current temperature measurement to a new unit. Overrides Measurement::convert