jacobfitzp/weight-conversions
最新稳定版本:1.1.0
Composer 安装命令:
composer require jacobfitzp/weight-conversions
包简介
This is my package weight-conversions
README 文档
README
Simple PHP package to convert between different units of weight measurement.
Supports gram, kilogram, milligram, ounce, pound, stone and ton... and can easily be extended to add additional units.
echo (new Pound(27.8)) ->to(Kilogram::class) ->round() ->formatted() // 12.6kg
Installation
You can install the package via composer:
composer require jacobfitzp/weight-conversions
Usage
// 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();
Extending
You can create your own units of measurement by extending the AbstractUnit class:
class Hobnobs extends AbstractUnit { public const RELATIVE_TO_KG = 50; public function formatted(string $format = '%s hobnobs'): string { return sprintf($format, $this->amount()); } }
Let's break down how this works... The main thing to be aware of is the RELATIVE_TO_KG constant, this represents the amount of the unit that is relative to 1kg - For exampe, above we are saying that 50 hobnobs = 1kg. This is used as the basis for all conversion calculations.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
统计信息
- 总下载量: 43
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-04-14