Skip to content

Add additional convenience creation methods for Money class #876

@jnoordsij

Description

@jnoordsij

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions