定制 drieschel/units-of-measurement 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

drieschel/units-of-measurement

Composer 安装命令:

composer require drieschel/units-of-measurement

包简介

Drieschel's Units of Measurement

README 文档

README

This library is created to convert between different unit systems as well as converting metric values between their prefixes.

Examples

Converting units

<?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);

Converting prefixes

Imagine you have an application which can handle values in microgram (µg) but the provided data is in kilogram (kg). The data can get converted from kilogram to microgram easily with the SI prefix converter component.

<?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);

The Units

Collections of units can be created by using static factory methods from the unit collection.

<?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);

The physical quantities

The physical quantity collection can be accessed as multidimensional array (read only) to get a specific unit or to get a physical quantity.

<?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'];

统计信息

  • 总下载量: 3
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-06-26

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固