initial commit; version 22.5.12042

This commit is contained in:
2022-12-12 23:28:25 -05:00
commit af1b03d79f
17653 changed files with 22692970 additions and 0 deletions

View File

@ -0,0 +1,276 @@
<?php
namespace PhpUnitsOfMeasure;
abstract class AbstractPhysicalQuantity implements PhysicalQuantityInterface
{
/**
* The collection of units in which this quantity can be represented.
*
* Commented out to ensure that each child class defines its own instance of this static.
*
* @var UnitOfMeasureInterface[]
*/
// protected static $unitDefinitions;
/**
* Static cache for unit lookups.
*
* @var UnitOfMeasureInterface[]
*/
private static $unitCache = [];
/**
* Create a cache key for the unit lookup cache.
*
* @var UnitOfMeasureInterface[]
*/
private static function buildUnitCacheKey($unit)
{
return get_called_class() . '#' . $unit;
}
/**
* Register a new unit of measure for all instances of this this physical quantity.
*
* @throws Exception\DuplicateUnitNameOrAlias If the unit name or any alias already exists
*
* @param UnitOfMeasureInterface $unit The new unit of measure
*/
public static function addUnit(UnitOfMeasureInterface $unit)
{
if (static::unitNameOrAliasesAlreadyRegistered($unit)) {
throw new Exception\DuplicateUnitNameOrAlias([
':labels' => implode(', ', array_merge([$unit->getName()], $unit->getAliases()))
]);
}
static::$unitDefinitions[] = $unit;
}
/**
* Get the unit of measure that matches the given name by either name or alias.
*
* @param string $unit A name or alias by which the unit is known.
*
* @throws Exception\UnknownUnitOfMeasure when an unknown unit of measure is given
*
* @return UnitOfMeasureInterface
*/
public static function getUnit($unit)
{
// If this class hasn't been initialized yet, do so now
if (!is_array(static::$unitDefinitions)) {
static::$unitDefinitions = [];
static::initialize();
}
$key = static::buildUnitCacheKey($unit);
if (isset(self::$unitCache[$key])) {
return self::$unitCache[$key];
}
foreach (static::$unitDefinitions as $unitOfMeasure) {
if ($unit === $unitOfMeasure->getName() || $unitOfMeasure->isAliasOf($unit)) {
return self::$unitCache[$key] = $unitOfMeasure;
}
}
throw new Exception\UnknownUnitOfMeasure([':unit' => $unit]);
}
/**
* Given a unit of measure, determine if its name or any of its aliases conflict
* with the set of already-known unit names and aliases.
*
* @param UnitOfMeasureInterface $unit The unit in question
*
* @return boolean true if there is a conflict, false if there is not
*/
protected static function unitNameOrAliasesAlreadyRegistered(UnitOfMeasureInterface $unit)
{
// If this class hasn't been initialized yet, do so now
if (!is_array(static::$unitDefinitions)) {
static::$unitDefinitions = [];
static::initialize();
}
$currentUnitNamesAndAliases = [];
foreach (static::$unitDefinitions as $unitOfMeasure) {
$currentUnitNamesAndAliases[] = $unitOfMeasure->getName();
$currentUnitNamesAndAliases = array_merge($currentUnitNamesAndAliases, $unitOfMeasure->getAliases());
}
$newUnitNamesAndAliases = array_merge([$unit->getName()], $unit->getAliases());
return count(array_intersect($currentUnitNamesAndAliases, $newUnitNamesAndAliases)) > 0;
}
/**
* Initialize the static properties of this quantity class, such as the set of
* default units of measure.
*/
protected static function initialize()
{
}
/**
* The scalar value, in the original unit of measure.
*
* @var float
*/
protected $originalValue;
/**
* The original unit of measure's string representation.
*
* @var string
*/
protected $originalUnit;
/**
* Store the value and its original unit.
*
* @param float $value The scalar value of the measurement
* @param string $unit The unit of measure in which this value is provided
*
* @throws Exception\NonNumericValue If the value is not numeric
* @throws Exception\NonStringUnitName If the unit is not a string
*/
public function __construct($value, $unit)
{
if (!is_numeric($value)) {
throw new Exception\NonNumericValue([':value' => $value]);
}
if (!is_string($unit)) {
throw new Exception\NonStringUnitName([':name' => $unit]);
}
$this->originalValue = $value;
$this->originalUnit = $unit;
}
/**
* @see \PhpUnitsOfMeasure\PhysicalQuantityInterface::toUnit
*/
public function toUnit($toUnit)
{
return $this->toUnitOfMeasure(static::getUnit($toUnit));
}
/**
* Convert this quantity to the given unit of measure.
*
* @param UnitOfMeasureInterface $unit The object representing the target unit of measure.
*
* @return float This quantity's value in the given unit of measure.
*/
private function toUnitOfMeasure(UnitOfMeasureInterface $unit)
{
$thisValueInNativeUnit = $this->toNativeUnit();
return $unit->convertValueFromNativeUnitOfMeasure($thisValueInNativeUnit);
}
/**
* @see \PhpUnitsOfMeasure\PhysicalQuantityInterface::toNativeUnit
*/
public function toNativeUnit()
{
return static::getUnit($this->originalUnit)
->convertValueToNativeUnitOfMeasure($this->originalValue);
}
/**
* @see \PhpUnitsOfMeasure\PhysicalQuantityInterface::__toString
*/
public function __toString()
{
return trim($this->originalValue . ' ' . static::getUnit($this->originalUnit)->getName());
}
/**
* @see \PhpUnitsOfMeasure\PhysicalQuantityInterface::add
*/
public function add(PhysicalQuantityInterface $quantity)
{
if (!$this->isEquivalentQuantity($quantity)) {
throw new Exception\PhysicalQuantityMismatch([
':lhs' => (string) $this,
':rhs' => (string) $quantity
]);
}
$quantityValueInThisOriginalUnit = $quantity->toUnitOfMeasure(static::getUnit($this->originalUnit));
$newValue = $this->originalValue + $quantityValueInThisOriginalUnit;
return new static($newValue, static::getUnit($this->originalUnit)->getName());
}
/**
* @see \PhpUnitsOfMeasure\PhysicalQuantityInterface::subtract
*/
public function subtract(PhysicalQuantityInterface $quantity)
{
if (!$this->isEquivalentQuantity($quantity)) {
throw new Exception\PhysicalQuantityMismatch([
':lhs' => (string) $this,
':rhs' => (string) $quantity
]);
}
$quantityValueInThisOriginalUnit = $quantity->toUnitOfMeasure(static::getUnit($this->originalUnit));
$newValue = $this->originalValue - $quantityValueInThisOriginalUnit;
return new static($newValue, static::getUnit($this->originalUnit)->getName());
}
/**
* @see \PhpUnitsOfMeasure\PhysicalQuantityInterface::isEquivalentQuantity
*/
public function isEquivalentQuantity(PhysicalQuantityInterface $testQuantity)
{
return get_class($this) === get_class($testQuantity);
}
/**
* @see \PhpUnitsOfMeasure\PhysicalQuantityInterface::isUnitDefined
*/
public static function isUnitDefined($name)
{
$units = static::getUnitDefinitions();
foreach ($units as $unit) {
if ($name === $unit->getName() || $unit->isAliasOf($name)) {
return true;
}
}
return false;
}
/**
* @see \PhpUnitsOfMeasure\PhysicalQuantityInterface::listAllUnits
*/
public static function listAllUnits()
{
$return = [];
$units = static::getUnitDefinitions();
foreach ($units as $unit) {
$return[$unit->getName()] = $unit->getAliases();
}
return $return;
}
/**
* Get the unit definition array
* @return Array $unitDefinitions
*/
public static function getUnitDefinitions()
{
if (!is_array(static::$unitDefinitions)) {
static::$unitDefinitions = [];
static::initialize();
}
return static::$unitDefinitions;
}
}

View File

@ -0,0 +1,26 @@
<?php
namespace PhpUnitsOfMeasure\Exception;
use Exception;
abstract class AbstractPhysicalQuantityException extends Exception
{
/**
* The error message template for this exception.
*
* @var string
*/
protected $error = '';
/**
* given an array of replacement values, assemble the error message for
* this exception.
*
* @param array $parameters key/value replacement pairs for the error message.
*/
public function __construct(array $parameters = [])
{
$message = strtr($this->error, $parameters);
parent::__construct($message);
}
}

View File

@ -0,0 +1,7 @@
<?php
namespace PhpUnitsOfMeasure\Exception;
class DuplicateUnitNameOrAlias extends AbstractPhysicalQuantityException
{
protected $error = 'The unit has a name or alias (:labels) which is already a registered unit for this quantity.';
}

View File

@ -0,0 +1,7 @@
<?php
namespace PhpUnitsOfMeasure\Exception;
class NonNumericValue extends AbstractPhysicalQuantityException
{
protected $error = 'Value (:value) must be numeric.';
}

