drieschel / units-of-measurement
Drieschel的测量单位
v0.3.0
2020-10-17 08:55 UTC
Requires
- php: >=7.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.15
- phpunit/phpunit: ^9.1
README
此库旨在在不同的单位系统之间进行转换,以及将公制值转换为它们的词头。
示例
单位转换
<?php use Drieschel\UnitsOfMeasurement\SiPrefixConverter; use Drieschel\UnitsOfMeasurement\UnitCollection; use Drieschel\UnitsOfMeasurement\UnitConverter; // Own units and/or prefixes can be used by providing them in the constructor. Both is optional. $units = UnitCollection::createAllUnits(); $siPrefixes = SiPrefixCollection::create(); $unitConverter = new UnitConverter($units, $siPrefixes); // Converting miles to kilometre ($converted = 20.1168). $converted = $unitConverter->convertBySymbol('mi', 'km', 12.5);
词头转换
假设你有一个可以处理微克(µg)值的程序,但提供的数据是在千克(kg)中。数据可以通过SI词头转换组件轻松地从千克转换为微克。
<?php use Drieschel\UnitsOfMeasurement\SiPrefixCollection; use Drieschel\UnitsOfMeasurement\SiPrefixConverter; use Drieschel\UnitsOfMeasurement\UnitCollection; // Own units and/or prefixes can be used by providing them in the constructor. Both is optional. $units = UnitCollection::createAllUnits(); $siPrefixes = SiPrefixCollection::create(); $prefixConverter = new SiPrefixConverter($units, $siPrefixes); // Note: The second argument from the "convertByUnitSymbol" method is the prefix symbol. // Converting litre to centilitre ($converted = 37.5). $centilitre = $prefixConverter->convertByUnitSymbol('L', 'c', 3.75); // Converting microgram to milligram ($milligram = 0.0021). $milligram = $prefixConverter->convertByUnitSymbol('µg', 'm', 2.1); // Note: The empty string ('') is the prefix symbol for 1E0 (1.). // Converting kilotonne to tonne ($tonne = 230.). $tonne = $prefixConverter->convertByUnitSymbol('kt', '', 0.23);
单位
可以通过单位集合的静态工厂方法创建单位集合。
<?php use Drieschel\UnitsOfMeasurement\UnitCollection; use Drieschel\UnitsOfMeasurement\PhysicalQuantityCollection; // Create all defined units. If the US Customary system is preferred over the // Imperial unit system, then the second (optional) argument has to be set to true. // It doesn't matter which system is preferred. Both unit versions (Imperial and USC) // will be created every time, the preferred version just has no unit system prefix // in the symbol. $physicalQuantities = PhysicalQuantityCollection::create(); $preferUsc = true; $units = UnitCollection::createAllUnits($physicalQuantities, $preferUsc); // A unit can get accessed by array notation or by get method. // This is the US preferred version, because the $preferUsc flag was set to true. $usGallon = $units['gal']; $usGallon = $units->get('gal'); $impGallon = $units['imp gal']; $impGallon = $units->get('imp gal'); // Create SI base units only. $siBaseUnits = UnitCollection::createSiBaseUnits($physicalQuantities); // Create SI derived units only. $siDerivedUnits = UnitCollection::createSiDerivedUnits($physicalQuantities, $siBaseUnits); // Create non SI units only. // The preferred non metric unit system can be chosen as in the "createAllUnits" method call. $nonSiUnits = UnitCollection::createNonSiUnits($physicalQuantities, $siBaseUnits, $preferUsc);
物理量
物理量集合可以通过多维数组(只读)访问,以获取特定的单位或物理量。
<?php use Drieschel\UnitsOfMeasurement\PhysicalQuantity; use Drieschel\UnitsOfMeasurement\PhysicalQuantityCollection; use Drieschel\UnitsOfMeasurement\SiBaseUnit; use Drieschel\UnitsOfMeasurement\NonSiUnit; // The $createUnits parameter can be set to true for creating all units at once. // The $preferUsc can be set to true if the US Customary unit system is preferred // over the Imperial unit system. $createUnits = true; $preferUsc = true; $physicalQuantities = PhysicalQuantityCollection::create($createUnits, $preferUsc); /** @var SiBaseUnit $metre */ $metre = $physicalQuantities['l']['m']; $metre = $physicalQuantities->get('l')->get('m'); /** @var NonSiUnit $litre */ $litre = $physicalQuantities['V']['L']; /** @var PhysicalQuantity $mass */ $mass = $physicalQuantities['m'];