Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Illuminate/Support/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,4 +439,15 @@ protected static function ensureIntlExtensionIsInstalled()
throw new RuntimeException('The "intl" PHP extension is required to use the ['.$method.'] method.');
}
}

/**
* Converting Persian/Arabic digits to English
* @param mixed $value
* @return bool|float|int
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why bool?

*/
public static function toEnglishFormat($value)
{
$numberFormatter = new NumberFormatter('ar-u-nu-latn', NumberFormatter::DECIMAL);
return $numberFormatter->parse($value);
Comment on lines +450 to +451
Copy link
Contributor

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:

Suggested change
$numberFormatter = new NumberFormatter('ar-u-nu-latn', NumberFormatter::DECIMAL);
return $numberFormatter->parse($value);
return self::parse($value, locale: 'ar-u-nu-latn');

}
}
13 changes: 13 additions & 0 deletions src/Illuminate/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -523,3 +523,16 @@
return is_null($callback) ? $value : $callback($value);
}
}

if (! function_exists('to_english_numbers')) {
/**
* Converting Persian/Arabic digits to English
* @param mixed $value
Copy link
Contributor

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

Suggested change
* @param mixed $value
* @param string $value

?

Copy link
Author

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

* @return string
Copy link
Contributor

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:

Suggested change
* @return string
* @return int|float

*/
function to_english_numbers($value): string
Comment on lines +529 to +533
Copy link
Contributor

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?

{
return Number::toEnglishFormat($value);

Check failure on line 535 in src/Illuminate/Support/helpers.php

View workflow job for this annotation

GitHub Actions / Source Code

Call to static method toEnglishFormat() on an unknown class Number.
}
}

7 changes: 7 additions & 0 deletions tests/Support/SupportHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1572,6 +1572,13 @@ public function testPregReplaceArray($pattern, $replacements, $subject, $expecte
preg_replace_array($pattern, $replacements, $subject)
);
}

public function testToEnglishNumbersConvertsPersianAndArabicDigits()
{
$this->assertEquals('123', to_english_numbers('۱۲۳'));
$this->assertEquals('456', to_english_numbers('٤٥٦'));
}

}

trait SupportTestTraitOne
Expand Down
7 changes: 7 additions & 0 deletions tests/Support/SupportNumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,4 +370,11 @@ public function testParseFloat()
$this->assertSame(1234.56, Number::parseFloat('1.234,56', locale: 'de'));
$this->assertSame(1234.56, Number::parseFloat('1 234,56', locale: 'fr'));
}

#[RequiresPhpExtension('intl')]
public function testToEnglishFormat()
{
$this->assertEquals('123', Number::toEnglishFormat('۱۲۳'));
$this->assertEquals('456', Number::toEnglishFormat('٤٥٦'));
}
}
Loading