vbpupil / measurement-converter
一个简单但有效的测量转换器,允许您快速创建可以轻松转换为其他测量格式的测量对象,例如将英制测量值转换为公制。
1.3.1
2018-04-04 08:46 UTC
Requires
- chippyash/strong-type: ^3.0
- symfony/var-dumper: ^3.3
Requires (Dev)
- phpunit/phpunit: <=5.7.21
This package is auto-updated.
Last update: 2024-09-07 00:02:07 UTC
README
测量转换器
一个简单但有效的测量转换器,允许您快速创建可以轻松转换为其他测量格式的测量对象,例如将英制测量值转换为公制。
目前此包支持以下内容
- 公制线性
- 英制线性
- 公制立方
- 英制立方
- 重量转换
- 吨
- 美吨
- 英吨
通过创建线性测量单位对象,您可以立即继承该转换的值到您单位的对应值。例如,通过创建一个 1 英寸对象,您也将立即获得 英尺、码 & 英里 的测量值。
支持的单位
使用示例
公制示例
1. 创建一个 50 英尺的对象
$feet= LinearUnitBuilder::build(new FloatType(18), new StringType('ft')); dump($feet->getHumanReadableLong());
2. 将其转换为毫米
$converter = new LinearUnitsConverter($feet, new StringType('mm'));
3. 获取新的毫米对象
$mm = $converter->get(); dump($mm->getHumanReadableLong());
4. 将毫米转换为英寸并获取英寸对象
$converter = new LinearUnitsConverter($mm, new StringType('in')); $inch = $converter->get(); dump($inch->getHumanReadableLong());
立方示例
1. 创建宽度、深度和高度对象并将这些传递给立方构造函数
$width = LinearUnitBuilder::build(new FloatType(18), new StringType('m')); $depth = LinearUnitBuilder::build(new FloatType(42), new StringType('m')); $height = LinearUnitBuilder::build(new FloatType(3), new StringType('cm')); $cubic = new CubicUnit($width, $depth, $height); dump($cubic->getValue(new StringType('mm')));
重量示例
1. 要将立方测量值转换为重量,只需传入支持的材质名称和立方对象
$tonnage = new WeightTonnageDensityConverter('soil', $cubic); dump($cubic); dump($tonnage->getValue());
2. 您也可以提供自己的密度测量值,这将作为 custom
添加到密度数组中
$tonnage = new WeightTonnageDensityConverter(1.2, $cubic); dump($cubic); dump($tonnage->getValue());
简单转换示例
1. 首先创建一个简单的 20 英尺英制线性单位对象
$length = LinearUnitBuilder::build(new FloatType(20), new StringType('ft'));
2. 然后将此转换为厘米公制线性单位对象
$length = LinearUnitBuilder::build( new FloatType((new Conversion($l))->into( new StringType('cm') )), new StringType( 'cm' ));