final class Temperature in Physical Fields 8
Provides a value object for temperature amounts.
Usage example:
$temperature = new Temperature('98.6', TemperatureUnit::FAHRENHEIT);
Hierarchy
- class \Drupal\physical\Measurement
- class \Drupal\physical\Temperature
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\physicalView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Measurement:: |
protected | property | The number. | |
Measurement:: |
protected | property | The unit. | |
Measurement:: |
public | function | Adds the given measurement to the current one. | |
Measurement:: |
public | function | Compares the current measurement with the given one. | |
Measurement:: |
public | function | Divides the current measurement by the given number. | |
Measurement:: |
public | function | Gets whether the current measurement is equivalent to the given one. | |
Measurement:: |
public | function | Gets the number. | |
Measurement:: |
public | function | Gets the unit. | |
Measurement:: |
public | function | Gets whether the current measurement is greater than the given one. | |
Measurement:: |
public | function | Gets whether the current measurement is greater than or equal to the given one. | |
Measurement:: |
public | function | Gets whether the current measurement is zero. | |
Measurement:: |
public | function | Gets whether the current measurement is lesser than the given measurement. | |
Measurement:: |
public | function | Gets whether the current measurement is lesser than or equal to the given measurement. | |
Measurement:: |
public | function | Multiplies the current measurement by the given number. | |
Measurement:: |
public | function | Rounds the current measurement. | |
Measurement:: |
public | function | Subtracts the given measurement from the current one. | |
Measurement:: |
public | function | Gets the array representation of the measurement. | |
Measurement:: |
public | function | Constructs a new Measurement object. | |
Measurement:: |
public | function | Gets the string representation of the measurement. | |
Temperature:: |
protected | property |
The measurement type. Overrides Measurement:: |
|
Temperature:: |
public | function |
Converts the current temperature measurement to a new unit. Overrides Measurement:: |