Library to handle temperature comparing, conversion and formatting.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src
tests
.editorconfig
.gitignore
.travis.yml
CHANGELOG.md
LICENSE.md
README.md
composer.json
composer.lock
phpunit.xml

README.md

Temperature

Library to handle temperature comparing, conversion and formatting.

Build Status Packagist Version Software License

Supported temperature scales

  • Celsius (C)
  • Kelvin (K)
  • Fahrenheit (F)
  • Rankine (R)
  • Reaumur (Re)

Getting Started

Prerequisites

This library requires at least PHP 7.1.

Installing

composer require damlys/temperature

Basic usage

This is how to use temperature library.

Creating

/** @var TemperatureFactoryInterface $factory */
$factory = new TemperatureFactory();

var_dump(
    $factory->make(36.6, TemperatureInterface::CELSIUS)
);

Outputs:

class Damlys\Temperature\CelsiusTemperature#3 (1) {
  private $value => double(36.6)
}

Converting

/** @var TemperatureConverterInterface $converter */
$converter = new TemperatureConverter(
    new ConversionFormulaFactory(),
    new TemperatureFactory()
);

/** @var TemperatureInterface $temperature */
$temperature = new CelsiusTemperature(36.6);

var_dump(
    $converter->convert(
        $temperature, 
        TemperatureInterface::KELVIN
    )
);

Outputs:

class Damlys\Temperature\KelvinTemperature#7 (1) {
  private $value => double(309.75)
}

Comparision

/** @var TemperatureComparatorInterface $comparator */
$comparator = new TemperatureComparator(
    new TemperatureConverter(
        new ConversionFormulaFactory(),
        new TemperatureFactory()
    )
);

/** @var TemperatureInterface $higherTemperature */
$higherTemperature = new CelsiusTemperature(36.6);
/** @var TemperatureInterface $lowerTemperature */
$lowerTemperature = new KelvinTemperature(303.15);

var_dump($comparator->compare($higherTemperature, '>', $lowerTemperature));
var_dump($comparator->compare($higherTemperature, '==', $lowerTemperature));
var_dump($comparator->compare($higherTemperature, '<', $lowerTemperature));

Outputs:

bool(true)
bool(false)
bool(false)

Check TemperatureComparatorInterface for available comparision operators.

Formatting

/** @var TemperatureFormatterInterface $formatter */ 
$formatter = new TemperatureFormatter();

$sunTemperature = new KelvinTemperature(5778.0);

var_dump(
    $formatter->format($sunTemperature)
);

Outputs:

string(9) "5,778.0 K"