ayeo/temperature

处理温度单位的简单库

0.1.1 2014-12-19 01:37 UTC

This package is auto-updated.

Last update: 2024-09-18 07:39:18 UTC


README

构建状态 Scrutinizer 代码质量 软件许可 Packagist 版本 代码覆盖率 温度

处理温度单位的简单库。功能

  • 显示格式化的温度(例如 100 °C)
  • 在不同尺度间简单转换

支持的温度尺度

  • 摄氏度 (C)
  • 开尔文 (K)
  • 华氏度 (F)
  • 兰金度 (R)
  • 列氏度 (Re)

使用 Composer 安装

require: {
	"ayeo/temperature": "~0.1"
}

基本用法

use Temperature\Factory\DefaultFactory as TemperatureFactory;

$factory = new TemperatureFactory();
$temperature = $factory->build(63, 'F');

$temperature; //63 °F
$temperature->convert('C'); //17.2222222222 °C
$temperature->convert('C')->setPrecision(2); //17.22 °C

自动转换

您可以将工厂设置为自动将温度转换为指定的尺度

use Temperature\Factory\DefaultFactory as TemperatureFactory;

$factory = new TemperatureFactory;
$factory->setAutoconvertTo('C');
$factory->getFormatter()->setPrecision(2);

$factory->build(100, 'F'); //37.78 °C

自定义格式化器

默认格式化器由您的区域设置构建。您可以调整它以满足您的需求。

use Temperature\Formatter\StandardFormatter;

$formatter = new StandardFormatter();
$formatter->setDecimalSeperator(",");
$formatter->setPrecision(2);
$formatter->setShowSymbolMode(false);

$factory->setFormatter($formatter);

$factory->build(10.50, 'C'); //10,50
$factory->build(100, 'K'); //100

您可以编写自己的格式化器。我必须实现 FormatterInterface。

自定义温度尺度

假设您需要一个新温度尺度。为了本例的目的,假设 C2 = 2 * 摄氏度

use \Temperature\Scales\Scale\AbstractScale;

class C2Scale extends AbstractScale
{
	const SYMBOL = "C2";

	/**
	 * @return float
	 */
	function getValueInCelsius()
	{
		return $this->value / 2;
	}

	/**
	 * @param $celsius
	 */
	function setValueInCelsius($celsius)
	{
		$this->value = $celsius * 2;
	}
}

$factory->getSupportedScales()->addSupportedType('C2', 'C2Scale');
$factory->build(100, 'C')->convert('C2'); //200 C2
$factory->build(50, 'K')->convert('C2'); //-446 C2

许可

MIT 许可证 (MIT)。有关更多信息,请参阅许可文件