Skip to content

Commit

Permalink
Merge branch 'master' into php-units-of-measure-issue-62
Browse files Browse the repository at this point in the history
  • Loading branch information
triplepoint authored Jul 26, 2017
2 parents 45b8b96 + 212a332 commit a0fe8e0
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 1 deletion.
24 changes: 23 additions & 1 deletion source/AbstractPhysicalQuantity.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ abstract class AbstractPhysicalQuantity implements PhysicalQuantityInterface
*/
// 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.
*
Expand Down Expand Up @@ -47,9 +64,14 @@ public static function getUnit($unit)
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 $unitOfMeasure;
return self::$unitCache[$key] = $unitOfMeasure;
}
}

Expand Down
32 changes: 32 additions & 0 deletions source/PhysicalQuantity/Power.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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',
]
);
}
}
52 changes: 52 additions & 0 deletions tests/PhysicalQuantity/PowerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace PhpUnitsOfMeasureTest\PhysicalQuantity;

use PhpUnitsOfMeasure\PhysicalQuantity\Power;

class PowerTest extends AbstractPhysicalQuantityTestCase
{
protected $supportedUnitsWithAliases = [
'W',
'watt',
'watts',
'µW',
'microwatt',
'microwatts',
'mW',
'milliwatt',
'milliwatts',
'kW',
'kilowatt',
'kilowatts',
'MW',
'megawatt',
'megawatts',
'GW',
'gigawatt',
'gigawatts',
'TW',
'terawatt',
'terawatts',
'PW',
'petawatt',
'petawatts',
];

protected function instantiateTestQuantity()
{
return new Power(1, 'W');
}

public function testToKilowatt()
{
$quantity = new Power(1000, 'W');
$this->assertEquals(1, $quantity->toUnit('kW'));
}

public function testToWatt()
{
$quantity = new Power(1, 'kW');
$this->assertEquals(1000, $quantity->toUnit('W'));
}
}

0 comments on commit a0fe8e0

Please sign in to comment.