Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
triplepoint committed Feb 10, 2014
2 parents 829f51d + b4cafee commit 63ddb69
Show file tree
Hide file tree
Showing 16 changed files with 276 additions and 37 deletions.
52 changes: 17 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
master: [![Build Status](https://travis-ci.org/triplepoint/php-units-of-measure.png?branch=master)](https://travis-ci.org/triplepoint/php-units-of-measure)

## Introduction
This is a PHP library for representing and converting physical units of measure. The utility of this library
is in encapsulating physical quantities in such a way that you don't have to keep track of which
unit they're represented in. For instance:
This is a PHP library for representing and converting physical units of measure. The utility of this library is in encapsulating physical quantities in such a way that you don't have to keep track of which unit they're represented in. For instance:

``` php
use PhpUnitsOfMeasure\PhysicalQuantity\Length;
Expand All @@ -15,11 +13,10 @@ echo $height->toUnit('m');
// would print 1.87757, which is 6.16 feet in meters.
```

Having this abstraction allows you to create interfaces that accept physical quantities
without requiring them to be in a particular unit. For example, this function assumes the
height is a float of a particular unit (presumably feet):
Having this abstraction allows you to create interfaces that accept physical quantities without requiring them to be in a particular unit. For example, this function assumes the height is a float of a particular unit (presumably feet):

``` php
// Tied to a specific unit of measure
function isTooTallToRideThisTrain( $height )
{
return $height > 5;
Expand All @@ -34,6 +31,7 @@ Whereas this version allows for height to be provided in whatever unit is conven
``` php
use PhpUnitsOfMeasure\PhysicalQuantity\Length;

// Free to operate on lengths in any unit of measure
function isTooTallToRideThisTrain( Length $height )
{
return $height->toUnit('ft') > 5;
Expand All @@ -44,13 +42,11 @@ isTooTallToRideThisTrain( new Length(2, 'm') );
```

## Installation
This library is best included in your projects via composer. See the [Composer website](http://getcomposer.org/)
for more details, and see the [Packagist.org site for this library](https://packagist.org/packages/triplepoint/php-units-of-measure).
This library is best included in your projects via composer. See the [Composer website](http://getcomposer.org/) for more details, and see the [Packagist.org site for this library](https://packagist.org/packages/triplepoint/php-units-of-measure).

## Use
### Conversion
As in the examples above, the basic usage of this library is in representing physical quantities and converting
between typical units of measure. For example:
As in the examples above, the basic usage of this library is in representing physical quantities and converting between typical units of measure. For example:

``` php
$quantity = new \PhpUnitsOfMeasure\PhysicalQuantity\Mass(6, 'lbs');
Expand All @@ -64,8 +60,7 @@ echo $quantity; // '6 lbs'
```

### Arithmetic Operators
There's also support for addition and subtraction. The `PhysicalQuantity` objects are immutable, and as such
these arithmetic methods return new quantity objects representing the results:
There's also support for addition and subtraction. The `PhysicalQuantity` objects are immutable, and as such these arithmetic methods return new quantity objects representing the results:

``` php
$first = new \PhpUnitsOfMeasure\PhysicalQuantity\Volume(6, 'liters');
Expand All @@ -81,10 +76,7 @@ echo $difference; // 4.5804708 l
### Adding new Units of Measure to Existing Quantities
Ocassionally, you will need to add a new unit of measure to a pre-existing quantity.

For example, let's say in a project you need a new measure of length, called "cubits". You have two
options: you can permanently add the new unit of measure to the `\PhpUnitsOfMeasure\PhysicalQuantity\Length`
class (and submit a pull request to get it added upstream, if appropriate), or you can add the
unit temporarily at run time, inside your calling code.
For example, let's say in a project you need a new measure of length, called "cubits". You have two options: you can permanently add the new unit of measure to a new child class of the `\PhpUnitsOfMeasure\PhysicalQuantity\Length` class (or add it directly to that class and submit a pull request to get it added upstream, if appropriate), or you can add the unit temporarily at run time, inside your calling code.

#### Adding a New Unit of Measure at Runtime
To add a new unit of measure to an existing quantity at run time, you'd do this:
Expand Down Expand Up @@ -126,11 +118,9 @@ echo $length->toUnit('feet'); // '21'
```

#### Permanently Adding a New Unit of Measure to a Physical Quantity
The above method only applies to the specific Length object and is therefore temporary; it would
be necessary to repeat this process every time you created a new measurement and wanted to use cubits.
The above method only applies to the specific Length object and is therefore temporary; it would be necessary to repeat this process every time you created a new measurement and wanted to use cubits.

A new unit of measure can be permanently added to a physical quantity class by essentially the same process,
only it would be done inside the constructor of the quantity class. For example:
A new unit of measure can be permanently added to a physical quantity class by essentially the same process, only it would be done inside the constructor of the quantity class. For example:

``` php
namespace PhpUnitsOfMeasure\PhysicalQuantity;
Expand Down Expand Up @@ -183,19 +173,12 @@ Now any new object of class `Length` that gets instantiated will come with the c
### Adding New Physical Quantities
[Physical quantities](http://en.wikipedia.org/wiki/Physical_quantity) are categories of measurable values, like mass, length, force, etc.

For physical quantities that are not already present in this library, it will be necessary to write a class
to support a new one. All physical quantities extend the `\PhpUnitsOfMeasure\PhysicalQuantity` class, and typically
have only a constructor method which creates the quantity's units of measure. See above for examples on how to add
new units to a quantity class.
For physical quantities that are not already present in this library, it will be necessary to write a class to support a new one. All physical quantities extend the `\PhpUnitsOfMeasure\PhysicalQuantity` class, and typically have only a constructor method which creates the quantity's units of measure. See above for examples on how to add new units to a quantity class.

Note that every physical quantity has a chosen "native unit" which is typically the SI standard. The main point for this
unit is that all of the quantity's other units of measure will convert to and from this chosen native unit. It's
important to be aware of a quantity's native unit when writing conversions for new units of measure.
Note that every physical quantity has a chosen "native unit" which is typically the SI standard. The main point for this unit is that all of the quantity's other units of measure will convert to and from this chosen native unit. It's important to be aware of a quantity's native unit when writing conversions for new units of measure.

### Adding new Aliases to Existing Units
It may come up that the right unit of measure exists for the right physical quantity, but there's a missing
alias for the unit. For example, if you thought 'footses' was an obviously lacking alias for the Length unit 'ft', you could
temporarily add the alias like this:
It may come up that the right unit of measure exists for the right physical quantity, but there's a missing alias for the unit. For example, if you thought 'footses' was an obviously lacking alias for the Length unit 'ft', you could temporarily add the alias like this:

``` php
use \PhpUnitsOfMeasure\PhysicalQuantity\Length;
Expand All @@ -220,12 +203,9 @@ API documentation (such as it is) is handled through GitApiDoc.
- http://gitapidoc.com/api/triplepoint/php-units-of-measure

## Testing and Contributing
Pull requests are welcome, especially regarding new units of measure or new physical quantities. However, please note that there
are many sources for conversion factors, and not all are careful to respect known precision.
Pull requests are welcome, especially regarding new units of measure or new physical quantities. However, please note that there are many sources for conversion factors, and not all are careful to respect known precision.

In the United States, the standards body for measurement is NIST, and they've published [NIST Special Publication 1038 "The International
System of Units (SI) - Conversion factors for General Use"](http://www.nist.gov/pml/wmd/metric/upload/SP1038.pdf). This guide
contains the approved conversion factors between various units and the base SI units.
In the United States, the standards body for measurement is NIST, and they've published [NIST Special Publication 1038 "The International System of Units (SI) - Conversion factors for General Use"](http://www.nist.gov/pml/wmd/metric/upload/SP1038.pdf). This guide contains the approved conversion factors between various units and the base SI units.

Also note that any new physical quantities should have the appropriate SI unit chosen for their native unit of measure.

Expand Down Expand Up @@ -258,3 +238,5 @@ Codesniffer verifies that coding standards are being met. Once the project is b
``` bash
vendor/bin/phpcs --encoding=utf-8 --extensions=php --standard=./tests/phpcs.xml -nsp ./
```

[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/triplepoint/php-units-of-measure/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
2 changes: 1 addition & 1 deletion source/PhpUnitsOfMeasure/PhysicalQuantity/Acceleration.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function ($x) {
return $x;
},
array(
'm/s²'
'm/s²',
'meter per second squared',
'meters per second squared'
)
Expand Down
35 changes: 35 additions & 0 deletions source/PhpUnitsOfMeasure/PhysicalQuantity/Area.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,40 @@ function ($x) {
$new_unit->addAlias('yard squared');
$new_unit->addAlias('yards squared');
$this->registerUnitOfMeasure($new_unit);


$new_unit = new UnitOfMeasure(
'ha',
function ($x) {
return $x / 10000;
},
function ($x) {
return $x * 10000;
},
array(
'hectare',
'hectares'
)
);

$this->registerUnitOfMeasure($new_unit);


// International Acre
$new_unit = new UnitOfMeasure(
'ac',
function ($x) {
return $x / 4046.8564224;
},
function ($x) {
return $x * 4046.8564224;
},
array(
'acre',
'acres'
)
);

$this->registerUnitOfMeasure($new_unit);
}
}
2 changes: 1 addition & 1 deletion source/PhpUnitsOfMeasure/UnitOfMeasure.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class UnitOfMeasure implements UnitOfMeasureInterface
* @param string $name This unit of measure's canonical name
* @param callable $from_native_unit The callable that can cast values into this unit of measure from the native unit of measure
* @param callable $to_native_unit The callable that can cast values into the native unit from this unit of measure
* @param array $aliases
* @param array $aliases
*
* @return void
*/
Expand Down
18 changes: 18 additions & 0 deletions tests/PhpUnitsOfMeasureTest/PhysicalQuantity/AccelerationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace PhpUnitsOfMeasureTest\PhysicalQuantity;

use PhpUnitsOfMeasure\PhysicalQuantity\Acceleration;

class AccelerationTest extends \PHPUnit_Framework_TestCase
{
/**
* Verify that the object instantiates without error.
*
* @covers \PhpUnitsOfMeasure\PhysicalQuantity\Acceleration::__construct()
*/
public function testConstructorSucceeds()
{
$target = new Acceleration(1, 'm/s^2');
}
}
10 changes: 10 additions & 0 deletions tests/PhpUnitsOfMeasureTest/PhysicalQuantity/AngleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@

class AngleTest extends \PHPUnit_Framework_TestCase
{
/**
* Verify that the object instantiates without error.
*
* @covers \PhpUnitsOfMeasure\PhysicalQuantity\Angle::__construct()
*/
public function testConstructorSucceeds()
{
$target = new Angle(1, 'deg');
}

public function testToDegrees()
{
$angle = new Angle(2 * M_PI, 'rads');
Expand Down
41 changes: 41 additions & 0 deletions tests/PhpUnitsOfMeasureTest/PhysicalQuantity/AreaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace PhpUnitsOfMeasureTest\PhysicalQuantity;

use PhpUnitsOfMeasure\PhysicalQuantity\Area;
use PHPUnit_Framework_TestCase;

class AreaTest extends PHPUnit_Framework_TestCase
{
/**
* Verify that the object instantiates without error.
*
* @covers \PhpUnitsOfMeasure\PhysicalQuantity\Area::__construct()
*/
public function testConstructorSucceeds()
{
$target = new Area(1, 'm^2');
}

public function testToHectares()
{
$area = new Area(3, 'ha');

$this->assertEquals(30000, $area->toUnit("m^2"));
}

/**
* There aren't lots of super nice conversions between ac -> m^2,
* so we'll check that it's close.
*
* @return [type] [description]
*/
public function testToAcres()
{
$area = new Area(13, 'ac');

$area = round($area->toUnit("m^2"), 6);

$this->assertEquals(52609.133491, $area);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace PhpUnitsOfMeasureTest\PhysicalQuantity;

use PhpUnitsOfMeasure\PhysicalQuantity\ElectricCurrent;

class ElectricCurrentTest extends \PHPUnit_Framework_TestCase
{
/**
* Verify that the object instantiates without error.
*
* @covers \PhpUnitsOfMeasure\PhysicalQuantity\ElectricCurrent::__construct()
*/
public function testConstructorSucceeds()
{
$target = new ElectricCurrent(1, 'A');
}
}
18 changes: 18 additions & 0 deletions tests/PhpUnitsOfMeasureTest/PhysicalQuantity/LengthTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace PhpUnitsOfMeasureTest\PhysicalQuantity;

use PhpUnitsOfMeasure\PhysicalQuantity\Length;

class LengthTest extends \PHPUnit_Framework_TestCase
{
/**
* Verify that the object instantiates without error.
*
* @covers \PhpUnitsOfMeasure\PhysicalQuantity\Length::__construct()
*/
public function testConstructorSucceeds()
{
$target = new Length(1, 'm');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace PhpUnitsOfMeasureTest\PhysicalQuantity;

use PhpUnitsOfMeasure\PhysicalQuantity\LuminousIntensity;

class LuminousIntensityTest extends \PHPUnit_Framework_TestCase
{
/**
* Verify that the object instantiates without error.
*
* @covers \PhpUnitsOfMeasure\PhysicalQuantity\LuminousIntensity::__construct()
*/
public function testConstructorSucceeds()
{
$target = new LuminousIntensity(1, 'cd');
}
}
18 changes: 18 additions & 0 deletions tests/PhpUnitsOfMeasureTest/PhysicalQuantity/MassTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace PhpUnitsOfMeasureTest\PhysicalQuantity;

use PhpUnitsOfMeasure\PhysicalQuantity\Mass;

class MassTest extends \PHPUnit_Framework_TestCase
{
/**
* Verify that the object instantiates without error.
*
* @covers \PhpUnitsOfMeasure\PhysicalQuantity\Mass::__construct()
*/
public function testConstructorSucceeds()
{
$target = new Mass(1, 'kg');
}
}
18 changes: 18 additions & 0 deletions tests/PhpUnitsOfMeasureTest/PhysicalQuantity/PressureTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace PhpUnitsOfMeasureTest\PhysicalQuantity;

use PhpUnitsOfMeasure\PhysicalQuantity\Pressure;

class PressureTest extends \PHPUnit_Framework_TestCase
{
/**
* Verify that the object instantiates without error.
*
* @covers \PhpUnitsOfMeasure\PhysicalQuantity\Pressure::__construct()
*/
public function testConstructorSucceeds()
{
$target = new Pressure(1, 'Pa');
}
}
18 changes: 18 additions & 0 deletions tests/PhpUnitsOfMeasureTest/PhysicalQuantity/TemperatureTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace PhpUnitsOfMeasureTest\PhysicalQuantity;

use PhpUnitsOfMeasure\PhysicalQuantity\Temperature;

class TemperatureTest extends \PHPUnit_Framework_TestCase
{
/**
* Verify that the object instantiates without error.
*
* @covers \PhpUnitsOfMeasure\PhysicalQuantity\Temperature::__construct()
*/
public function testConstructorSucceeds()
{
$target = new Temperature(1, 'K');
}
}
9 changes: 9 additions & 0 deletions tests/PhpUnitsOfMeasureTest/PhysicalQuantity/TimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@

class TimeTest extends \PHPUnit_Framework_TestCase
{
/**
* Verify that the object instantiates without error.
*
* @covers \PhpUnitsOfMeasure\PhysicalQuantity\Time::__construct()
*/
public function testConstructorSucceeds()
{
$target = new Time(1, 'sec');
}

public function testToSeconds()
{
Expand Down
Loading

0 comments on commit 63ddb69

Please sign in to comment.