View File

@ -0,0 +1,7 @@
<?php
namespace PhpUnitsOfMeasure\Exception;
class NonStringUnitName extends AbstractPhysicalQuantityException
{
protected $error = 'Unit name or alias (:name) must be a string value.';
}

View File

@ -0,0 +1,7 @@
<?php
namespace PhpUnitsOfMeasure\Exception;
class PhysicalQuantityMismatch extends AbstractPhysicalQuantityException
{
protected $error = '(:lhs) and (:rhs) cannot be added or subtracted because they are not the same physical quantity.';
}

View File

@ -0,0 +1,7 @@
<?php
namespace PhpUnitsOfMeasure\Exception;
class UnknownUnitOfMeasure extends AbstractPhysicalQuantityException
{
protected $error = 'Unknown unit of measure (:unit).';
}

View File

@ -0,0 +1,201 @@
<?php
namespace PhpUnitsOfMeasure;
/**
* Physical quantities with this trait
* have units which are metric and therefore have
* a standard set of prefixes.
*/
trait HasSIUnitsTrait
{
/**
* Given the patterns for generating the names and aliases,
* generate the various metric units of measure and add
* them to this physical quantity.
*
* Names and Aliases are created by replacing identifiers in the respective
* patterns. The current allowed replacement identifiers are:
* %p = the abbreviated SI prefix, like 'M' for 'megameter' or 'k' for 'kilogram'
* %P = the full SI prefix, like 'mega' for 'megameter' or 'kilo' for 'kilogram'
* %U = uppercase version of %P
*
* So for instance, in order to generate 'kg', 'mg', and 'g' names for SI units, the
* appropriate pattern would be '%pg'. Similarly, to generate 'kilogram', 'milligram',
* and 'gram' aliases, the pattern would be '%Pgram'.
*
* The $siUnit given in the 1st parameter must be some SI unit in the series of units
* to be generated by this method. This value is necessary to establish a conversion
* factor between this continuum of SI units and the Physical Quantity's native unit.
*
* The second parameter provides a scaling factor between the given SI unit in the first parameter
* and the base unit of the SI continuum (ie, 'grams', 'meters', 'seconds', etc). For instance,
* if a Kilogram unit of measure was passed for the 1st parameter, it would be necessary to
* then pass 1e-3 in the 2nd parameter to indicate that a gram is 1/1000 of the given unit.
*
* @param UnitOfMeasure $siUnit A unit in this physical quantity that is an SI unit of measure
* @param integer $toBaseSiUnitFactor The power-of-ten factor that converts the given SI unit into the not-prefixed SI base unit (ie 1e-3 for kilograms)
* @param string $namePattern The pattern to apply to the base unit's name to generate a new SI unit name
* @param array $aliasPatterns The collection of alias patterns to use in generating a new SI unit's aliases
* @param integer $powerFactor Use power factor for squares, qubic and other multiplication of SI factor (ie. square is 2, qubic is 3)
*/
protected static function addMissingSIPrefixedUnits(
UnitOfMeasure $siUnit,
$toBaseSiUnitFactor,
$namePattern,
array $aliasPatterns = [],
$powerFactor = NULL
) {
/**
* The standard set of SI prefixes
*/
$siPrefixes = [
[
'abbr_prefix' => 'Y',
'long_prefix' => 'yotta',
'factor' => 1e24
],
[
'abbr_prefix' => 'Z',
'long_prefix' => 'zetta',
'factor' => 1e21
],
[
'abbr_prefix' => 'E',
'long_prefix' => 'exa',
'factor' => 1e18
],
[
'abbr_prefix' => 'P',
'long_prefix' => 'peta',
'factor' => 1e15
],
[
'abbr_prefix' => 'T',
'long_prefix' => 'tera',
'factor' => 1e12
],
[
'abbr_prefix' => 'G',
'long_prefix' => 'giga',
'factor' => 1e9
],
[
'abbr_prefix' => 'M',
'long_prefix' => 'mega',
'factor' => 1e6
],
[
'abbr_prefix' => 'k',
'long_prefix' => 'kilo',
'factor' => 1e3
],
[
'abbr_prefix' => 'h',
'long_prefix' => 'hecto',
'factor' => 1e2
],
[
'abbr_prefix' => 'da',
'long_prefix' => 'deca',
'factor' => 1e1
],
[
'abbr_prefix' => '',
'long_prefix' => '',
'factor' => 1
],
[
'abbr_prefix' => 'd',
'long_prefix' => 'deci',
'factor' => 1e-1
],
[
'abbr_prefix' => 'c',
'long_prefix' => 'centi',
'factor' => 1e-2
],
[
'abbr_prefix' => 'm',
'long_prefix' => 'milli',
'factor' => 1e-3
],
[
'abbr_prefix' => 'µ',
'long_prefix' => 'micro',
'factor' => 1e-6
],
[
'abbr_prefix' => 'n',
'long_prefix' => 'nano',
'factor' => 1e-9
],
[
'abbr_prefix' => 'p',
'long_prefix' => 'pico',
'factor' => 1e-12
],
[
'abbr_prefix' => 'f',
'long_prefix' => 'femto',
'factor' => 1e-15
],
[
'abbr_prefix' => 'a',
'long_prefix' => 'atto',
'factor' => 1e-18
],
[
'abbr_prefix' => 'z',
'long_prefix' => 'zepto',
'factor' => 1e-21
],
[
'abbr_prefix' => 'y',
'long_prefix' => 'yocto',
'factor' => 1e-24
],
];
// Determine the conversion factor from the no-prefix SI unit to the physical quantity's native unit
$noPrefixToNativeUnitFactor = $siUnit->convertValueToNativeUnitOfMeasure(1) * $toBaseSiUnitFactor;
// For each of the standard SI prefixes, attempt to register a new unit of measure
foreach ($siPrefixes as $prefixDefinition) {
// Build a function for resolving a pattern into a unit name
$parsePattern = function ($pattern) use ($prefixDefinition) {
return strtr(
$pattern,
[
'%p' => $prefixDefinition['abbr_prefix'],
'%P' => $prefixDefinition['long_prefix'],
'%U' => strtoupper($prefixDefinition['long_prefix'])
]
);
};
// Generate the base name of the new unit
$name = $parsePattern($namePattern);
// Determine the factor that converts the new unit into the physical quantity's
// native unit of measure.
if (is_int($powerFactor) && $powerFactor != 0) {
$prefixDefinition['factor'] = pow($prefixDefinition['factor'], $powerFactor);
}
$toNativeUnitFactor = $noPrefixToNativeUnitFactor * $prefixDefinition['factor'];
// Instantiate the new unit of measure
$newUnit = UnitOfMeasure::linearUnitFactory($name, $toNativeUnitFactor);
// Generate the aliases of the new unit
foreach ($aliasPatterns as $aliasPattern) {
$newUnitAlias = $parsePattern($aliasPattern);
$newUnit->addAlias($newUnitAlias);
}
// If the unit doesn't conflict with any of the already-existing units, register it
if (!static::unitNameOrAliasesAlreadyRegistered($newUnit)) {
static::addUnit($newUnit);
}
}
}
}

View File

@ -0,0 +1,38 @@
<?php
namespace PhpUnitsOfMeasure\PhysicalQuantity;
use PhpUnitsOfMeasure\AbstractPhysicalQuantity;
use PhpUnitsOfMeasure\HasSIUnitsTrait;
use PhpUnitsOfMeasure\UnitOfMeasure;
class Acceleration extends AbstractPhysicalQuantity
{
use HasSIUnitsTrait;
protected static $unitDefinitions;
protected static function initialize()
{
// meters per second squared
$meterpersecondsquared = UnitOfMeasure::nativeUnitFactory('m/s^2');
$meterpersecondsquared->addAlias('m/s²');
$meterpersecondsquared->addAlias('meter per second squared');
$meterpersecondsquared->addAlias('meters per second squared');
$meterpersecondsquared->addAlias('metre per second squared');
$meterpersecondsquared->addAlias('metres per second squared');
static::addUnit($meterpersecondsquared);
static::addMissingSIPrefixedUnits(
$meterpersecondsquared,
1,
'%pm/s^2',
[
'%pm/s²',
'%Pmeter per second squared',
'%Pmeters per second squared',
'%Pmetre per second squared',
'%Pmetres per second squared',
]
);
}
}

View File

