1
0
Fork 0
mirror of https://github.com/archtechx/money.git synced 2025-12-12 03:14:03 +00:00

update: add dedicated exception class

This commit is contained in:
Gaurav 2022-03-15 17:03:57 +05:30
parent 96786939bd
commit d6cb1ba6ae
2 changed files with 19 additions and 2 deletions

View file

@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace ArchTech\Money\Exceptions;
use Exception;
class CannotExtractCurrencyException extends Exception
{
public function __construct(string $message)
{
parent::__construct($message);
}
}

View file

@ -4,6 +4,8 @@ declare(strict_types=1);
namespace ArchTech\Money; namespace ArchTech\Money;
use ArchTech\Money\Exceptions\CannotExtractCurrencyException;
class PriceFormatter class PriceFormatter
{ {
/** Format a decimal per the currency's specifications. */ /** Format a decimal per the currency's specifications. */
@ -53,13 +55,13 @@ class PriceFormatter
&& str_ends_with($formatted, $currency->suffix()) && str_ends_with($formatted, $currency->suffix())
) { ) {
if ($possibleCurrency) { if ($possibleCurrency) {
throw new \Exception('Multiple currencies are using the same prefix and suffix. Please specify the currency of the formatted string.'); throw new CannotExtractCurrencyException('Multiple currencies are using the same prefix and suffix. Please specify the currency of the formatted string.');
} }
$possibleCurrency = $currency; $possibleCurrency = $currency;
} }
} }
return $possibleCurrency ?? throw new \Exception('None of the currencies are using the prefix and suffix that would match with the formatted string.'); return $possibleCurrency ?? throw new CannotExtractCurrencyException('None of the currencies are using the prefix and suffix that would match with the formatted string.');
} }
} }