From 8c1e21b73ac4016e162a676ab41027062ca58492 Mon Sep 17 00:00:00 2001 From: vovabndrnk Date: Wed, 2 Apr 2025 12:08:46 +0200 Subject: [PATCH 01/12] Update composer.json --- composer.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/composer.json b/composer.json index af85219..92d661f 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,10 @@ { "type": "library", "name": "vesper/unit-conversion", + "description": "This package provides a powerful and flexible way to handle unit conversion in PHP. It supports parsing units from strings, performing conversions, and working with both simple and compound units.", + "keywords": ["unit", "conversion", "php", "measurement"], + "homepage": "https://github.com/hivesper/unit-conversion", + "license": "MIT", "autoload": { "psr-4": { "Vesper\\UnitConversion\\": "src/" From fe73be93c30f37d86d907ecdc7c1dce04870f74d Mon Sep 17 00:00:00 2001 From: vovabndrnk Date: Thu, 3 Apr 2025 09:14:00 +0200 Subject: [PATCH 02/12] Remove php keyword from composer --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 92d661f..854ff21 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "type": "library", "name": "vesper/unit-conversion", "description": "This package provides a powerful and flexible way to handle unit conversion in PHP. It supports parsing units from strings, performing conversions, and working with both simple and compound units.", - "keywords": ["unit", "conversion", "php", "measurement"], + "keywords": ["unit", "conversion", "measurement"], "homepage": "https://github.com/hivesper/unit-conversion", "license": "MIT", "autoload": { From a484070cd8c655edcf0ce121fca76d239944a693 Mon Sep 17 00:00:00 2001 From: Ege Darici Date: Fri, 11 Apr 2025 15:51:51 +0200 Subject: [PATCH 03/12] Check if unit is registered before tokenizing --- src/Parser.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Parser.php b/src/Parser.php index 62cbb8b..28f7107 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -13,6 +13,11 @@ public function __construct(protected Registry $registry) public function parse(string $input): Unit { + $unit = $this->registry->get($input); + if ($unit) { + return $unit; + } + $tokens = $this->tokenize($input); $parts = []; From a0c752d8197ca75fa28d3981e8d25159174bb492 Mon Sep 17 00:00:00 2001 From: Ege Darici Date: Fri, 11 Apr 2025 15:52:03 +0200 Subject: [PATCH 04/12] Add tests for dash joined units --- tests/ParserTest.php | 53 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/tests/ParserTest.php b/tests/ParserTest.php index ad31e1e..c8d5929 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -6,6 +6,8 @@ use Vesper\UnitConversion\Registry; use Vesper\UnitConversion\RegistryBuilder; use Vesper\UnitConversion\Exceptions\InvalidUnitException; +use Vesper\UnitConversion\Unit; +use Vesper\UnitConversion\UnitPart; final class ParserTest extends TestCase { @@ -175,4 +177,55 @@ public function test_throws_on_unknown_token() $this->parser->parse('kilometer / yeet'); } + + public function test_parses_dash_separated_units() + { + $result = $this->parser->parse('gram-liter'); + [$gram, $liter] = $result->getParts(); + + $this->assertCount(2, $result->getParts()); + + $this->assertEquals(Dimension::MASS, $gram->getDimension()); + $this->assertEquals(0.001, $gram->getRatio()); + $this->assertEquals(1, $gram->getPower()); + + $this->assertEquals(Dimension::LENGTH, $liter->getDimension()); + $this->assertEquals(0.1, $liter->getRatio()); + $this->assertEquals(3, $liter->getPower()); + } + + public function test_parses_dash_joined_unit_directly_if_registered() + { + $this->registry->register('watt-hour', new Unit( + // Watt + new UnitPart(1, Dimension::MASS, 1), + new UnitPart(1, Dimension::LENGTH, 2), + new UnitPart(1, Dimension::TIME, -3), + // Hour + new UnitPart(3600, Dimension::TIME, 1) + )); + + $result = $this->parser->parse('watt-hour'); + [$wattMass, $wattLength, $wattTime, $hour] = $result->getParts(); + + $this->assertCount(4, $result->getParts()); + + // Watt + $this->assertEquals(Dimension::MASS, $wattMass->getDimension()); + $this->assertEquals(1, $wattMass->getRatio()); + $this->assertEquals(1, $wattMass->getPower()); + + $this->assertEquals(Dimension::LENGTH, $wattLength->getDimension()); + $this->assertEquals(1, $wattLength->getRatio()); + $this->assertEquals(2, $wattLength->getPower()); + + $this->assertEquals(Dimension::TIME, $wattTime->getDimension()); + $this->assertEquals(1, $wattTime->getRatio()); + $this->assertEquals(-3, $wattTime->getPower()); + + // Hour + $this->assertEquals(Dimension::TIME, $hour->getDimension()); + $this->assertEquals(3600, $hour->getRatio()); + $this->assertEquals(1, $hour->getPower()); + } } From 7e4684f9b69f21693140defc8e6c84ac8b388c99 Mon Sep 17 00:00:00 2001 From: Ege Darici Date: Mon, 14 Apr 2025 13:00:49 +0200 Subject: [PATCH 05/12] Handle cases of dash seperated and joined units, if registered handle as single unit, otherwise divide tokens --- src/Parser.php | 79 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 52 insertions(+), 27 deletions(-) diff --git a/src/Parser.php b/src/Parser.php index 28f7107..6fcac5c 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -24,54 +24,79 @@ public function parse(string $input): Unit foreach ($tokens as $i => $token) { $prevToken = $tokens[$i - 1] ?? null; - if ($token['type'] === 'unit') { - $unit = $this->registry->get($token['value']); + if ($token['type'] !== 'unit') { + continue; + } - if ($unit === null) { - throw new InvalidUnitException("Unknown unit: {$token['value']}"); - } + $unit = $this->registry->get($token['value']); - $powerSign = $prevToken && $prevToken['type'] === 'operator' && $prevToken['value'] === '/' - ? -1 - : 1; + if ($unit === null) { + throw new InvalidUnitException("Unknown unit: {$token['value']}"); + } - $parts[] = array_map(function (UnitPart|FactorUnitPart $part) use ($token, $powerSign) { - if ($part instanceof FactorUnitPart) { - return new FactorUnitPart($part->getRatio()); - } + $powerSign = $prevToken && $prevToken['type'] === 'operator' && $prevToken['value'] === '/' + ? -1 + : 1; + $power = $token['power'] * $powerSign; - return new UnitPart( - $part->getRatio(), - $part->getDimension(), - $part->getPower() * $token['power'] * $powerSign, - ); - }, $unit->getParts()); - } + $parts[] = array_map(function (UnitPart|FactorUnitPart $part) use ($power) { + if ($part instanceof FactorUnitPart) { + return new FactorUnitPart($part->getRatio()); + } + + return new UnitPart( + $part->getRatio(), + $part->getDimension(), + $part->getPower() * $power, + ); + }, $unit->getParts()); } return new Unit(...array_merge([], ...$parts)); } - protected function tokenize(string $input): array { $matches = []; $tokens = []; + // Handle cases like "watt-hour" or "watt-hour/kg" with different regex logic: + // - Dash-separated units (e.g., "watt-hour") should be treated as a single unit. + // - For inputs with division operators (e.g., "watt-hour/kg"), units separated by "/" + // should be processed individually while still supporting dash-separated units. + $allowDash = str_contains($input, '/') || str_contains($input, '*'); + + $regex = $allowDash + ? '/(?P[\w-]+)(?:\^(?P-?\d))?|(?P[*\/])/' + : '/(?P\w+)(?:\^(?P-?\d))?|(?P[*\/])/'; + preg_match_all( - '/(?P\w+)(?:\^(?P-?\d))?|(?P[*\/])/', + $regex, preg_replace('/\s+/', '', $input), $matches, PREG_SET_ORDER ); - foreach ($matches as $match) { if ($match['unit']) { - $tokens[] = [ - 'type' => 'unit', - 'value' => $match['unit'], - 'power' => (int)($match['power'] ?? 1), - ]; + // If the unit contains a dash, check if it's a registered unit as a whole + if (str_contains($match['unit'], '-') && $this->registry->get($match['unit']) !== null) { + // If the compound unit (e.g., "watt-hour") is in the registry, treat it as a single unit + $tokens[] = [ + 'type' => 'unit', + 'value' => $match['unit'], + 'power' => (int)($match['power'] ?? 1), + ]; + } else { + // Otherwise, split the dash-separated units into individual tokens (e.g., "gram-liter" becomes "gram" and "liter") + $units = explode('-', $match['unit']); + foreach ($units as $unit) { + $tokens[] = [ + 'type' => 'unit', + 'value' => $unit, + 'power' => (int)($match['power'] ?? 1), + ]; + } + } } else if ($match['operator']) { $tokens[] = [ 'type' => 'operator', From 4c22830284f541018faa4954df813a09159d7ff6 Mon Sep 17 00:00:00 2001 From: Ege Darici Date: Mon, 14 Apr 2025 13:01:34 +0200 Subject: [PATCH 06/12] Add test cases of dash seperated and joined units --- tests/ParserTest.php | 68 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 2 deletions(-) diff --git a/tests/ParserTest.php b/tests/ParserTest.php index c8d5929..9607dc9 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -194,10 +194,30 @@ public function test_parses_dash_separated_units() $this->assertEquals(3, $liter->getPower()); } - public function test_parses_dash_joined_unit_directly_if_registered() + public function test_parses_dash_separated_unit_with_operator() + { + $result = $this->parser->parse('gram-liter/hour'); + [$gram, $liter, $hour] = $result->getParts(); + + $this->assertCount(3, $result->getParts()); + + $this->assertEquals(Dimension::MASS, $gram->getDimension()); + $this->assertEquals(0.001, $gram->getRatio()); + $this->assertEquals(1, $gram->getPower()); + + $this->assertEquals(Dimension::LENGTH, $liter->getDimension()); + $this->assertEquals(0.1, $liter->getRatio()); + $this->assertEquals(3, $liter->getPower()); + + $this->assertEquals(Dimension::TIME, $hour->getDimension()); + $this->assertEquals(3600, $hour->getRatio()); + $this->assertEquals(-1, $hour->getPower()); + } + + public function test_parses_dash_joined_single_unit_directly_if_registered() { $this->registry->register('watt-hour', new Unit( - // Watt + // Watt new UnitPart(1, Dimension::MASS, 1), new UnitPart(1, Dimension::LENGTH, 2), new UnitPart(1, Dimension::TIME, -3), @@ -228,4 +248,48 @@ public function test_parses_dash_joined_unit_directly_if_registered() $this->assertEquals(3600, $hour->getRatio()); $this->assertEquals(1, $hour->getPower()); } + + public function test_parses_dash_joined_single_unit_with_operator() + { + $this->registry->register('watt-hour', new Unit( + // Watt + new UnitPart(1, Dimension::MASS, 1), + new UnitPart(1, Dimension::LENGTH, 2), + new UnitPart(1, Dimension::TIME, -3), + // Hour + new UnitPart(3600, Dimension::TIME, 1) + )); + + $result = $this->parser->parse('watt-hour/kg'); + [$wattMass, $wattLength, $wattTime, $hour, $kilo, $gram] = $result->getParts(); + + $this->assertCount(6, $result->getParts()); + + // Watt + $this->assertEquals(Dimension::MASS, $wattMass->getDimension()); + $this->assertEquals(1, $wattMass->getRatio()); + $this->assertEquals(1, $wattMass->getPower()); + + $this->assertEquals(Dimension::LENGTH, $wattLength->getDimension()); + $this->assertEquals(1, $wattLength->getRatio()); + $this->assertEquals(2, $wattLength->getPower()); + + $this->assertEquals(Dimension::TIME, $wattTime->getDimension()); + $this->assertEquals(1, $wattTime->getRatio()); + $this->assertEquals(-3, $wattTime->getPower()); + + // Hour + $this->assertEquals(Dimension::TIME, $hour->getDimension()); + $this->assertEquals(3600, $hour->getRatio()); + $this->assertEquals(1, $hour->getPower()); + + // Kilogram + $this->assertNull($kilo->getDimension()); + $this->assertEquals(1000, $kilo->getRatio()); + $this->assertEquals(1, $kilo->getPower()); + + $this->assertEquals(Dimension::MASS, $gram->getDimension()); + $this->assertEquals(0.001, $gram->getRatio()); + $this->assertEquals(-1, $gram->getPower()); + } } From fcc34b2b65b0cd35ee48990ca7d9c95725302c75 Mon Sep 17 00:00:00 2001 From: Ege Darici Date: Tue, 15 Apr 2025 10:23:33 +0200 Subject: [PATCH 07/12] Check if parts match with registered watt-hour unit --- tests/ParserTest.php | 63 ++++++++------------------------------------ 1 file changed, 11 insertions(+), 52 deletions(-) diff --git a/tests/ParserTest.php b/tests/ParserTest.php index 9607dc9..0d3699c 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -216,72 +216,31 @@ public function test_parses_dash_separated_unit_with_operator() public function test_parses_dash_joined_single_unit_directly_if_registered() { - $this->registry->register('watt-hour', new Unit( - // Watt - new UnitPart(1, Dimension::MASS, 1), - new UnitPart(1, Dimension::LENGTH, 2), - new UnitPart(1, Dimension::TIME, -3), - // Hour - new UnitPart(3600, Dimension::TIME, 1) - )); - + $wattHour = $this->registry->get('watt-hour'); $result = $this->parser->parse('watt-hour'); [$wattMass, $wattLength, $wattTime, $hour] = $result->getParts(); $this->assertCount(4, $result->getParts()); - // Watt - $this->assertEquals(Dimension::MASS, $wattMass->getDimension()); - $this->assertEquals(1, $wattMass->getRatio()); - $this->assertEquals(1, $wattMass->getPower()); - - $this->assertEquals(Dimension::LENGTH, $wattLength->getDimension()); - $this->assertEquals(1, $wattLength->getRatio()); - $this->assertEquals(2, $wattLength->getPower()); - - $this->assertEquals(Dimension::TIME, $wattTime->getDimension()); - $this->assertEquals(1, $wattTime->getRatio()); - $this->assertEquals(-3, $wattTime->getPower()); - - // Hour - $this->assertEquals(Dimension::TIME, $hour->getDimension()); - $this->assertEquals(3600, $hour->getRatio()); - $this->assertEquals(1, $hour->getPower()); + $this->assertEquals($wattMass, $wattHour->getPart(0)); + $this->assertEquals($wattLength, $wattHour->getPart(1)); + $this->assertEquals($wattTime, $wattHour->getPart(2)); + $this->assertEquals($hour, $wattHour->getPart(3)); } public function test_parses_dash_joined_single_unit_with_operator() { - $this->registry->register('watt-hour', new Unit( - // Watt - new UnitPart(1, Dimension::MASS, 1), - new UnitPart(1, Dimension::LENGTH, 2), - new UnitPart(1, Dimension::TIME, -3), - // Hour - new UnitPart(3600, Dimension::TIME, 1) - )); - + $wattHour = $this->registry->get('watt-hour'); $result = $this->parser->parse('watt-hour/kg'); [$wattMass, $wattLength, $wattTime, $hour, $kilo, $gram] = $result->getParts(); $this->assertCount(6, $result->getParts()); - // Watt - $this->assertEquals(Dimension::MASS, $wattMass->getDimension()); - $this->assertEquals(1, $wattMass->getRatio()); - $this->assertEquals(1, $wattMass->getPower()); - - $this->assertEquals(Dimension::LENGTH, $wattLength->getDimension()); - $this->assertEquals(1, $wattLength->getRatio()); - $this->assertEquals(2, $wattLength->getPower()); - - $this->assertEquals(Dimension::TIME, $wattTime->getDimension()); - $this->assertEquals(1, $wattTime->getRatio()); - $this->assertEquals(-3, $wattTime->getPower()); - - // Hour - $this->assertEquals(Dimension::TIME, $hour->getDimension()); - $this->assertEquals(3600, $hour->getRatio()); - $this->assertEquals(1, $hour->getPower()); + // Watt-hour + $this->assertEquals($wattMass, $wattHour->getPart(0)); + $this->assertEquals($wattLength, $wattHour->getPart(1)); + $this->assertEquals($wattTime, $wattHour->getPart(2)); + $this->assertEquals($hour, $wattHour->getPart(3)); // Kilogram $this->assertNull($kilo->getDimension()); From 15b709e2ecb31c9fd070b9215232291628e508e0 Mon Sep 17 00:00:00 2001 From: Ege Darici Date: Tue, 15 Apr 2025 10:25:02 +0200 Subject: [PATCH 08/12] Check division mode --- src/Parser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Parser.php b/src/Parser.php index 6fcac5c..1985575 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -63,7 +63,7 @@ protected function tokenize(string $input): array // - Dash-separated units (e.g., "watt-hour") should be treated as a single unit. // - For inputs with division operators (e.g., "watt-hour/kg"), units separated by "/" // should be processed individually while still supporting dash-separated units. - $allowDash = str_contains($input, '/') || str_contains($input, '*'); + $allowDash = str_contains($input, '/'); $regex = $allowDash ? '/(?P[\w-]+)(?:\^(?P-?\d))?|(?P[*\/])/' From e739696271b4b6689883e44f31e0439e20b59178 Mon Sep 17 00:00:00 2001 From: Ege Darici Date: Wed, 16 Apr 2025 16:28:53 +0200 Subject: [PATCH 09/12] Clean code --- src/Parser.php | 42 ++++++++---------------------------------- tests/ParserTest.php | 1 - 2 files changed, 8 insertions(+), 35 deletions(-) diff --git a/src/Parser.php b/src/Parser.php index 1985575..e8c8b35 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -13,9 +13,8 @@ public function __construct(protected Registry $registry) public function parse(string $input): Unit { - $unit = $this->registry->get($input); - if ($unit) { - return $unit; + if ($this->registry->has($input)) { + return $this->registry->get($input);; } $tokens = $this->tokenize($input); @@ -33,7 +32,6 @@ public function parse(string $input): Unit if ($unit === null) { throw new InvalidUnitException("Unknown unit: {$token['value']}"); } - $powerSign = $prevToken && $prevToken['type'] === 'operator' && $prevToken['value'] === '/' ? -1 : 1; @@ -59,18 +57,8 @@ protected function tokenize(string $input): array $matches = []; $tokens = []; - // Handle cases like "watt-hour" or "watt-hour/kg" with different regex logic: - // - Dash-separated units (e.g., "watt-hour") should be treated as a single unit. - // - For inputs with division operators (e.g., "watt-hour/kg"), units separated by "/" - // should be processed individually while still supporting dash-separated units. - $allowDash = str_contains($input, '/'); - - $regex = $allowDash - ? '/(?P[\w-]+)(?:\^(?P-?\d))?|(?P[*\/])/' - : '/(?P\w+)(?:\^(?P-?\d))?|(?P[*\/])/'; - preg_match_all( - $regex, + '/(?P\w+)(?:\^(?P-?\d))?|(?P[*\/])/', preg_replace('/\s+/', '', $input), $matches, PREG_SET_ORDER @@ -78,25 +66,11 @@ protected function tokenize(string $input): array foreach ($matches as $match) { if ($match['unit']) { - // If the unit contains a dash, check if it's a registered unit as a whole - if (str_contains($match['unit'], '-') && $this->registry->get($match['unit']) !== null) { - // If the compound unit (e.g., "watt-hour") is in the registry, treat it as a single unit - $tokens[] = [ - 'type' => 'unit', - 'value' => $match['unit'], - 'power' => (int)($match['power'] ?? 1), - ]; - } else { - // Otherwise, split the dash-separated units into individual tokens (e.g., "gram-liter" becomes "gram" and "liter") - $units = explode('-', $match['unit']); - foreach ($units as $unit) { - $tokens[] = [ - 'type' => 'unit', - 'value' => $unit, - 'power' => (int)($match['power'] ?? 1), - ]; - } - } + $tokens[] = [ + 'type' => 'unit', + 'value' => $match['unit'], + 'power' => (int)($match['power'] ?? 1), + ]; } else if ($match['operator']) { $tokens[] = [ 'type' => 'operator', diff --git a/tests/ParserTest.php b/tests/ParserTest.php index 0d3699c..61e10f7 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -221,7 +221,6 @@ public function test_parses_dash_joined_single_unit_directly_if_registered() [$wattMass, $wattLength, $wattTime, $hour] = $result->getParts(); $this->assertCount(4, $result->getParts()); - $this->assertEquals($wattMass, $wattHour->getPart(0)); $this->assertEquals($wattLength, $wattHour->getPart(1)); $this->assertEquals($wattTime, $wattHour->getPart(2)); From d9a31cb6ee1d54804a2d935899ef5715ae2b3804 Mon Sep 17 00:00:00 2001 From: Ege Darici Date: Thu, 17 Apr 2025 09:42:05 +0200 Subject: [PATCH 10/12] Adjust code structure --- src/Parser.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Parser.php b/src/Parser.php index e8c8b35..9e9f998 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -52,6 +52,7 @@ public function parse(string $input): Unit return new Unit(...array_merge([], ...$parts)); } + protected function tokenize(string $input): array { $matches = []; From f26336790db92ecd2c1aa328e4328e64980a0b4d Mon Sep 17 00:00:00 2001 From: Tom Date: Fri, 11 Apr 2025 12:34:47 +0200 Subject: [PATCH 11/12] Test units that we ship --- tests/EnergyTest.php | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/tests/EnergyTest.php b/tests/EnergyTest.php index f2f379b..cb04d08 100644 --- a/tests/EnergyTest.php +++ b/tests/EnergyTest.php @@ -2,7 +2,8 @@ use PHPUnit\Framework\TestCase; use Vesper\UnitConversion\Converter; -use Vesper\UnitConversion\Dimension; +use Vesper\UnitConversion\Registry; +use Vesper\UnitConversion\RegistryBuilder; use Vesper\UnitConversion\Unit; use Vesper\UnitConversion\UnitPart; @@ -16,23 +17,20 @@ protected function setUp(): void { parent::setUp(); - $this->cal = new Unit( - new UnitPart(4.184, Dimension::MASS, 1), - new UnitPart(1, Dimension::LENGTH, 2), - new UnitPart(1, Dimension::TIME, -2) - ); - $this->joule = new Unit( - new UnitPart(1, Dimension::MASS, 1), - new UnitPart(1, Dimension::LENGTH, 2), - new UnitPart(1, Dimension::TIME, -2), - ); + $registry = new Registry(); + + RegistryBuilder::build($registry); + + $this->cal = $registry->get('calorie'); + + $this->joule = $registry->get('joule'); $this->converter = new Converter(); } public function test_cal_to_joule(): void { - $this->assertEqualsWithDelta(4.184, $this->converter->convert($this->cal, $this->joule, 1), 0.0000001); + $this->assertEqualsWithDelta(4.1868, $this->converter->convert($this->cal, $this->joule, 1), 0.0000001); } public function test_joule_to_cal(): void From 7b0b58dce689a12a61c6d499b591cd8c79bf01d5 Mon Sep 17 00:00:00 2001 From: Tom Date: Fri, 11 Apr 2025 12:48:58 +0200 Subject: [PATCH 12/12] Add watt-hour --- src/RegistryBuilder.php | 14 ++++++++++++++ tests/EnergyTest.php | 11 +++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/RegistryBuilder.php b/src/RegistryBuilder.php index 86a20e3..3902117 100644 --- a/src/RegistryBuilder.php +++ b/src/RegistryBuilder.php @@ -130,6 +130,20 @@ protected static function initEnergy(Registry $registry): void new UnitPart(1, Dimension::TIME, -2) ) ); + + static::registerSiUnit( + $registry, + 'watt-hour', + ['Wh'], + new Unit( + // watt + new UnitPart(1, Dimension::MASS, 1), + new UnitPart(1, Dimension::LENGTH, 2), + new UnitPart(1, Dimension::TIME, -3), + // hour + new UnitPart(3600, Dimension::TIME, 1) + ) + ); } protected static function initLength(Registry $registry): void diff --git a/tests/EnergyTest.php b/tests/EnergyTest.php index cb04d08..8f20aaa 100644 --- a/tests/EnergyTest.php +++ b/tests/EnergyTest.php @@ -5,12 +5,12 @@ use Vesper\UnitConversion\Registry; use Vesper\UnitConversion\RegistryBuilder; use Vesper\UnitConversion\Unit; -use Vesper\UnitConversion\UnitPart; final class EnergyTest extends TestCase { protected Unit $cal; protected Unit $joule; + protected Unit $watthour; protected Converter $converter; protected function setUp(): void @@ -25,6 +25,8 @@ protected function setUp(): void $this->joule = $registry->get('joule'); + $this->watthour = $registry->get('watt-hour'); + $this->converter = new Converter(); } @@ -35,6 +37,11 @@ public function test_cal_to_joule(): void public function test_joule_to_cal(): void { - $this->assertEqualsWithDelta(1 / 4.184, $this->converter->convert($this->joule, $this->cal, 1), 0.0000001); + $this->assertEqualsWithDelta(1, $this->converter->convert($this->joule, $this->cal, 4.1868), 0.0000001); + } + + public function test_joule_to_watt_hour(): void + { + $this->assertEqualsWithDelta(1, $this->converter->convert($this->joule, $this->watthour, 3600), 0.0000001); } }