@ -0,0 +1,80 @@
<?php
namespace PhpUnitsOfMeasure\PhysicalQuantity;
use PhpUnitsOfMeasure\AbstractPhysicalQuantity;
use PhpUnitsOfMeasure\UnitOfMeasure;
use PhpUnitsOfMeasure\HasSIUnitsTrait;
class Angle extends AbstractPhysicalQuantity
{
use HasSIUnitsTrait;
protected static $unitDefinitions;
protected static function initialize()
{
// Radians
$radian = UnitOfMeasure::nativeUnitFactory('rad');
$radian->addAlias('radian');
$radian->addAlias('radians');
static::addUnit($radian);
static::addMissingSIPrefixedUnits(
$radian,
1,
'%prad',
[
'%Pradian',
'%Pradians',
]
);
// Degrees
$degree = UnitOfMeasure::linearUnitFactory('deg', M_PI / 180);
$degree->addAlias('°');
$degree->addAlias('degree');
$degree->addAlias('degrees');
static::addUnit($degree);
static::addMissingSIPrefixedUnits(
$degree,
1,
'%pdeg',
[
'%Pdegree',
'%Pdegrees',
]
);
// Arcminute
$arcminute = UnitOfMeasure::linearUnitFactory('arcmin', M_PI / 180 / 60);
$arcminute->addAlias('');
$arcminute->addAlias('arcminute');
$arcminute->addAlias('arcminutes');
$arcminute->addAlias('amin');
$arcminute->addAlias('am');
$arcminute->addAlias('MOA');
static::addUnit($arcminute);
// Arcsecond
$arcsecond = UnitOfMeasure::linearUnitFactory('arcsec', M_PI / 180 / 3600);
$arcsecond->addAlias('″');
$arcminute->addAlias('arcsecond');
$arcminute->addAlias('arcseconds');
$arcsecond->addAlias('asec');
$arcsecond->addAlias('as');
static::addUnit($arcsecond);
static::addMissingSIPrefixedUnits(
$arcsecond,
1,
'%Parcsec',
[
'%Parcsecond',
'%Parcseconds',
'%pasec',
'%pas',
]
);
}
}

View File

@ -0,0 +1,103 @@
<?php
namespace PhpUnitsOfMeasure\PhysicalQuantity;
use PhpUnitsOfMeasure\AbstractPhysicalQuantity;
use PhpUnitsOfMeasure\HasSIUnitsTrait;
use PhpUnitsOfMeasure\UnitOfMeasure;
class Area extends AbstractPhysicalQuantity
{
use HasSIUnitsTrait;
protected static $unitDefinitions;
protected static function initialize()
{
// Meters squared
$metersquared = UnitOfMeasure::nativeUnitFactory('m^2');
$metersquared->addAlias('m²');
$metersquared->addAlias('meter squared');
$metersquared->addAlias('square meter');
$metersquared->addAlias('square meters');
$metersquared->addAlias('meters squared');
$metersquared->addAlias('metre squared');
$metersquared->addAlias('metres squared');
static::addUnit($metersquared);
static::addMissingSIPrefixedUnits(
$metersquared,
1,
'%pm^2',
[
'%pm²',
'%Pmeter squared',
'square %Pmeter',
'square %Pmeters',
'%Pmeters squared',
'%Pmetre squared',
'%Pmetres squared'
],
2 // square power factor
);
// Foot squared
$newUnit = UnitOfMeasure::linearUnitFactory('ft^2', 9.290304e-2);
$newUnit->addAlias('ft²');
$newUnit->addAlias('foot squared');
$newUnit->addAlias('square foot');
$newUnit->addAlias('square feet');
$newUnit->addAlias('feet squared');
static::addUnit($newUnit);
// Inch squared
$newUnit = UnitOfMeasure::linearUnitFactory('in^2', 6.4516e-4);
$newUnit->addAlias('in²');
$newUnit->addAlias('inch squared');
$newUnit->addAlias('square inch');
$newUnit->addAlias('square inches');
$newUnit->addAlias('inches squared');
static::addUnit($newUnit);
// Mile squared
$newUnit = UnitOfMeasure::linearUnitFactory('mi^2', 2.589988e6);
$newUnit->addAlias('mi²');
$newUnit->addAlias('mile squared');
$newUnit->addAlias('miles squared');
$newUnit->addAlias('square mile');
$newUnit->addAlias('square miles');
static::addUnit($newUnit);
// Yard squared
$newUnit = UnitOfMeasure::linearUnitFactory('yd^2', 8.361274e-1);
$newUnit->addAlias('yd²');
$newUnit->addAlias('yard squared');
$newUnit->addAlias('yards squared');
$newUnit->addAlias('square yard');
$newUnit->addAlias('square yards');
static::addUnit($newUnit);
// Are
$newUnit = UnitOfMeasure::linearUnitFactory('a', 100);
$newUnit->addAlias('are');
$newUnit->addAlias('ares');
static::addUnit($newUnit);
// Decare
$newUnit = UnitOfMeasure::linearUnitFactory('daa', 1000);
$newUnit->addAlias('decare');
$newUnit->addAlias('decares');
static::addUnit($newUnit);
// Hectare
$newUnit = UnitOfMeasure::linearUnitFactory('ha', 10000);
$newUnit->addAlias('hectare');
$newUnit->addAlias('hectares');
static::addUnit($newUnit);
// International Acre
$newUnit = UnitOfMeasure::linearUnitFactory('ac', 4046.8564224);
$newUnit->addAlias('acre');
$newUnit->addAlias('acres');
static::addUnit($newUnit);
}
}

View File

@ -0,0 +1,36 @@
<?php
namespace PhpUnitsOfMeasure\PhysicalQuantity;
use PhpUnitsOfMeasure\AbstractPhysicalQuantity;
use PhpUnitsOfMeasure\UnitOfMeasure;
use PhpUnitsOfMeasure\HasSIUnitsTrait;
class ElectricCurrent extends AbstractPhysicalQuantity
{
use HasSIUnitsTrait;
protected static $unitDefinitions;
protected static function initialize()
{
// Ampere
$ampere = UnitOfMeasure::nativeUnitFactory('A');
$ampere->addAlias('amp');
$ampere->addAlias('amps');
$ampere->addAlias('ampere');
$ampere->addAlias('amperes');
static::addUnit($ampere);
static::addMissingSIPrefixedUnits(
$ampere,
1,
'%pA',
[
'%Pamp',
'%Pamps',
'%Pampere',
'%Pamperes'
]
);
}
}

View File

@ -0,0 +1,80 @@
<?php
namespace PhpUnitsOfMeasure\PhysicalQuantity;
use PhpUnitsOfMeasure\AbstractPhysicalQuantity;
use PhpUnitsOfMeasure\UnitOfMeasure;
use PhpUnitsOfMeasure\HasSIUnitsTrait;
class Energy extends AbstractPhysicalQuantity
{
use HasSIUnitsTrait;
protected static $unitDefinitions;
protected static function initialize()
{
// Joule
$joule = UnitOfMeasure::nativeUnitFactory('J');
$joule->addAlias('joule');
$joule->addAlias('joules');
static::addUnit($joule);
static::addMissingSIPrefixedUnits(
$joule,
1,
'%pJ',
[
'%Pjoule',
'%Pjoules',
]
);
// Electronvolt
$ev = UnitOfMeasure::linearUnitFactory('eV', 1.6021766208e-19);
$ev->addAlias('electronvolt');
$ev->addAlias('electronvolts');
static::addUnit($ev);
static::addMissingSIPrefixedUnits(
$ev,
1,
'%peV',
[
'%Pelectronvolt',
'%Pelectronvolts',
]
);
// Watt hour
$wattHour = UnitOfMeasure::linearUnitFactory('Wh', 3600);
$wattHour->addAlias('watt hour');
$wattHour->addAlias('watt hours');
static::addUnit($wattHour);
static::addMissingSIPrefixedUnits(
$wattHour,
1,
'%pWh',
[
'%Pwatt hour',
'%Pwatt hours',
]
);
// Calorie
$calorie = UnitOfMeasure::linearUnitFactory('cal', 4.184);
$calorie->addAlias('calorie');
$calorie->addAlias('calories');
static::addUnit($calorie);
static::addMissingSIPrefixedUnits(
$calorie,
1,
'%pcal',
[
'%Pcalorie',
'%Pcalories',
]
);
}
}

View File

