Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace Vesper\UnitConversion;

use Vesper\UnitConversion\Exceptions\CannotConvertUnitException;

class Converter
{
public function convert(Unit $from, Unit $to, float $value = 1): float
{
if (!$from->canConvertTo($to)) {
throw new \Exception("Cannot convert from [$from] to [$to]");
throw new CannotConvertUnitException("Cannot convert from [$from] to [$to]");
}

$fromOffset = $from->getPart(0)->getRatio() * $from->getPart(0)->getOffset();
Expand Down
9 changes: 9 additions & 0 deletions src/Exceptions/CannotConvertUnitException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Vesper\UnitConversion\Exceptions;

use Exception;

class CannotConvertUnitException extends Exception
{
}
9 changes: 9 additions & 0 deletions src/Exceptions/InvalidUnitException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Vesper\UnitConversion\Exceptions;

use Exception;

class InvalidUnitException extends Exception
{
}
4 changes: 3 additions & 1 deletion src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Vesper\UnitConversion;

use Vesper\UnitConversion\Exceptions\InvalidUnitException;

class Parser
{
public function __construct(protected Registry $registry)
Expand All @@ -21,7 +23,7 @@ public function parse(string $input): Unit
$unit = $this->registry->get($token['value']);

if ($unit === null) {
throw new \Exception("Unknown unit: {$token['value']}");
throw new InvalidUnitException("Unknown unit: {$token['value']}");
}

$powerSign = $prevToken && $prevToken['type'] === 'operator' && $prevToken['value'] === '/'
Expand Down
6 changes: 4 additions & 2 deletions src/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Vesper\UnitConversion;

use Vesper\UnitConversion\Exceptions\InvalidUnitException;

class Registry
{
protected array $registry = [];
Expand All @@ -23,12 +25,12 @@ public function alias(string $name, array|string $aliases): self
$base = $this->get($name);

if ($base === null) {
throw new \Exception("Cannot alias unknown unit [$name]");
throw new InvalidUnitException("Cannot alias unknown unit [$name]");
}

foreach ((array)$aliases as $alias) {
if (isset($this->registry[$alias])) {
throw new \Exception("Name [$alias] is already registered");
throw new InvalidUnitException("Name [$alias] is already registered");
}

$this->registry[$alias] = $base;
Expand Down
3 changes: 2 additions & 1 deletion tests/ConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Vesper\UnitConversion\Dimension;
use Vesper\UnitConversion\Unit;
use Vesper\UnitConversion\UnitPart;
use Vesper\UnitConversion\Exceptions\CannotConvertUnitException;

final class ConverterTest extends TestCase
{
Expand All @@ -27,7 +28,7 @@ public function test_converts_incompatible_throws()
new UnitPart(1, Dimension::LENGTH, 1),
);

$this->expectException(\Exception::class);
$this->expectException(CannotConvertUnitException::class);
$this->expectExceptionMessage("Cannot convert from [$m] to [$m2]");

$this->converter->convert($m, $m2);
Expand Down
3 changes: 2 additions & 1 deletion tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Vesper\UnitConversion\Parser;
use Vesper\UnitConversion\Registry;
use Vesper\UnitConversion\RegistryBuilder;
use Vesper\UnitConversion\Exceptions\InvalidUnitException;

final class ParserTest extends TestCase
{
Expand Down Expand Up @@ -169,7 +170,7 @@ public function test_ignores_whitespace()

public function test_throws_on_unknown_token()
{
$this->expectException(\Exception::class);
$this->expectException(InvalidUnitException::class);
$this->expectExceptionMessage('Unknown unit: yeet');

$this->parser->parse('kilometer / yeet');
Expand Down
5 changes: 3 additions & 2 deletions tests/RegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Vesper\UnitConversion\Registry;
use Vesper\UnitConversion\Unit;
use Vesper\UnitConversion\UnitPart;
use Vesper\UnitConversion\Exceptions\InvalidUnitException;

final class RegistryTest extends TestCase
{
Expand Down Expand Up @@ -42,15 +43,15 @@ public function test_alias_unknown_throws()
{
$this->assertNull($this->registry->get('yeet'));

$this->expectException(Exception::class);
$this->expectException(InvalidUnitException::class);
$this->expectExceptionMessage('Cannot alias unknown unit [yeet]');

$this->registry->alias('yeet', 'y');
}

public function test_alias_overwrite_throws()
{
$this->expectException(Exception::class);
$this->expectException(InvalidUnitException::class);
$this->expectExceptionMessage('Name [gram] is already registered');

$this->registry->alias('gram', 'gram');
Expand Down