-
-
Notifications
You must be signed in to change notification settings - Fork 102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
No support for adding CurrencyProviders #61
Comments
Hi, I don't have a problem with introducing a IMO, if you're using something else than ISO currencies, you should create your Monies from a factory that gets the provider injected, not from |
Absolutely I agree. I wouldn't want a secondary provider to come in through Any suggestions as to how such a factory might work? If For my project, i have done the following, any alternative to this?:
|
Hi! Nice library, thanks. However, version 0.8.0 completely removes HRK from the ISO currencies. While this is correct, we have legacy data in Croatian Kuna, and we use Laravel Nova which interfaces directly to Money::of or Money::ofMinor and does not allow us to hook into this and provide a custom new Currency(...) easily. Is it possible to implement an extension to ISOCurrencyProvider such as addCurrency(new Currency(...)); which adds the new currency to $currencyData for the singleton? This would allow us (or anyone else) to add currencies at boot time and not have to hook deeper into the system. Thanks for considering! |
@puzzledmonkey I'm generally against adding any kind of global state configuration, as it may have side effects if several parts of your application use brick/money. What exactly prevents Laravel Nova from using a factory to create Money instances? |
Well I can't modify their code, so for now I've just made a small field class that extends their Currency field and overrides the method that uses Money::of. I understand you're against any kind of global state configuration, but isn't that kind of what the ISOCurrentProvider singleton's currencyData provides? Of course it's up to you, but I just figured it would help people in our case where a legacy currency has been removed but needs supporting for the forseeable future too. |
Note: for now I just overrode the Nova Currency field method and add the custom currency in as necessary. However I'm not sure this is the best method either because I'm creating a new Currency object for each call to the field. A global setting would only need to instantiate it once. |
So we should use it like this if HRK is needed? if ('HRK' === $currency) {
// https://github.com/brick/money/blob/eb7fe4cee92325589dada779000758c0e56b5508/data/iso-currencies.php#L63C5-L63C38
$currency = new Brick\Money\Currency('HRK', 191, 'Kuna', 2);
} else {
// leave as is
}
Brick\Money\Money::of($number, $currency); |
yes that's pretty much exactly the code i used to get this working |
@hrvoj3e Pretty much, yes. I would advise to create a service that gets injected throughout your application, and centralize the currency logic there. It could look like this: class CurrencyProvider
{
public function getCurrency(string|int $currencyCode): Currency
{
if ('HRK' === $currencyCode) {
return new Currency('HRK', 191, 'Kuna', 2);
}
return Currency::of($currencyCode);
}
} and pass the resulting The library could provide a The only options I can think of:
If anything, I prefer option 1. At least we don't provide two ways of building Money objects. In either case, you'll need support from your DI container to always have a Other ideas welcome! |
what about my idea above? "Is it possible to implement an extension to ISOCurrencyProvider such as addCurrency(new Currency(...)); which adds the new currency to $currencyData for the singleton? This would allow us (or anyone else) to add currencies at boot time and not have to hook deeper into the system." |
@puzzledmonkey Unfortunately, this falls under global configuration, which I try to avoid at all costs. |
By the way, I forgot about #30 which started the same conversation back in 2020. |
Hi,
Although you do support custom currencies through
new Currency
, the project does not support the expectation that the Money class may not rely on the ISOCurrencyProvider, and could instead rely on a CurrencyProviderInterface.I propose making ISOCurrencyProvider an implementation of CurrencyProviderInterface, and setting it to default to maintain the existing functionality, and then enable users to set their own Concrete CurrencyProvider to allow for additional options.
This would cover the case where cryptocurrencies (popularity rising as they are!) can be built using the interface and be validated correctly, instead of having to manually override the string
currency
with anew Currency()
every time. This is for sure necessary as a developer when you want to dynamically load a list of available crypto's. I wouldn't expect the CryptoCurrencyProvider to be a part of this project, but at least the Interface to support additional non standard currencies.I would be more than happy to pick this work up and submit a PR. I am writing it anyway for my local project but without the reliance on the Interface :)
Thanks.
The text was updated successfully, but these errors were encountered: