binsoul/common-measurement

测量及其对应的单位和维度

dev-master / 1.0.x-dev 2016-04-28 17:33 UTC

This package is auto-updated.

Last update: 2024-09-14 22:05:52 UTC


README

Latest Version on Packagist Software License Total Downloads

此软件包提供了用于建模和使用测量及其对应单位和维度的类。

安装

通过 composer

$ composer require binsoul/common-measurement

用法

输出长度单位为米。

<?php

use BinSoul\Common\Measurement\Measure;
use BinSoul\Common\Measurement\Measure\LengthMeasure;
use BinSoul\Common\Measurement\SystemOfUnits\StandardUnits;
use BinSoul\Common\Measurement\SystemOfUnits\USCustomaryUnits;

include 'vendor/autoload.php';

function output(LengthMeasure $length)
{
    $standardUnits = new StandardUnits();
    $converted = $length->to($standardUnits->METRE);

    echo sprintf(
        "%s %s = %s %s\n",
        $length->getValue(),
        $length->getUnit()->getSymbol(),
        $converted->getValue(),
        $converted->getUnit()->getSymbol()
    );
}

$standardUnits = new StandardUnits();
$usUnits = new USCustomaryUnits($standardUnits);

output(Measure::valueOf(1, $standardUnits->CENTIMETRE)); // 1 cm = 0.01 m
output(Measure::valueOf(1, $standardUnits->KILOMETRE)); // 1 km = 1000 m
output(Measure::valueOf(1, $usUnits->INCH)); // 1 in = 0.0254 m
output(Measure::valueOf(1, $usUnits->MILE)); // 1 mi = 1609.344 m

测试

$ composer test

许可证

MIT 许可证 (MIT)。请参阅 许可证文件 获取更多信息。