@ -0,0 +1,102 @@
<?php
namespace PhpUnitsOfMeasure\PhysicalQuantity;
use PhpUnitsOfMeasure\AbstractPhysicalQuantity;
use PhpUnitsOfMeasure\UnitOfMeasure;
use PhpUnitsOfMeasure\HasSIUnitsTrait;
class Length extends AbstractPhysicalQuantity
{
use HasSIUnitsTrait;
protected static $unitDefinitions;
protected static function initialize()
{
// Meter
$meter = UnitOfMeasure::nativeUnitFactory('m');
$meter->addAlias('meter');
$meter->addAlias('meters');
$meter->addAlias('metre');
$meter->addAlias('metres');
static::addUnit($meter);
static::addMissingSIPrefixedUnits(
$meter,
1,
'%pm',
[
'%Pmeter',
'%Pmeters',
'%Pmetre',
'%Pmetres'
]
);
// Foot
$newUnit = UnitOfMeasure::linearUnitFactory('ft', 0.3048);
$newUnit->addAlias('foot');
$newUnit->addAlias('feet');
static::addUnit($newUnit);
// Inch
$newUnit = UnitOfMeasure::linearUnitFactory('in', 0.0254);
$newUnit->addAlias('inch');
$newUnit->addAlias('inches');
static::addUnit($newUnit);
// Mile
$newUnit = UnitOfMeasure::linearUnitFactory('mi', 1609.344);
$newUnit->addAlias('mile');
$newUnit->addAlias('miles');
static::addUnit($newUnit);
// Yard
$newUnit = UnitOfMeasure::linearUnitFactory('yd', 0.9144);
$newUnit->addAlias('yard');
$newUnit->addAlias('yards');
static::addUnit($newUnit);
// Nautical mile
$newUnit = UnitOfMeasure::linearUnitFactory('M', 1852);
$newUnit->addAlias('nautical mile');
$newUnit->addAlias('nautical miles');
$newUnit->addAlias('nmi');
$newUnit->addAlias('NM');
static::addUnit($newUnit);
// Scandinavian mil
$newUnit = UnitOfMeasure::linearUnitFactory('mil', 10000);
static::addUnit($newUnit);
// Astronomical Unit
$newUnit = UnitOfMeasure::linearUnitFactory('AU', 149597870700);
$newUnit->addAlias('au');
$newUnit->addAlias('astronomical unit');
$newUnit->addAlias('astronomical units');
static::addUnit($newUnit);
// Light Year
$newUnit = UnitOfMeasure::linearUnitFactory('ly', 9460730472580800);
$newUnit->addAlias('LY');
$newUnit->addAlias('light year');
$newUnit->addAlias('light years');
static::addUnit($newUnit);
// Light Year
$parsec = UnitOfMeasure::linearUnitFactory('pc', 3.085677581491367279E+16); // parsec == au * 648000 / pi (30856775814913672.789139379577965)
$parsec->addAlias('parsec');
$parsec->addAlias('parsecs');
static::addUnit($parsec);
static::addMissingSIPrefixedUnits(
$parsec,
1,
'%ppc',
[
'%Pparsec',
'%Pparsecs'
]
);
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace PhpUnitsOfMeasure\PhysicalQuantity;
use PhpUnitsOfMeasure\AbstractPhysicalQuantity;
use PhpUnitsOfMeasure\UnitOfMeasure;
use PhpUnitsOfMeasure\HasSIUnitsTrait;
class LuminousIntensity extends AbstractPhysicalQuantity
{
use HasSIUnitsTrait;
protected static $unitDefinitions;
protected static function initialize()
{
// Candela
$candela = UnitOfMeasure::nativeUnitFactory('cd');
$candela->addAlias('candela');
static::addUnit($candela);
static::addMissingSIPrefixedUnits(
$candela,
1,
'%pcd',
[
'%Pcandela',
]
);
}
}

View File

@ -0,0 +1,102 @@
<?php
namespace PhpUnitsOfMeasure\PhysicalQuantity;
use PhpUnitsOfMeasure\AbstractPhysicalQuantity;
use PhpUnitsOfMeasure\UnitOfMeasure;
use PhpUnitsOfMeasure\HasSIUnitsTrait;
class Mass extends AbstractPhysicalQuantity
{
use HasSIUnitsTrait;
protected static $unitDefinitions;
protected static function initialize()
{
// Gram
$gram = UnitOfMeasure::nativeUnitFactory('g');
$gram->addAlias('gram');
$gram->addAlias('grams');
static::addUnit($gram);
static::addMissingSIPrefixedUnits(
$gram,
1,
'%pg',
[
'%Pgram',
'%Pgrams',
]
);
// Tonne (metric)
$tonne = UnitOfMeasure::linearUnitFactory('t', 1e6);
$tonne->addAlias('T');
$tonne->addAlias('ton');
$tonne->addAlias('tons');
$tonne->addAlias('tonne');
$tonne->addAlias('tonnes');
static::addUnit($tonne);
static::addMissingSIPrefixedUnits(
$tonne,
1,
'%pt',
[
'%pT',
'%Pton',
'%Ptons',
'%Ptonne',
'%Ptonnes',
]
);
// Grain
$newUnit = UnitOfMeasure::linearUnitFactory('gr', 453.59237 / (16 * 437.5));
$newUnit->addAlias('grain');
$newUnit->addAlias('grains');
static::addUnit($newUnit);
// Pound
$newUnit = UnitOfMeasure::linearUnitFactory('lb', 453.59237);
$newUnit->addAlias('lbs');
$newUnit->addAlias('pound');
$newUnit->addAlias('pounds');
static::addUnit($newUnit);
// Ounce
$newUnit = UnitOfMeasure::linearUnitFactory('oz', 453.59237 / 16);
$newUnit->addAlias('ounce');
$newUnit->addAlias('ounces');
static::addUnit($newUnit);
// Stone
$newUnit = UnitOfMeasure::linearUnitFactory('st', 453.59237 * 14);
$newUnit->addAlias('stone');
$newUnit->addAlias('stones');
static::addUnit($newUnit);
// Hundredweight
$newUnit = UnitOfMeasure::linearUnitFactory('cwt', 453.59237 * 112);
$newUnit->addAlias('hundredweight');
$newUnit->addAlias('hundredweights');
static::addUnit($newUnit);
// US short Ton
$newUnit = UnitOfMeasure::linearUnitFactory('ust', 453.59237 * 2000);
$newUnit->addAlias('us short ton');
$newUnit->addAlias('us short tons');
static::addUnit($newUnit);
// US long Ton
$newUnit = UnitOfMeasure::linearUnitFactory('ukt', 453.59237 * 2240);
$newUnit->addAlias('uk long ton');
$newUnit->addAlias('uk long tons');
static::addUnit($newUnit);
// Asian picul
$newUnit = UnitOfMeasure::linearUnitFactory('picul', 60478.982);
$newUnit->addAlias('tam');
static::addUnit($newUnit);
}
}

View File

@ -0,0 +1,46 @@
<?php
namespace PhpUnitsOfMeasure\PhysicalQuantity;
use PhpUnitsOfMeasure\AbstractPhysicalQuantity;
use PhpUnitsOfMeasure\UnitOfMeasure;
use PhpUnitsOfMeasure\HasSIUnitsTrait;
class Power extends AbstractPhysicalQuantity
{
use HasSIUnitsTrait;
protected static $unitDefinitions;
protected static function initialize()
{
// Watt
$watt = UnitOfMeasure::nativeUnitFactory('W');
$watt->addAlias('watt');
$watt->addAlias('watts');
static::addUnit($watt);
static::addMissingSIPrefixedUnits(
$watt,
1,
'%pW',
[
'%Pwatt',
'%Pwatts',
]
);
// decibels-milliwatt
$newUnit = new UnitOfMeasure(
'dBm',
function ($x) {
return 10 * log10($x) + 30;
},
function ($x) {
return pow(10, ($x / 10)) / 1000;
}
);
$newUnit->addAlias('dbm');
$newUnit->addAlias('decibels-milliwatt');
static::addUnit($newUnit);
}
}

View File

@ -0,0 +1,87 @@
<?php
namespace PhpUnitsOfMeasure\PhysicalQuantity;
use PhpUnitsOfMeasure\AbstractPhysicalQuantity;
use PhpUnitsOfMeasure\UnitOfMeasure;
use PhpUnitsOfMeasure\HasSIUnitsTrait;
class Pressure extends AbstractPhysicalQuantity
{
use HasSIUnitsTrait;
protected static $unitDefinitions;
protected static function initialize()
{
// Pascal
$pascal = UnitOfMeasure::nativeUnitFactory('Pa');
$pascal->addAlias('pascal');
$pascal->addAlias('pascals');
static::addUnit($pascal);
static::addMissingSIPrefixedUnits(
$pascal,
1,
'%pPa',
[
'%Ppascal',
'%Ppascals',
]
);
// Standard atmosphere
$newUnit = UnitOfMeasure::linearUnitFactory('atm', 101325);
$newUnit->addAlias('atmosphere');
$newUnit->addAlias('atmospheres');
static::addUnit($newUnit);
// Technical atmosphere
$newUnit = UnitOfMeasure::linearUnitFactory('at', 98066.5);
$newUnit->addAlias('technical atmosphere');
$newUnit->addAlias('technical atmospheres');
static::addUnit($newUnit);
// Torr
$newUnit = UnitOfMeasure::linearUnitFactory('torr', 101325 / 760);
static::addUnit($newUnit);
// Bar
$bar = UnitOfMeasure::linearUnitFactory('bar', 1e5);
static::addUnit($bar);
static::addMissingSIPrefixedUnits(
$bar,
1,
'%pbar'
);
// Inch of Mercury
$newUnit = UnitOfMeasure::linearUnitFactory('inHg', 1 / 0.00029530);
//$newUnit->addAlias('inhg');
$newUnit->addAlias('inches of mercury');
static::addUnit($newUnit);
// Millimeter of Mercury
$newUnit = UnitOfMeasure::linearUnitFactory('mmHg', 133.322387415);
//$newUnit->addAlias('mmhg');
$newUnit->addAlias('millimeters of mercury');
$newUnit->addAlias('millimetres of mercury');
static::addUnit($newUnit);
// Pound per Square Inch
$newUnit = UnitOfMeasure::linearUnitFactory('psi', 6894.757293168);
$newUnit->addAlias('pounds per square inch');
static::addUnit($newUnit);
// Kilo-pound per Square Inch
$newUnit = UnitOfMeasure::linearUnitFactory('ksi', 6894757.293168);
$newUnit->addAlias('kilopounds per square inch');
static::addUnit($newUnit);
// Kilo-pound per Square Inch
$newUnit = UnitOfMeasure::linearUnitFactory('Mpsi', 6894757293.168);
//$newUnit->addAlias('mpsi');
$newUnit->addAlias('megapounds per square inch');
static::addUnit($newUnit);
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace PhpUnitsOfMeasure\PhysicalQuantity;
use PhpUnitsOfMeasure\AbstractPhysicalQuantity;
use PhpUnitsOfMeasure\UnitOfMeasure;
use PhpUnitsOfMeasure\HasSIUnitsTrait;
class Quantity extends AbstractPhysicalQuantity
{
use HasSIUnitsTrait;
protected static $unitDefinitions;
protected static function initialize()
{
// Moles
$mole = UnitOfMeasure::nativeUnitFactory('mol');
$mole->addAlias('mole');
$mole->addAlias('moles');
static::addUnit($mole);
static::addMissingSIPrefixedUnits(
$mole,
1,
'%pmol',
[
'%Pmole',
'%Pmoles',
]
);
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace PhpUnitsOfMeasure\PhysicalQuantity;
use PhpUnitsOfMeasure\AbstractPhysicalQuantity;
use PhpUnitsOfMeasure\UnitOfMeasure;
use PhpUnitsOfMeasure\HasSIUnitsTrait;
class SolidAngle extends AbstractPhysicalQuantity
{
use HasSIUnitsTrait;
protected static $unitDefinitions;
protected static function initialize()
{
// Steradians
$steradian = UnitOfMeasure::nativeUnitFactory('sr');
$steradian->addAlias('steradian');
$steradian->addAlias('steradians');
static::addUnit($steradian);
static::addMissingSIPrefixedUnits(
$steradian,
1,
'%psr',
[
'%Psteradian',
'%Psteradians',
]
);
}
}

View File

@ -0,0 +1,141 @@
<?php
namespace PhpUnitsOfMeasure\PhysicalQuantity;
use PhpUnitsOfMeasure\AbstractPhysicalQuantity;
use PhpUnitsOfMeasure\UnitOfMeasure;
use PhpUnitsOfMeasure\HasSIUnitsTrait;
class Temperature extends AbstractPhysicalQuantity
{
use HasSIUnitsTrait;
protected static $unitDefinitions;
protected static function initialize()
{
// Kelvin
$kelvin = UnitOfMeasure::nativeUnitFactory('K');
$kelvin->addAlias('k');
$kelvin->addAlias('°K');
$kelvin->addAlias('kelvin');
static::addUnit($kelvin);
static::addMissingSIPrefixedUnits(
$kelvin,
1,
'%pK',
[
'%pk',
'%Pkelvin',
]
);
// Degree Celsius
$newUnit = new UnitOfMeasure(
'°C',
function ($x) {
return $x - 273.15;
},
function ($x) {
return $x + 273.15;
}
);
$newUnit->addAlias('c');
$newUnit->addAlias('C');
$newUnit->addAlias('celsius');
static::addUnit($newUnit);
// Degree Fahrenheit
$newUnit = new UnitOfMeasure(
'°F',
function ($x) {
return ($x * 9 / 5) - 459.67;
},
function ($x) {
return ($x + 459.67) * 5/9;
}
);
$newUnit->addAlias('f');
$newUnit->addAlias('F');
$newUnit->addAlias('fahrenheit');
static::addUnit($newUnit);
// Degree Rankine
$newUnit = UnitOfMeasure::linearUnitFactory('°Ra', 5/9);
$newUnit->addAlias('°R');
$newUnit->addAlias('r');
$newUnit->addAlias('R');
$newUnit->addAlias('ra');
$newUnit->addAlias('Ra');
$newUnit->addAlias('rankine');
static::addUnit($newUnit);
// Degree Delisle
$newUnit = new UnitOfMeasure(
'°De',
function ($x) {
return (373.15 - $x) * 3/2;
},
function ($x) {
return 373.15 - $x * 2/3;
}
);
$newUnit->addAlias('°D');
$newUnit->addAlias('D');
$newUnit->addAlias('d');
$newUnit->addAlias('de');
$newUnit->addAlias('De');
$newUnit->addAlias('delisle');
static::addUnit($newUnit);
// Degree Newton
$newUnit = new UnitOfMeasure(
'°N',
function ($x) {
return ($x - 273.15) * 33/100;
},
function ($x) {
return $x * 100/33 + 273.15;
}
);
$newUnit->addAlias('N');
$newUnit->addAlias('newton');
static::addUnit($newUnit);
// Degree Réaumur
$newUnit = new UnitOfMeasure(
'°Ré',
function ($x) {
return ($x - 273.15) * 4/5;
},
function ($x) {
return $x * 5/4 + 273.15;
}
);
$newUnit->addAlias('°Re');
$newUnit->addAlias('Ré');
$newUnit->addAlias('re');
$newUnit->addAlias('Re');
$newUnit->addAlias('réaumur');
$newUnit->addAlias('reaumur');
static::addUnit($newUnit);
// Degree Rømer
$newUnit = new UnitOfMeasure(
'°Rø',
function ($x) {
return ($x - 273.15) * 21/40 + 7.5;
},
function ($x) {
return ($x - 7.5) * 40/21 + 273.15;
}
);
$newUnit->addAlias('°Ro');
$newUnit->addAlias('Rø');
$newUnit->addAlias('ro');
$newUnit->addAlias('Ro');
$newUnit->addAlias('rømer');
$newUnit->addAlias('romer');
static::addUnit($newUnit);
}
}

View File

@ -0,0 +1,95 @@
<?php
namespace PhpUnitsOfMeasure\PhysicalQuantity;
use PhpUnitsOfMeasure\AbstractPhysicalQuantity;
use PhpUnitsOfMeasure\UnitOfMeasure;
use PhpUnitsOfMeasure\HasSIUnitsTrait;
class Time extends AbstractPhysicalQuantity
{
use HasSIUnitsTrait;
protected static $unitDefinitions;
protected static function initialize()
{
// Second
$second = UnitOfMeasure::nativeUnitFactory('s');
$second->addAlias('sec');
$second->addAlias('secs');
$second->addAlias('second');
$second->addAlias('seconds');
static::addUnit($second);
static::addMissingSIPrefixedUnits(
$second,
1,
'%ps',
[
'%Psec',
'%Psecs',
'%Psecond',
'%Pseconds'
]
);
// Minutes
$newUnit = UnitOfMeasure::linearUnitFactory('m', 60);
$newUnit->addAlias('min');
$newUnit->addAlias('mins');
$newUnit->addAlias('minute');
$newUnit->addAlias('minutes');
static::addUnit($newUnit);
// Hours
$newUnit = UnitOfMeasure::linearUnitFactory('h', 3600);
$newUnit->addAlias('hr');
$newUnit->addAlias('hrs');
$newUnit->addAlias('hour');
$newUnit->addAlias('hours');
static::addUnit($newUnit);
// Days
$newUnit = UnitOfMeasure::linearUnitFactory('d', 86400);
$newUnit->addAlias('day');
$newUnit->addAlias('days');
static::addUnit($newUnit);
// Weeks, understood as 7 days
$newUnit = UnitOfMeasure::linearUnitFactory('w', 604800);
$newUnit->addAlias('wk');
$newUnit->addAlias('wks');
$newUnit->addAlias('week');
$newUnit->addAlias('weeks');
static::addUnit($newUnit);
// Gregorian year, understood as 365.2425 days
$newUnit = UnitOfMeasure::linearUnitFactory('yr', 31556952);
$newUnit->addAlias('year');
$newUnit->addAlias('years');
$newUnit->addAlias('gregorian year');
$newUnit->addAlias('gregorian years');
static::addUnit($newUnit);
// Decade
$newUnit = UnitOfMeasure::linearUnitFactory('decade', 315569520);
$newUnit->addAlias('decades');
static::addUnit($newUnit);
// Century
$newUnit = UnitOfMeasure::linearUnitFactory('century', 3155695200);
$newUnit->addAlias('centuries');
static::addUnit($newUnit);
// Millennium
$newUnit = UnitOfMeasure::linearUnitFactory('millennium', 31556952000);
$newUnit->addAlias('millennia');
static::addUnit($newUnit);
// Julian year, understood as 365.25 days
$newUnit = UnitOfMeasure::linearUnitFactory('jyr', 31557600);
$newUnit->addAlias('julian year');
$newUnit->addAlias('julian years');
static::addUnit($newUnit);
}
}

View File

@ -0,0 +1,127 @@
<?php
namespace PhpUnitsOfMeasure\PhysicalQuantity;
use PhpUnitsOfMeasure\AbstractPhysicalQuantity;
use PhpUnitsOfMeasure\HasSIUnitsTrait;
use PhpUnitsOfMeasure\UnitOfMeasure;
class Velocity extends AbstractPhysicalQuantity
{
use HasSIUnitsTrait;
protected static $unitDefinitions;
protected static function initialize()
{
// meter per second
$meterpersecond = UnitOfMeasure::nativeUnitFactory('m/s');
$meterpersecond->addAlias('meters/sec');
$meterpersecond->addAlias('meters per second');
$meterpersecond->addAlias('meter per second');
$meterpersecond->addAlias('metres per second');
$meterpersecond->addAlias('metre per second');
static::addUnit($meterpersecond);
static::addMissingSIPrefixedUnits(
$meterpersecond,
1,
'%pm/s',
[
'%Pmeters/sec',
'%Pmeter per second',
'%Pmeters per second',
'%Pmetre per second',
'%Pmetres per second'
]
);
// meter per minute
$meterperminute = UnitOfMeasure::linearUnitFactory('m/min', 1 / 60);
$meterperminute->addAlias('meters/min');
$meterperminute->addAlias('meters per minute');
$meterperminute->addAlias('meter per minute');
$meterperminute->addAlias('metres per minute');
$meterperminute->addAlias('metre per minute');
static::addUnit($meterperminute);
static::addMissingSIPrefixedUnits(
$meterperminute,
1,
'%pm/min',
[
'%Pmeters/min',
'%Pmeter per minute',
'%Pmeters per minute',
'%Pmetre per minute',
'%Pmetres per minute'
]
);
// meters per hour
$meterperhour = UnitOfMeasure::linearUnitFactory('m/h', 1 / 3600);
$meterperhour->addAlias('m/hr');
$meterperhour->addAlias('m/hour');
$meterperhour->addAlias('meters/hour');
$meterperhour->addAlias('meter per hour');
$meterperhour->addAlias('meters per hour');
$meterperhour->addAlias('metre per hour');
$meterperhour->addAlias('metres per hour');
static::addUnit($meterperhour);
static::addMissingSIPrefixedUnits(
$meterperhour,
1,
'%pm/h',
[
'%pm/hr',
'%pm/hour',
'%Pmeters/hour',
'%Pmeter per hour',
'%Pmeters per hour',
'%Pmetre per hour',
'%Pmetres per hour'
]
);
// feet per second
$newUnit = UnitOfMeasure::linearUnitFactory('ft/s', 0.3048);
$newUnit->addAlias('fps');
$newUnit->addAlias('FPS');
$newUnit->addAlias('feet/sec');
$newUnit->addAlias('feet per second');
static::addUnit($newUnit);
// feet per minute
$newUnit = UnitOfMeasure::linearUnitFactory('ft/min', 0.3048 / 60);
$newUnit->addAlias('fpm');
$newUnit->addAlias('FPM');
$newUnit->addAlias('feet/min');
$newUnit->addAlias('feet per minute');
static::addUnit($newUnit);
// miles per second
$newUnit = UnitOfMeasure::linearUnitFactory('mps', 1609.344);
$newUnit->addAlias('MPS');
$newUnit->addAlias('miles/sec');
$newUnit->addAlias('miles per second');
static::addUnit($newUnit);
// miles per hour
$newUnit = UnitOfMeasure::linearUnitFactory('mph', 1609.344 / 3600);
$newUnit->addAlias('MPH');
$newUnit->addAlias('miles/hour');
$newUnit->addAlias('miles per hour');
static::addUnit($newUnit);
// knot
$newUnit = UnitOfMeasure::linearUnitFactory('kn', 0.514444);
$newUnit->addAlias('knot');
$newUnit->addAlias('knots');
static::addUnit($newUnit);
// Supersonic speed
$newUnit = UnitOfMeasure::linearUnitFactory('mach', 344);
$newUnit->addAlias('speed of sound');
static::addUnit($newUnit);
}
}

View File

@ -0,0 +1,131 @@
<?php
namespace PhpUnitsOfMeasure\PhysicalQuantity;
use PhpUnitsOfMeasure\AbstractPhysicalQuantity;
use PhpUnitsOfMeasure\HasSIUnitsTrait;
use PhpUnitsOfMeasure\UnitOfMeasure;
class Volume extends AbstractPhysicalQuantity
{
use HasSIUnitsTrait;
protected static $unitDefinitions;
protected static function initialize()
{
// Cubic meter
$cubicmeter = UnitOfMeasure::nativeUnitFactory('m^3');
$cubicmeter->addAlias('m³');
$cubicmeter->addAlias('cubic meter');
$cubicmeter->addAlias('cubic meters');
$cubicmeter->addAlias('cubic metre');
$cubicmeter->addAlias('cubic metres');
static::addUnit($cubicmeter);
static::addMissingSIPrefixedUnits(
$cubicmeter,
1,
'%pm^3',
[
'%pm³',
'cubic %Pmeter',
'cubic %Pmeters',
'cubic %Pmetre',
'cubic %Pmetres'
],
3 // cubic power factor
);
// Cubic foot
$newUnit = UnitOfMeasure::linearUnitFactory('ft^3', pow(0.3048, 3));
$newUnit->addAlias('ft³');
$newUnit->addAlias('cubic foot');
$newUnit->addAlias('cubic feet');
static::addUnit($newUnit);
// Cubic inch
$newUnit = UnitOfMeasure::linearUnitFactory('in^3', pow(0.0254, 3));
$newUnit->addAlias('in³');
$newUnit->addAlias('cubic inch');
$newUnit->addAlias('cubic inches');
static::addUnit($newUnit);
// Cubic yard
$newUnit = UnitOfMeasure::linearUnitFactory('yd^3', pow(0.9144, 3));
$newUnit->addAlias('yd³');
$newUnit->addAlias('cubic yard');
$newUnit->addAlias('cubic yards');
static::addUnit($newUnit);
// Liter
$liter = UnitOfMeasure::linearUnitFactory('l', 1e-3);
$liter->addAlias('L');
$liter->addAlias('liter');
$liter->addAlias('liters');
$liter->addAlias('litre');
$liter->addAlias('litres');
static::addUnit($liter);
static::addMissingSIPrefixedUnits(
$liter,
1,
'%pl',
[
'%pL',
'%Pliter',
'%Pliters',
'%Plitre',
'%Plitres'
]
);
// Cup
$newUnit = UnitOfMeasure::linearUnitFactory('cup', 2.365882e-4);
$newUnit->addAlias('cup');
$newUnit->addAlias('cups');
static::addUnit($newUnit);
// teaspoon
$newUnit = UnitOfMeasure::linearUnitFactory('tsp', 0.00000492892);
$newUnit->addAlias('teaspoon');
$newUnit->addAlias('teaspoons');
static::addUnit($newUnit);
// tablespoon
$newUnit = UnitOfMeasure::linearUnitFactory('tbsp', 0.00001478676);
$newUnit->addAlias('tablespoon');
$newUnit->addAlias('tablespoons');
static::addUnit($newUnit);
// Gallon
$newUnit = UnitOfMeasure::linearUnitFactory('gal', 3.7854118e-3);
$newUnit->addAlias('gallon');
$newUnit->addAlias('gallons');
$newUnit->addAlias('us gal');
static::addUnit($newUnit);
// Quart
$newUnit = UnitOfMeasure::linearUnitFactory('qt', 9.4635295e-4);
$newUnit->addAlias('quart');
$newUnit->addAlias('quarts');
$newUnit->addAlias('qts');
$newUnit->addAlias('liq qt');
static::addUnit($newUnit);
// Fluid Ounce
$newUnit = UnitOfMeasure::linearUnitFactory('fl oz', 2.957353e-5);
$newUnit->addAlias('fluid ounce');
$newUnit->addAlias('fluid ounces');
$newUnit->addAlias('fluid oz');
$newUnit->addAlias('fl. oz.');
$newUnit->addAlias('oz. fl.');
static::addUnit($newUnit);
// Pint
$newUnit = UnitOfMeasure::linearUnitFactory('pt', 4.73176475e-4);
$newUnit->addAlias('pint');
$newUnit->addAlias('pints');
$newUnit->addAlias('liq pt');
static::addUnit($newUnit);
}
}

View File

@ -0,0 +1,222 @@
<?php
namespace PhpUnitsOfMeasure\PhysicalQuantity;
use PhpUnitsOfMeasure\AbstractPhysicalQuantity;
use PhpUnitsOfMeasure\HasSIUnitsTrait;
use PhpUnitsOfMeasure\UnitOfMeasure;
class VolumeFlow extends AbstractPhysicalQuantity
{
use HasSIUnitsTrait;
protected static $unitDefinitions;
protected static function initialize()
{
// Cubic meter per second
$cubicmeterpersecond = UnitOfMeasure::nativeUnitFactory('m^3/s');
$cubicmeterpersecond->addAlias('m³/s');
$cubicmeterpersecond->addAlias('cubic meter per second');
$cubicmeterpersecond->addAlias('cubic meters per second');
$cubicmeterpersecond->addAlias('cubic metre per second');
$cubicmeterpersecond->addAlias('cubic metres per second');
static::addUnit($cubicmeterpersecond);
static::addMissingSIPrefixedUnits(
$cubicmeterpersecond,
1,
'%pm^3/s',
[
'%pm³/s',
'cubic %Pmeter per second',
'cubic %Pmeters per second',
'cubic %Pmetre per second',
'cubic %Pmetres per second'
],
3 // cubic power factor
);
// Cubic meter per minute
$newUnit = UnitOfMeasure::linearUnitFactory('m^3/min', 1 / 60);
$newUnit->addAlias('m³/min');
$newUnit->addAlias('cmm');
$newUnit->addAlias('CMM');
$newUnit->addAlias('cubic meter per minute');
$newUnit->addAlias('cubic meters per minute');
static::addUnit($newUnit);
// Cubic meter per hour
$newUnit = UnitOfMeasure::linearUnitFactory('m^3/h', 1 / 3600);
$newUnit->addAlias('m³/h');
$newUnit->addAlias('cmh');
$newUnit->addAlias('CMH');
$newUnit->addAlias('cubic meter per hour');
$newUnit->addAlias('cubic meters per hour');
static::addUnit($newUnit);
// Liter per second
$literpersecond = UnitOfMeasure::linearUnitFactory('l/s', 1e-3);
$literpersecond->addAlias('L/s');
$literpersecond->addAlias('liter per second');
$literpersecond->addAlias('liters per second');
$literpersecond->addAlias('litre per second');
$literpersecond->addAlias('litres per second');
static::addUnit($literpersecond);
static::addMissingSIPrefixedUnits(
$literpersecond,
1,
'%pl/s',
[
'%pL/s',
'%Pliter per second',
'%Pliters per second',
'%Plitre per second',
'%Plitres per second'
]
);
// Liter per minute
$newUnit = UnitOfMeasure::linearUnitFactory('l/min', 1e-3 / 60);
$newUnit->addAlias('L/min');
$newUnit->addAlias('lpm');
$newUnit->addAlias('LPM');
$newUnit->addAlias('liter per minute');
$newUnit->addAlias('liters per minute');
$newUnit->addAlias('litre per minute');
$newUnit->addAlias('litres per minute');
static::addUnit($newUnit);
// Liter per hour
$newUnit = UnitOfMeasure::linearUnitFactory('l/h', 1e-3 / 3600);
$newUnit->addAlias('L/h');
$newUnit->addAlias('lph');
$newUnit->addAlias('LPH');
$newUnit->addAlias('liter per hour');
$newUnit->addAlias('liters per hour');
$newUnit->addAlias('litre per hour');
$newUnit->addAlias('litres per hour');
static::addUnit($newUnit);
// Cubic foot per second
$newUnit = UnitOfMeasure::linearUnitFactory('ft^3/s', pow(0.3048, 3));
$newUnit->addAlias('cfs');
$newUnit->addAlias('CFS');
$newUnit->addAlias('ft³/s');
$newUnit->addAlias('cubic foot per second');
$newUnit->addAlias('cubic feet per second');
static::addUnit($newUnit);
// Cubic foot per minute
$newUnit = UnitOfMeasure::linearUnitFactory('ft^3/min', pow(0.3048, 3) / 60);
$newUnit->addAlias('cfm');
$newUnit->addAlias('CFM');
$newUnit->addAlias('ft³/min');
$newUnit->addAlias('cubic foot per minute');
$newUnit->addAlias('cubic feet per minute');
static::addUnit($newUnit);
// Cubic foot per hour
$newUnit = UnitOfMeasure::linearUnitFactory('ft^3/h', pow(0.3048, 3) / 3600);
$newUnit->addAlias('cfh');
$newUnit->addAlias('CFH');
$newUnit->addAlias('ft³/h');
$newUnit->addAlias('cubic foot per hour');
$newUnit->addAlias('cubic feet per hour');
static::addUnit($newUnit);
// Gallon per second
$newUnit = UnitOfMeasure::linearUnitFactory('gal/s', 3.7854118e-3);
$newUnit->addAlias('gps');
$newUnit->addAlias('GPS');
$newUnit->addAlias('gallon per second');
$newUnit->addAlias('gallons per second');
$newUnit->addAlias('us gal/s');
static::addUnit($newUnit);
// Gallon per minute
$newUnit = UnitOfMeasure::linearUnitFactory('gal/min', 3.7854118e-3 / 60);
$newUnit->addAlias('gpm');
$newUnit->addAlias('GPM');
$newUnit->addAlias('gallon per minute');
$newUnit->addAlias('gallons per minute');
$newUnit->addAlias('us gal/min');
static::addUnit($newUnit);
// Gallon per hour
$newUnit = UnitOfMeasure::linearUnitFactory('gal/h', 3.7854118e-3 / 3600);
$newUnit->addAlias('gph');
$newUnit->addAlias('GPH');
$newUnit->addAlias('gallon per hour');
$newUnit->addAlias('gallons per hour');
$newUnit->addAlias('us gal/h');
static::addUnit($newUnit);
// Gallon per day
$newUnit = UnitOfMeasure::linearUnitFactory('gal/d', 3.7854118e-3 / 86400);
$newUnit->addAlias('gpd');
$newUnit->addAlias('GPD');
$newUnit->addAlias('gallon per day');
$newUnit->addAlias('gallons per day');
$newUnit->addAlias('us gal/d');
static::addUnit($newUnit);
/*
// Cubic inch per second
$newUnit = UnitOfMeasure::linearUnitFactory('in^3', pow(0.0254, 3));
$newUnit->addAlias('in³');
$newUnit->addAlias('cubic inch per second');
$newUnit->addAlias('cubic inches per second');
static::addUnit($newUnit);
// Cubic yard per second
$newUnit = UnitOfMeasure::linearUnitFactory('yd^3', pow(0.9144, 3));
$newUnit->addAlias('yd³');
$newUnit->addAlias('cubic yard per second');
$newUnit->addAlias('cubic yards per second');
static::addUnit($newUnit);
// Cup
$newUnit = UnitOfMeasure::linearUnitFactory('cup', 2.365882e-4);
$newUnit->addAlias('cup');
$newUnit->addAlias('cups');
static::addUnit($newUnit);
// teaspoon
$newUnit = UnitOfMeasure::linearUnitFactory('tsp', 0.00000492892);
$newUnit->addAlias('teaspoon');
$newUnit->addAlias('teaspoons');
static::addUnit($newUnit);
// tablespoon
$newUnit = UnitOfMeasure::linearUnitFactory('tbsp', 0.00001478676);
$newUnit->addAlias('tablespoon');
$newUnit->addAlias('tablespoons');
static::addUnit($newUnit);
// Quart
$newUnit = UnitOfMeasure::linearUnitFactory('qt', 9.4635295e-4);
$newUnit->addAlias('quart');
$newUnit->addAlias('quarts');
$newUnit->addAlias('qts');
$newUnit->addAlias('liq qt');
static::addUnit($newUnit);
// Fluid Ounce
$newUnit = UnitOfMeasure::linearUnitFactory('fl oz', 2.957353e-5);
$newUnit->addAlias('fluid ounce');
$newUnit->addAlias('fluid ounces');
$newUnit->addAlias('fluid oz');
$newUnit->addAlias('fl. oz.');
$newUnit->addAlias('oz. fl.');
static::addUnit($newUnit);
// Pint
$newUnit = UnitOfMeasure::linearUnitFactory('pt', 4.73176475e-4);
$newUnit->addAlias('pint');
$newUnit->addAlias('pints');
$newUnit->addAlias('liq pt');
static::addUnit($newUnit);
*/
}
}

View File

@ -0,0 +1,93 @@
<?php
namespace PhpUnitsOfMeasure;
/**
* classes which implement this interface represent individual physical quantities.
*/
interface PhysicalQuantityInterface
{
/**
* Fetch the measurement, in the given unit of measure
*
* @param UnitOfMeasureInterface|string $unit The desired unit of measure, or a string name of one
*
* @return float The measurement cast in the requested units
*/
public function toUnit($unit);
/**
* Fetch the measurement in the quantity's native unit of measure
*
* @return float the measurement cast to the native unit of measurement
*/
public function toNativeUnit();
/**
* Display the value as a string, in the original unit of measure
*
* @return string The pretty-print version of the value, in the original unit of measure
*/
public function __toString();
/**
* Add a given quantity to this quantity, and return a new quantity object.
*
* Note that the new quantity's original unit will be the same as this object's.
*
* Also note that the two quantities must represent the same physical quantity.
*
* @param PhysicalQuantityInterface $quantity The quantity to add to this one
*
* @throws \PhpUnitsOfMeasure\Exception\PhysicalQuantityMismatch when there is a mismatch between physical quantities
*
* @return PhysicalQuantityInterface the new quantity
*/
public function add(PhysicalQuantityInterface $quantity);
/**
* Subtract a given quantity from this quantity, and return a new quantity object.
*
* Note that the new quantity's original unit will be the same as this object's.
*
* Also note that the two quantities must represent the same physical quantity.
*
* @param PhysicalQuantityInterface $quantity The quantity to subtract from this one
*
* @throws \PhpUnitsOfMeasure\Exception\PhysicalQuantityMismatch when there is a mismatch between physical quantities
*
* @return PhysicalQuantityInterface the new quantity
*/
public function subtract(PhysicalQuantityInterface $quantity);
/**
* Determine whether the given PhysicalQuantityInterface object represents the same
* physical quantity as this object. This is used, for example, to determine if
* two quantities can be added to or subtracted from each other.
*
* Note that this is not considering magnitude, and is only comparing dimensions.
*
* @param PhysicalQuantityInterface $testQuantity
*
* @return boolean True if the quantities are the same, false if not.
*/
public function isEquivalentQuantity(PhysicalQuantityInterface $testQuantity);
/**
* Verify if the given value respond to an already defined unit of meaure of the current
* phisical quantity.
*
* @param string $name the string to verify
*
* @return boolean True if $name has been defined into the current physical quantity, false if not.
*/
public static function isUnitDefined($name);
/**
* Return a list of all the unit of measure defined in the current physical quantity
*
* @return array of all units as strings.
* Keys of the array are the units of measure; for any key the value is
* another array containing all aliases.
*/
public static function listAllUnits();
}

View File

@ -0,0 +1,171 @@
<?php
namespace PhpUnitsOfMeasure;
class UnitOfMeasure implements UnitOfMeasureInterface
{
/**
* For the special case of units that have a linear conversion factor, this factory
* method simplifies the construction of the unit of measure.
*
* For example the relationship between meters and feet is a simple multiplicative factor of
* 0.3048 meters in a foot. Converting back and forth between these two units is a matter of
* multiplication or division by this scaling factor.
*
* In contrast, converting Celsius to Fahrenheit involves an offset calculation, and cannot
* be represented by a simple conversion factor. In such cases this class's constructor should be
* invoked directly.
*
* To help in getting the multiplication and division right, think of the toNativeUnitFactor
* as the number you'd multiply this unit by to get to the native unit of measure. In
* other words:
* 'Value in the native unit of measure' = 'Value in this unit of measure' * toNativeUnitFactor
*
* @param string $name This unit of measure's canonical name
* @param float $toNativeUnitFactor The factor to scale the unit by where factor * base unit = this unit
*
* @return self
*/
public static function linearUnitFactory($name, $toNativeUnitFactor)
{
return new static(
$name,
function ($valueInNativeUnit) use ($toNativeUnitFactor) {
return $valueInNativeUnit / $toNativeUnitFactor;
},
function ($valueInThisUnit) use ($toNativeUnitFactor) {
return $valueInThisUnit * $toNativeUnitFactor;
}
);
}
/**
* This is a special case of the linear unit factory above, for use in generating the native unit of measure
* for a given physical quantity. By definition, the conversion factor is 1.
*
* @param string $name This unit of measure's canonical name
*
* @return self
*/
public static function nativeUnitFactory($name)
{
return static::linearUnitFactory($name, 1);
}
/**
* The canonical name for this unit of measure.
*
* Typically this is the official way the unit is abbreviated.
*
* @var string
*/
protected $name;
/**
* A collection of alias names that map to this unit of measure
*
* @var string[]
*/
protected $aliases = [];
/**
* A callable that can convert a value in this quantity's
* native unit to this unit of measure.
*
* @var callable
*/
protected $fromNativeUnit;
/**
* A callable that can convert a value in this unit of measure
* to a value in the native unit of the physical quantity.
*
* @var callable
*/
protected $toNativeUnit;
/**
* Configure this object's mandatory properties.
*
* @param string $name This unit of measure's canonical name
* @param callable $fromNativeUnit The callable that can cast values into this unit of measure from the native unit of measure
* @param callable $toNativeUnit The callable that can cast values into the native unit from this unit of measure
*
* @throws Exception\NonStringUnitName
*/
public function __construct($name, callable $fromNativeUnit, callable $toNativeUnit)
{
if (!is_string($name)) {
throw new Exception\NonStringUnitName([':name' => $name]);
}
$this->name = $name;
$this->fromNativeUnit = $fromNativeUnit;
$this->toNativeUnit = $toNativeUnit;
}
/**
* @see \PhpUnitsOfMeasure\UnitOfMeasureInterface::getName
*/
public function getName()
{
return $this->name;
}
/**
* @see \PhpUnitsOfMeasure\UnitOfMeasureInterface::addAlias
*/
public function addAlias($alias)
{
if (!is_string($alias)) {
throw new Exception\NonStringUnitName([':name' => $alias]);
}
$this->aliases[] = $alias;
}
/**
* @see \PhpUnitsOfMeasure\UnitOfMeasureInterface::getAliases
*/
public function getAliases()
{
return $this->aliases;
}
/**
* @see \PhpUnitsOfMeasure\UnitOfMeasureInterface::isAliasOf
*/
public function isAliasOf($unit)
{
if (!is_string($unit)) {
throw new Exception\NonStringUnitName([':name' => $unit]);
}
return in_array($unit, $this->aliases);
}
/**
* @see \PhpUnitsOfMeasure\UnitOfMeasureInterface::convertValueFromNativeUnitOfMeasure
*/
public function convertValueFromNativeUnitOfMeasure($value)
{
if (!is_numeric($value)) {
throw new Exception\NonNumericValue([':value' => $value]);
}
$callable = $this->fromNativeUnit;
return $callable($value);
}
/**
* @see \PhpUnitsOfMeasure\UnitOfMeasureInterface::convertValueToNativeUnitOfMeasure
*/
public function convertValueToNativeUnitOfMeasure($value)
{
if (!is_numeric($value)) {
throw new Exception\NonNumericValue([':value' => $value]);
}
$callable = $this->toNativeUnit;
return $callable($value);
}
}

View File

@ -0,0 +1,88 @@
<?php
namespace PhpUnitsOfMeasure;
/**
* Classes that implement this interface represent
* units of measure of physical quantities. In addition
* to handling their names and various aliases, these
* objects are capable of converting values to and
* from an externally-agreed-upon native unit of measure.
*
* The native unit of measure can be any arbitrary
* unit compatible with the physical quantity to which
* this unit of measure belongs, however all the units of measure
* for a particular physical quantity must agree on the same
* native unit.
*
* For instance, all units of measure for Length must agree
* that the meter is the native unit to and from which all values are
* converted.
*/
interface UnitOfMeasureInterface
{
/**
* Get the canonical name of this unit of measure.
*
* @return string The canonical name of this unit of measure.
*/
public function getName();
/**
* Add a new alias for this unit of measure
*
* @param string $alias The new alias
*
* @throws \PhpUnitsOfMeasure\Exception\NonStringUnitName If The alias name is not a string.
*/
public function addAlias($alias);
/**
* Get the list of alternate names for this unit
*
* @return string[] The collection of aliases
*/
public function getAliases();
/**
* Is the given unit an alias of this unit of measure?
*
* @param string $unit A string representation of a potential alias of this unit of measure
*
* @throws \PhpUnitsOfMeasure\Exception\NonStringUnitName If The unit name is not a string.
*
* @return boolean
*/
public function isAliasOf($unit);
/**
* Convert the given value from the native unit of measure to
* this unit of measure.
*
* The native unit of measure can be anything, but all the
* units of measure for a given physical quantity must agree
* on what that unit is.
*
* @param float $value The quantity to convert from this unit of measure
*
* @throws \PhpUnitsOfMeasure\Exception\NonNumericValue If The value is not numeric.
*
* @return float the new value in the native unit
*/
public function convertValueFromNativeUnitOfMeasure($value);
/**
* Convert the given value from this unit of measure into the
* native unit of measure.
*
* The native unit of measure can be anything, but all the
* units of measure for a given physical quantity must agree
* on what that unit is.
*
* @param float $value The quantity to convert from the native unit of measure
*
* @throws \PhpUnitsOfMeasure\Exception\NonNumericValue If The value is not numeric.
*
* @return float the new value in this unit of measure
*/
public function convertValueToNativeUnitOfMeasure($value);
}