This bundle is a Symfony2 integration of the library triplepoint/php-units-of-measure.
The following words are used with the given definition, to avoid confusion:
- Quantity: Describes the type of a unit. Example
Length
orMass
. - Unit: Describes the name of a unit. Example
meter
orkilogram
. - Alias: Another name for a unit. Example
meter
can have the aliasesmeters
,metre
andmetres
. - Native Unit: Every quantity needs a base measurement that will be converted to. Example Length has
meter
, sofeet
will first be converted tometer
before converted toinch
. - Factor: A numeric value used to convert a measurement of a unit to another. Example:
kilogram
has the factor 1, whileton
has 1000.
The configuration allows to enable the integrated units from the library and extend them with own units. Also own linear units can be simply configured.
php_units_of_measure:
# Define which integrated PhysicalQuantities should be available.
integrated:
# Enable the quantity Time without any modifications.
Time: true
# Enable the quantity Length with some additional units.
Length:
units:
# A new (fictional) unit, where 1um is defined as 100000000 meter.
UltraMeter:
aliases: [ 'um' ]
to_native_factor: 100000000
# Define own quantities.
defined:
# This quantity is just for demonstration purposes
Scrum:
units:
# Native unit is a single person.
Persons:
aliases: [ 'p' ]
type: native
# A team consists of 7 persons.
Teams:
aliases: [ 't' ]
to_native_factor: 7
# A ScrumOfScrum is considered to be 3 teams.
ScrumOfScrums:
aliases: [ 'sos' ]
to_native_factor: 21
There is a service for fetching all available quantities:
$quantities = $this->getContainer()->get('php_units_of_measure.quantities');
// Long form
$lengthDefinition = $quantities->getDefinition('length');
$length = $lengthDefinition->createUnit(10, 'meter');
// Short form
$length = $quantities->createUnit('length', 10, meter);
For each enabled quantity, a own service with the definition is available:
$lengthDefinition = $this->getContainer()->get('php_units_of_measure.quantities.length');
$length = $lengthDefinition->createUnit(10, 'meter');
This service can be easily injected and used in other dependencies.
- Create github organization for PhpUnitOfMeasure with @triplepoint
- Integrate this with Twig, that would be cool
- Enable definition of new units via configuration.
- Enabled existing Units via configuration.
- Enable creating new Units for existing Quantities.
- Enabled adding aliases to existing units. Does not seem to be possible to add aliases afterwards.
- Provide a central point to access all configured Units.
- Integrate own Quantity classes (via DI Tag?).