In the v3 major, various strongly types classed were introduced for typesafe handling of data, including the (very relevant) Money class. This removes various drawbacks from earlier versions. However, one is still required to provide very particularly formatted strings for money values, due to upstream API limitations. This is both easily susceptible to human error and moreover a likely layer that requires conversion in lots of applications, as one will rarely use strings to deal with amounts of money.
To ease this process, I would suggest adding one or more (static) methods to the Money class, that for example could do the following things:
- accept an integer amount (e.g.
Money::createFromCents), then use say number_format($amount / 100, 2, '.', '') to correctly format this string
- accept a float amount (e.g.
Money::createFromFloat), then use say number_format($amount, 2, '.', '')` to correctly format this string
For me personally, I would very much also like a similar method that would except a Money instance from brick/money to create money here, say Money::createFromBrickMoney($money) => new (amount: (string) $money->getAmount(), currency: $money->getCurrency()->getCurrencyCode()), with this package then added as a suggestion to the composer depends. However, it is understandable if such a request is considered out of scope.
Similarly, the moneyphp/money library also seems to be a widely used library for dealing with money inside PHP, which could justify adding a conversion method here for convenience.
In the v3 major, various strongly types classed were introduced for typesafe handling of data, including the (very relevant)
Moneyclass. This removes various drawbacks from earlier versions. However, one is still required to provide very particularly formatted strings for money values, due to upstream API limitations. This is both easily susceptible to human error and moreover a likely layer that requires conversion in lots of applications, as one will rarely use strings to deal with amounts of money.To ease this process, I would suggest adding one or more (static) methods to the
Moneyclass, that for example could do the following things:Money::createFromCents), then use saynumber_format($amount / 100, 2, '.', '')to correctly format this stringMoney::createFromFloat), then use saynumber_format($amount, 2, '.', '')` to correctly format this stringFor me personally, I would very much also like a similar method that would except a
Moneyinstance from brick/money to create money here, sayMoney::createFromBrickMoney($money) => new (amount: (string) $money->getAmount(), currency: $money->getCurrency()->getCurrencyCode()), with this package then added as a suggestion to the composer depends. However, it is understandable if such a request is considered out of scope.Similarly, the moneyphp/money library also seems to be a widely used library for dealing with money inside PHP, which could justify adding a conversion method here for convenience.