diff --git a/README.md b/README.md index 172b054..d2712ad 100644 --- a/README.md +++ b/README.md @@ -180,11 +180,11 @@ $fromFormatted = Money::fromFormatted($formatted); $fromFormatted->is($money); // true ``` -Optional overrides for the [currency specification](#currency-logic) are accepted too. +If you had passed overrides while [formatting the money instance](#formatting-money), the same can passed to this method. ```php $money = money(1000); -$formatted = $money->formatted(); // $10.00 -$fromFormatted = Money::fromFormatted($formatted, USD::class, ['decimalSeparator' => ',', 'prefix' => '$ ', 'suffix' => ' USD']); +$formatted = $money->formatted(['prefix' => '$ ', 'suffix' => ' USD']); // $ 10.00 USD +$fromFormatted = Money::fromFormatted($formatted, USD::class, ['prefix' => '$ ', 'suffix' => ' USD']); $fromFormatted->is($money); // true ``` diff --git a/src/Money.php b/src/Money.php index 9708188..16f0c3c 100644 --- a/src/Money.php +++ b/src/Money.php @@ -171,7 +171,13 @@ final class Money implements JsonSerializable, Arrayable, Wireable ])); } - /** Create a Money instance from a formatted string. */ + /** + * Create a Money instance from a formatted string. + * + * @param string $formatted The string formatted using the `formatted()` or `rawFormatted()` method. + * @param Currency|string|null $currency The currency to use when passing the overrides. If not provided, the currency of the formatted string is used. + * @param array ...$overrides The overrides used when formatting the money instance. + */ public static function fromFormatted(string $formatted, Currency|string $currency = null, mixed ...$overrides): self { $currency = isset($currency) diff --git a/src/PriceFormatter.php b/src/PriceFormatter.php index 67f04a9..b01010f 100644 --- a/src/PriceFormatter.php +++ b/src/PriceFormatter.php @@ -56,13 +56,13 @@ class PriceFormatter && str_ends_with($formatted, $currency->suffix()) ) { if ($possibleCurrency) { - throw new CannotExtractCurrencyException('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 as '$formatted'. Please specify the currency of the formatted string."); } $possibleCurrency = $currency; } } - return $possibleCurrency ?? throw new CannotExtractCurrencyException('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 '$formatted'."); } }