brick / money

A money and currency library for PHP
MIT License
1.65k stars 102 forks source link

allocateWithRemainder() performs no allocation #68

Open ghost opened 1 year ago

ghost commented 1 year ago

Prior to this change, Money::of('1', 'USD')->allocateWithRemainder(400, 0, 40, 20, 2) returned 86, 0, 8, 4, 0, 2 (as cents wrapped in Money instances of course), and I distributed the 2 cents remainder by adding one to the 86 figure, one to the 8 figure.

After the change, Money::of('1', 'USD')->allocateWithRemainder(400, 0, 40, 20, 2) returns 0, 0, 0, 0, 0, 100.

I understand the reason behind this, however allocateWithRemainder() is no longer helpful in cases like the above, as it does no allocation at all.

Is there anything within the library that can help me with that? Or should I resort to allocating the 100 cents myself (probably by weight)?

Thanks!

BenMorel commented 1 year ago

Hi, I did not think that the previous behaviour could be useful, sorry for "fixing" it!

One solution I can see is to provide both behaviours, either under different method names, or behind a flag. I'm open to suggestions regarding naming!

Maybe even both implementations could be merged into allocate(), which would now have 3 possible behaviours depending on a flag... although this can become quickly messy.

In the meantime, I guess you could just copy the old implementation in your own codebase?

ghost commented 1 year ago

Thanks Ben, I'll stay on v0.5.3 for now, but will look into replicating the old implementation in my own codebase as you suggested. 👍

BenMorel commented 1 year ago

I'd like to re-introduce the old behaviour in some form though, do you know how we could name the old and the new behaviour?

ghost commented 1 year ago

Sorry for the late reply, I ended up adding the needed functionality in an external helper named apportion(Money $amount, array $ratios): array as the definition of "apportion" seemed fitting for the purpose. 👍