jacobfitzp/weight-conversions

这是我的重量转换包weight-conversions

1.1.0 2023-04-15 17:30 UTC

README

Latest Version on Packagist Tests Total Downloads

这是一个简单的PHP包,用于在不同的重量单位之间进行转换。

支持克、千克、毫克、盎司、磅、石和吨等,并且可以轻松扩展以添加更多单位。

echo (new Pound(27.8))
    ->to(Kilogram::class)
    ->round()
    ->formatted()

// 12.6kg

安装

您可以通过composer安装此包

composer require jacobfitzp/weight-conversions

使用方法

// Create a new measurement unit
$kilograms = new Kilogram();

// Set weight amount
// Can also be passed to the constructor
$kilograms->setAmount(5.75);

// Convert to a different unit type
$ounce = $kilograms->to(Ounce::class);

// Get weight amount
$ounce->amount();

// Round the amount
// Defaults to a precision of 1 decimal place
$ounce->round();

// Get a formatted human-readable version of the amount
// For example "2kgG", "12st 5" 
$ounce->formatted();

扩展

您可以通过扩展AbstractUnit类来创建自己的度量单位

class Hobnobs extends AbstractUnit
{
    public const RELATIVE_TO_KG = 50;
    
    public function formatted(string $format = '%s hobnobs'): string
    {
        return sprintf($format, $this->amount());
    }
}

让我们来分解一下它是如何工作的... 需要注意的主要是RELATIVE_TO_KG常量,这代表与1千克相对的单位数量 - 例如,上面我们说50块曲奇饼干等于1千克。这是所有转换计算的基础。

测试

composer test

变更日志

有关最近更改的更多信息,请参阅变更日志

贡献

有关详细信息,请参阅贡献指南

安全漏洞

有关如何报告安全漏洞,请审查我们的安全策略

致谢

许可

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