pdobrovolny/数量

物理量和公式

资助包维护!
Patreon

2.1.0 2022-01-16 11:08 UTC

This package is auto-updated.

Last update: 2024-09-07 22:59:06 UTC


README

Php version

pipeline status coverage report

Donate to this project using Patreon

物理量和公式

安装

要安装,请使用 composer

composer require pdobrovolny/quantity

文档和用法

一个库,可以将各种数量表示为值对象,并具有将一个度量单位转换为另一个单位的能力。

国际单位制

基本单位

  • 物质的量
  • 电流
  • 长度
  • 光强度
  • 质量
  • 热力学温度
  • 时间

导出单位

  • 吸收剂量
  • 加速度
  • 角度
  • 面积
  • 电容
  • 催化活性
  • 电阻
  • 电荷
  • 电导
  • 等效剂量
  • 频率
  • 照度
  • 电感
  • 光通量
  • 磁通量
  • 磁通密度
  • 功率
  • 压力
  • 放射性
  • 立体角
  • 速度
  • 电压
  • 体积
  • 体积流量率

子单位

  • 角度
    • 角分
    • 角秒
  • 长度
    • 天文单位
    • 光年
    • 秒差距
  • 质量
    • 原子质量单位
  • 时间
    • 小时
    • 分钟
  • 摄氏度
  • 公顷
  • 光功率
  • 伏安

英制单位

  • 面积
    • 平方英里
    • 平方英尺
    • 平方英里
    • 平方英寸
    • 平方里
    • 平方英里
    • 平方码
  • 长度
    • 英尺
    • 英里
    • 英寸
    • 英里
  • 质量
    • 长吨
    • 盎司
    • 英石
  • 体积
    • 立方链
    • 立方英尺
    • 立方英里
    • 立方英寸
    • 立方里
    • 立方英里
    • 立方码
    • 英国
      • 液盎司
      • 加仑
      • 吉耳
      • 品脱
      • 夸脱
    • 美国
      • 液盎司
      • 加仑
      • 吉耳
      • 品脱
      • 夸脱
  • 华氏度

示例

主要示例

基本用法

$length = new Length(1.);

\var_dump($length->getValue());     // double(1)
\var_dump($length::UNIT);           // string(1) "m"

通过工厂创建数量

$quantityFactory = new QuantityFactory();

$length = $quantityFactory->createWithValue(ILength::class, 1., EMetricExponent::KILO);

\var_dump($length->getValue());     // double(1000)
\var_dump($length::class);          // string(50) "pdobrovolny\quantity\container\metric\basic\Length"

通过公式创建数量

$quantityFactory = new QuantityFactory();
$length = $quantityFactory->createWithValue(ILength::class, 100000);
$time = $quantityFactory->createWithValue(ITime::class, 3600);

$velocity = $quantityFactory->create(IVelocity::class, [$length, $time]);

\var_dump($velocity->getValue());   // double(27.777777777778)
\var_dump($velocity::class);        // string(54) "pdobrovolny\quantity\container\metric\derived\Velocity"

自定义工厂

final class MyLength implements ILength
{
    public function __construct(private float $value)
    {
    }

    public function getValue(): float
    {
        return $this->value;
    }
}

$quantityFactory = new QuantityFactory([
    ILength::class => autowire(MyLength::class),
]);

$length = $quantityFactory->createWithValue(ILength::class, 1.);
\var_dump($length::class);          // string(16) "example\MyLength"

转换示例

英里到长度

$quantityFactory = new QuantityFactory();
$mile = $quantityFactory->createWithValue(IMile::class, 1);

$length = $quantityFactory->create(ILength::class, [$mile]);

\var_dump($length->getValue());     // double(1609.344)

摄氏度到华氏度

$quantityFactory = new QuantityFactory();

$celsius = $quantityFactory->createWithValue(ICelsius::class, 20);
$thermodynamicTemperature = $quantityFactory->create(IThermodynamicTemperature::class, [$celsius]);
$fahrenheit = $quantityFactory->create(IFahrenheit::class, [$thermodynamicTemperature]);

\var_dump($fahrenheit->getValue());     // double(68)

格式化示例

创建 QuantityFormatter

$formatter = new QuantityFormatter(
    new \NumberFormatter('cs_CZ', \NumberFormatter::PATTERN_DECIMAL)
);

缩放数量

$quantity = new Length(1.);

echo $formatter->format($quantity) . "\n\n";

echo $formatter->format($quantity, EMetricExponent::MILI) . "\n";
echo $formatter->format($quantity, EMetricExponent::BASE) . "\n";
echo $formatter->format($quantity, EMetricExponent::DEKA) . "\n";
echo $formatter->format($quantity, EMetricExponent::KILO) . "\n";

输出

1 m

1 000 mm
1 m
0,1 dam
0,001 km

支持

我更喜欢让我的工作对每个人开放。为了做到这一点,我依赖 Patreon 上的自愿捐赠 Patreon