Skip to content

Commit

Permalink
Make Number helper locale parameters null and default to App locale
Browse files Browse the repository at this point in the history
This makes so that if no locale parameter is specified, the configured App locale is used.
  • Loading branch information
caendesilva committed Nov 4, 2023
1 parent 8151a80 commit 9cbebdd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/Illuminate/Support/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Support;

use Illuminate\Support\Facades\App;
use Illuminate\Support\Traits\Macroable;
use NumberFormatter;
use RuntimeException;
Expand All @@ -14,14 +15,14 @@ class Number
* Format the number to a fluent human-readable string.
*
* @param float|int $number
* @param string $locale
* @param ?string $locale
* @return false|string
*/
public static function toHuman($number, $locale = 'en')
public static function toHuman($number, $locale = null)
{
static::needsIntlExtension();

$formatter = new NumberFormatter($locale, NumberFormatter::SPELLOUT);
$formatter = new NumberFormatter($locale ?? App::getLocale(), NumberFormatter::SPELLOUT);

return $formatter->format($number);
}
Expand All @@ -31,14 +32,14 @@ public static function toHuman($number, $locale = 'en')
*
* @param float|int $number
* @param string $currency
* @param string $locale
* @param ?string $locale
* @return false|string
*/
public static function toCurrency($number, $currency = 'USD', $locale = 'en')
public static function toCurrency($number, $currency = 'USD', $locale = null)
{
static::needsIntlExtension();

$formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);
$formatter = new NumberFormatter($locale ?? App::getLocale(), NumberFormatter::CURRENCY);

return $formatter->formatCurrency($number, $currency);
}
Expand Down
8 changes: 8 additions & 0 deletions tests/Support/SupportNumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@

namespace Illuminate\Tests\Support;

use Illuminate\Support\Facades\App;
use Illuminate\Support\Number;
use PHPUnit\Framework\TestCase;

class SupportNumberTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();

App::shouldReceive('getLocale')->andReturn('en');
}

public function testToHuman()
{
$this->needsIntlExtension();
Expand Down

0 comments on commit 9cbebdd

Please sign in to comment.