Skip to content

Commit 63ddb69

Browse files
committed
Merge branch 'develop'
2 parents 829f51d + b4cafee commit 63ddb69

File tree

16 files changed

+276
-37
lines changed

16 files changed

+276
-37
lines changed

README.md

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
master: [![Build Status](https://travis-ci.org/triplepoint/php-units-of-measure.png?branch=master)](https://travis-ci.org/triplepoint/php-units-of-measure)
33

44
## Introduction
5-
This is a PHP library for representing and converting physical units of measure. The utility of this library
6-
is in encapsulating physical quantities in such a way that you don't have to keep track of which
7-
unit they're represented in. For instance:
5+
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:
86

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

18-
Having this abstraction allows you to create interfaces that accept physical quantities
19-
without requiring them to be in a particular unit. For example, this function assumes the
20-
height is a float of a particular unit (presumably feet):
16+
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):
2117

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

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

4644
## Installation
47-
This library is best included in your projects via composer. See the [Composer website](http://getcomposer.org/)
48-
for more details, and see the [Packagist.org site for this library](https://packagist.org/packages/triplepoint/php-units-of-measure).
45+
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).
4946

5047
## Use
5148
### Conversion
52-
As in the examples above, the basic usage of this library is in representing physical quantities and converting
53-
between typical units of measure. For example:
49+
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:
5450

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

6662
### Arithmetic Operators
67-
There's also support for addition and subtraction. The `PhysicalQuantity` objects are immutable, and as such
68-
these arithmetic methods return new quantity objects representing the results:
63+
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:
6964

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

84-
For example, let's say in a project you need a new measure of length, called "cubits". You have two
85-
options: you can permanently add the new unit of measure to the `\PhpUnitsOfMeasure\PhysicalQuantity\Length`
86-
class (and submit a pull request to get it added upstream, if appropriate), or you can add the
87-
unit temporarily at run time, inside your calling code.
79+
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.
8880

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

128120
#### Permanently Adding a New Unit of Measure to a Physical Quantity
129-
The above method only applies to the specific Length object and is therefore temporary; it would
130-
be necessary to repeat this process every time you created a new measurement and wanted to use cubits.
121+
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.
131122

132-
A new unit of measure can be permanently added to a physical quantity class by essentially the same process,
133-
only it would be done inside the constructor of the quantity class. For example:
123+
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:
134124

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

186-
For physical quantities that are not already present in this library, it will be necessary to write a class
187-
to support a new one. All physical quantities extend the `\PhpUnitsOfMeasure\PhysicalQuantity` class, and typically
188-
have only a constructor method which creates the quantity's units of measure. See above for examples on how to add
189-
new units to a quantity class.
176+
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.
190177

191-
Note that every physical quantity has a chosen "native unit" which is typically the SI standard. The main point for this
192-
unit is that all of the quantity's other units of measure will convert to and from this chosen native unit. It's
193-
important to be aware of a quantity's native unit when writing conversions for new units of measure.
178+
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.
194179

195180
### Adding new Aliases to Existing Units
196-
It may come up that the right unit of measure exists for the right physical quantity, but there's a missing
197-
alias for the unit. For example, if you thought 'footses' was an obviously lacking alias for the Length unit 'ft', you could
198-
temporarily add the alias like this:
181+
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:
199182

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

222205
## Testing and Contributing
223-
Pull requests are welcome, especially regarding new units of measure or new physical quantities. However, please note that there
224-
are many sources for conversion factors, and not all are careful to respect known precision.
206+
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.
225207

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

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

@@ -258,3 +238,5 @@ Codesniffer verifies that coding standards are being met. Once the project is b
258238
``` bash
259239
vendor/bin/phpcs --encoding=utf-8 --extensions=php --standard=./tests/phpcs.xml -nsp ./
260240
```
241+
242+
[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/triplepoint/php-units-of-measure/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

source/PhpUnitsOfMeasure/PhysicalQuantity/Acceleration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function ($x) {
2626
return $x;
2727
},
2828
array(
29-
'm/s²'
29+
'm/s²',
3030
'meter per second squared',
3131
'meters per second squared'
3232
)

source/PhpUnitsOfMeasure/PhysicalQuantity/Area.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,5 +174,40 @@ function ($x) {
174174
$new_unit->addAlias('yard squared');
175175
$new_unit->addAlias('yards squared');
176176
$this->registerUnitOfMeasure($new_unit);
177+
178+
179+
$new_unit = new UnitOfMeasure(
180+
'ha',
181+
function ($x) {
182+
return $x / 10000;
183+
},
184+
function ($x) {
185+
return $x * 10000;
186+
},
187+
array(
188+
'hectare',
189+
'hectares'
190+
)
191+
);
192+
193+
$this->registerUnitOfMeasure($new_unit);
194+
195+
196+
// International Acre
197+
$new_unit = new UnitOfMeasure(
198+
'ac',
199+
function ($x) {
200+
return $x / 4046.8564224;
201+
},
202+
function ($x) {
203+
return $x * 4046.8564224;
204+
},
205+
array(
206+
'acre',
207+
'acres'
208+
)
209+
);
210+
211+
$this->registerUnitOfMeasure($new_unit);
177212
}
178213
}

source/PhpUnitsOfMeasure/UnitOfMeasure.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class UnitOfMeasure implements UnitOfMeasureInterface
4646
* @param string $name This unit of measure's canonical name
4747
* @param callable $from_native_unit The callable that can cast values into this unit of measure from the native unit of measure
4848
* @param callable $to_native_unit The callable that can cast values into the native unit from this unit of measure
49-
* @param array $aliases
49+
* @param array $aliases
5050
*
5151
* @return void
5252
*/
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace PhpUnitsOfMeasureTest\PhysicalQuantity;
4+
5+
use PhpUnitsOfMeasure\PhysicalQuantity\Acceleration;
6+
7+
class AccelerationTest extends \PHPUnit_Framework_TestCase
8+
{
9+
/**
10+
* Verify that the object instantiates without error.
11+
*
12+
* @covers \PhpUnitsOfMeasure\PhysicalQuantity\Acceleration::__construct()
13+
*/
14+
public function testConstructorSucceeds()
15+
{
16+
$target = new Acceleration(1, 'm/s^2');
17+
}
18+
}

tests/PhpUnitsOfMeasureTest/PhysicalQuantity/AngleTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@
66

77
class AngleTest extends \PHPUnit_Framework_TestCase
88
{
9+
/**
10+
* Verify that the object instantiates without error.
11+
*
12+
* @covers \PhpUnitsOfMeasure\PhysicalQuantity\Angle::__construct()
13+
*/
14+
public function testConstructorSucceeds()
15+
{
16+
$target = new Angle(1, 'deg');
17+
}
18+
919
public function testToDegrees()
1020
{
1121
$angle = new Angle(2 * M_PI, 'rads');
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace PhpUnitsOfMeasureTest\PhysicalQuantity;
4+
5+
use PhpUnitsOfMeasure\PhysicalQuantity\Area;
6+
use PHPUnit_Framework_TestCase;
7+
8+
class AreaTest extends PHPUnit_Framework_TestCase
9+
{
10+
/**
11+
* Verify that the object instantiates without error.
12+
*
13+
* @covers \PhpUnitsOfMeasure\PhysicalQuantity\Area::__construct()
14+
*/
15+
public function testConstructorSucceeds()
16+
{
17+
$target = new Area(1, 'm^2');
18+
}
19+
20+
public function testToHectares()
21+
{
22+
$area = new Area(3, 'ha');
23+
24+
$this->assertEquals(30000, $area->toUnit("m^2"));
25+
}
26+
27+
/**
28+
* There aren't lots of super nice conversions between ac -> m^2,
29+
* so we'll check that it's close.
30+
*
31+
* @return [type] [description]
32+
*/
33+
public function testToAcres()
34+
{
35+
$area = new Area(13, 'ac');
36+
37+
$area = round($area->toUnit("m^2"), 6);
38+
39+
$this->assertEquals(52609.133491, $area);
40+
}
41+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace PhpUnitsOfMeasureTest\PhysicalQuantity;
4+
5+
use PhpUnitsOfMeasure\PhysicalQuantity\ElectricCurrent;
6+
7+
class ElectricCurrentTest extends \PHPUnit_Framework_TestCase
8+
{
9+
/**
10+
* Verify that the object instantiates without error.
11+
*
12+
* @covers \PhpUnitsOfMeasure\PhysicalQuantity\ElectricCurrent::__construct()
13+
*/
14+
public function testConstructorSucceeds()
15+
{
16+
$target = new ElectricCurrent(1, 'A');
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace PhpUnitsOfMeasureTest\PhysicalQuantity;
4+
5+
use PhpUnitsOfMeasure\PhysicalQuantity\Length;
6+
7+
class LengthTest extends \PHPUnit_Framework_TestCase
8+
{
9+
/**
10+
* Verify that the object instantiates without error.
11+
*
12+
* @covers \PhpUnitsOfMeasure\PhysicalQuantity\Length::__construct()
13+
*/
14+
public function testConstructorSucceeds()
15+
{
16+
$target = new Length(1, 'm');
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace PhpUnitsOfMeasureTest\PhysicalQuantity;
4+
5+
use PhpUnitsOfMeasure\PhysicalQuantity\LuminousIntensity;
6+
7+
class LuminousIntensityTest extends \PHPUnit_Framework_TestCase
8+
{
9+
/**
10+
* Verify that the object instantiates without error.
11+
*
12+
* @covers \PhpUnitsOfMeasure\PhysicalQuantity\LuminousIntensity::__construct()
13+
*/
14+
public function testConstructorSucceeds()
15+
{
16+
$target = new LuminousIntensity(1, 'cd');
17+
}
18+
}

0 commit comments

Comments
 (0)