From 6b175ba94d056ef29dfbcff9d3d9f621e92f6014 Mon Sep 17 00:00:00 2001 From: Jonathan Hanson Date: Sun, 15 Jan 2017 23:14:24 -0800 Subject: [PATCH 1/4] In order to address difficulties with pull request #64, this fix mocks out UnitOfMeasureInterface::isAliasOf() for the AbstractPhysicalQuantityTest unit test mock. --- tests/AbstractPhysicalQuantityTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/AbstractPhysicalQuantityTest.php b/tests/AbstractPhysicalQuantityTest.php index 19ce3f2..2936602 100644 --- a/tests/AbstractPhysicalQuantityTest.php +++ b/tests/AbstractPhysicalQuantityTest.php @@ -30,6 +30,12 @@ protected function getTestUnitOfMeasure($name, $aliases = []) ->willReturn($name); $newUnit->method('getAliases') ->willReturn($aliases); + $newUnit->method('isAliasOf') + ->will($this->returnCallback( + function ($value) use ($aliases) { + return in_array($value, $aliases); + } + )); return $newUnit; } From 8dc6141126f6a32e5bc7d99f15166c8228f04af7 Mon Sep 17 00:00:00 2001 From: pierpaolocira Date: Fri, 12 May 2017 10:55:23 +0200 Subject: [PATCH 2/4] Issue 67: Implementation of Power --- source/PhysicalQuantity/Power.php | 32 +++++++++++++++++ tests/PhysicalQuantity/PowerTest.php | 53 ++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 source/PhysicalQuantity/Power.php create mode 100644 tests/PhysicalQuantity/PowerTest.php diff --git a/source/PhysicalQuantity/Power.php b/source/PhysicalQuantity/Power.php new file mode 100644 index 0000000..0ec2a70 --- /dev/null +++ b/source/PhysicalQuantity/Power.php @@ -0,0 +1,32 @@ +addAlias('watt'); + $watt->addAlias('watts'); + static::addUnit($watt); + + static::addMissingSIPrefixedUnits( + $watt, + 1, + '%pW', + [ + '%Pwatt', + '%Pwatts', + ] + ); + } +} diff --git a/tests/PhysicalQuantity/PowerTest.php b/tests/PhysicalQuantity/PowerTest.php new file mode 100644 index 0000000..ca21c2b --- /dev/null +++ b/tests/PhysicalQuantity/PowerTest.php @@ -0,0 +1,53 @@ +assertEquals(1, $quantity->toUnit('kW')); + } + + public function testToWatt() + { + $quantity = new Energy(1, 'kW'); + $this->assertEquals(1000, $quantity->toUnit('W')); + } + +} From 19cca508daf72511168c8b185e2bdf1627b5dd75 Mon Sep 17 00:00:00 2001 From: pierpaolocira Date: Fri, 12 May 2017 11:20:02 +0200 Subject: [PATCH 3/4] Fixing test file --- tests/PhysicalQuantity/PowerTest.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/PhysicalQuantity/PowerTest.php b/tests/PhysicalQuantity/PowerTest.php index ca21c2b..b66e777 100644 --- a/tests/PhysicalQuantity/PowerTest.php +++ b/tests/PhysicalQuantity/PowerTest.php @@ -40,14 +40,13 @@ protected function instantiateTestQuantity() public function testToKilowatt() { - $quantity = new Energy(1000, 'W'); + $quantity = new Power(1000, 'W'); $this->assertEquals(1, $quantity->toUnit('kW')); } public function testToWatt() { - $quantity = new Energy(1, 'kW'); + $quantity = new Power(1, 'kW'); $this->assertEquals(1000, $quantity->toUnit('W')); } - } From 786a7239d7e8c7e76e5e30a219cdfb0757e9dda0 Mon Sep 17 00:00:00 2001 From: Stephen Clouse Date: Mon, 24 Jul 2017 16:27:15 -0500 Subject: [PATCH 4/4] Add caching for unit lookups with getUnit() --- source/AbstractPhysicalQuantity.php | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/source/AbstractPhysicalQuantity.php b/source/AbstractPhysicalQuantity.php index 0d61bbf..029e332 100644 --- a/source/AbstractPhysicalQuantity.php +++ b/source/AbstractPhysicalQuantity.php @@ -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. * @@ -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; } }