moneyphp / money

PHP implementation of Fowler's Money pattern.
http://moneyphp.org
MIT License
4.63k stars 441 forks source link

From Numeric Code? #536

Closed kunicmarko20 closed 5 years ago

kunicmarko20 commented 5 years ago

Hi, I can't find a way to test if the numeric currency code is valid or convert the numeric code.

I am able to provide a PR for this if you think there is a place for this in lib.

kunicmarko20 commented 5 years ago

Another option is exposing the whole array and I can do that in my code, but getCurrencies is private and getIterator doesn't give numeric codes

UlrichEckhardt commented 5 years ago

I'm not 100% sure about the exact place, but I believe that you can loop using an iterator with the foreach ($iterator as $key => $value) {...} syntax. However, I'm wondering what your use-case is and whether the numeric index shouldn't be treated as implementation detail/artifact.

kunicmarko20 commented 5 years ago

Iterator only returns the letter code:

https://github.com/moneyphp/money/blob/cdf172c75f0dcb514fa4756b14033f472129d80e/src/Currencies/ISOCurrencies.php#L64-L74

I am using moneyphp internaly, for money and currencies and I am getting numeric code from a external service, to be able to use currency I need to convert numeric code to letter code as this library checkes only the letter code:

https://github.com/moneyphp/money/blob/cdf172c75f0dcb514fa4756b14033f472129d80e/src/Currencies/ISOCurrencies.php#L28

gmponos commented 5 years ago

Note that numeric code is used only for ISO currencies and does not exist for all currencies or in the interface.

kunicmarko20 commented 5 years ago

Yep, what would work for me is having a method that would convert numeric to letter or just exposing the whole array of values. Does one of those 2 sound like a good idea?

frederikbosch commented 5 years ago

I think this outside Money scope. You will have to care of this tourself. You might want to use https://github.com/moneyphp/currencies and create your own map with numeric code as array key.

kunicmarko20 commented 5 years ago

Can you please elaborate why is it out of scope? Numeric code is also part of ISO 4217.

frederikbosch commented 5 years ago

We don’t want to move towards a currency repository library. This library solves problems with money calculations. If you need to convert currency numeric <-> letter codes, this should happen in your own code.

kunicmarko20 commented 5 years ago

I agree on the conversion part but a "Currencies" class that validates both letter and number code should probably be a part of the lib.

Something like this https://github.com/alcohol/iso4217/blob/master/ISO4217.php#L26-L35

Don't want to bother you anymore, thank you for replying.

frederikbosch commented 5 years ago

You are free to create a PR. Then we can discuss whether or not we want to merge it.