-
Notifications
You must be signed in to change notification settings - Fork 11.7k
toEnglishFormat() / add number conversion helper #57669
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
toEnglishFormat() / add number conversion helper #57669
Conversation
….com/farad-tech/laravel-framework into feature/add-number-conversion-helper
| * Converting Persian/Arabic digits to English | ||
| * @param mixed $value | ||
| * @return string | ||
| */ | ||
| function to_english_numbers($value): string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How would I know that this is specific to Persian/Arabic digits from the helper name alone? And these are not English, the are Arabic numbers, just not Eastern Arabic numerals, but Western Arabic numerals.
And what about other numbering systems?
Co-authored-by: Sebastian Hädrich <11225821+shaedrich@users.noreply.github.com>
| if (! function_exists('to_english_numbers')) { | ||
| /** | ||
| * Converting Persian/Arabic digits to English | ||
| * @param mixed $value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this really mixed? Shouldn't this be
| * @param mixed $value | |
| * @param string $value |
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I created this function to Number helper so we don't need to repeat it here. I'll remove that from helper.php
| /** | ||
| * Converting Persian/Arabic digits to English | ||
| * @param mixed $value | ||
| * @return string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After using Number::toEnglishFormat() (#57669 (comment)), the return type has to be adjusted as well:
| * @return string | |
| * @return int|float |
| /** | ||
| * Converting Persian/Arabic digits to English | ||
| * @param mixed $value | ||
| * @return bool|float|int |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why bool?
| use Illuminate\Support\Fluent; | ||
| use Illuminate\Support\HigherOrderTapProxy; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, and you have to import the Number class 😉 (#57669 (comment))
https://github.com/laravel/framework/actions/runs/19084359967/job/54521096356?pr=57669
| use Illuminate\Support\Fluent; | |
| use Illuminate\Support\Number; | |
| use Illuminate\Support\HigherOrderTapProxy; |
| $numberFormatter = new NumberFormatter('ar-u-nu-latn', NumberFormatter::DECIMAL); | ||
| return $numberFormatter->parse($value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could even use internal methods:
| $numberFormatter = new NumberFormatter('ar-u-nu-latn', NumberFormatter::DECIMAL); | |
| return $numberFormatter->parse($value); | |
| return self::parse($value, locale: 'ar-u-nu-latn'); |
Many Laravel developers working with Persian or Arabic inputs often need to normalize user-entered numeric values (for example, phone numbers or amounts) before validation or database storage. Having this as a built-in helper improves developer experience and prevents code duplication across projects.
All tests are included and passing.