8000 Money instance creation from a formatted string by gauravmak · Pull Request #16 · archtechx/money · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Money instance creation from a formatted string #16

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

Merged
merged 13 commits into from
Apr 5, 2022

Conversation

gauravmak
Copy link
Contributor
@gauravmak gauravmak commented Mar 9, 2022

This PR adds an additional method to create a money instance from a formatted string.

Changes overview:

  • New static method fromFormatted() inside the Money class which:
    1. Accepts currency and currency configuration overrides (optional)
    2. Tries to extract the currency if it is not provided by the user
    3. Throws an exception if no currency matches the prefix and suffix of the formatted string
    4. Throws an exception if multiple currencies match the prefix and suffix of the formatted string
    5. Calls the PriceFormatter::resolve() to get the decimal value
  • New static method resolve() inside the PriceFormatter class to manage the decimal extraction. A process on formatted string:
    1. Removes the prefix and suffix
    2. Removes everything other than the number of the decimal separator
    3. Replaces decimal formatted string with a . (period) to allow the following float conversion
    4. Converts to float and returns the decimal value
  • New tests inside the MoneyTest class for the new method
  • Readme updates to inform users about the new method's functionality
  • Improve code readability by using the pow() PHP function

Closes #1

@stancl
Copy link
Member
stancl commented Mar 9, 2022

Do you think the method could resolve the currency as well?

It will be ambiguous sometimes (e.g. USD and AUD), but when it's not, it'd be cool if it could infer the currency from the string.

@gauravmak
8000 Copy link
Contributor Author

@stancl Now it extracts the currency from the formatted string.

gauravmak and others added 2 commits March 15, 2022 16:46
@gauravmak gauravmak requested a review from stancl March 15, 2022 11:35
@stancl
Copy link
Member
stancl commented Apr 5, 2022

Looks great, thanks!

@stancl stancl merged commit 545610e into archtechx:master Apr 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Money::fromString() method for converting formatted strings back into Money instances
2 participants
0