Skip to content

Commit

Permalink
- Added Closure argument support to setDefaultCurrency method.
Browse files Browse the repository at this point in the history
- Added Closure argument support to setDefaultLocale method.
- Added Closure argument support to setDefaultTimeZone method.
- Updated tests.
- Updated README.
  • Loading branch information
elusivecodes committed Oct 11, 2024
1 parent 830d6d0 commit 7dfa9fd
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 12 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ $percent = Formatter::percent($value, $options);

Set the default currency.

- `$currency` is a string representing the currency code.
- `$currency` is a string representing the currency code, or a *Closure* that returns the currency code.

```php
Formatter::setDefaultCurrency($currency);
Expand All @@ -129,7 +129,7 @@ Formatter::setDefaultCurrency($currency);

Set the default locale.

- `$locale` is a string representing the locale.
- `$locale` is a string representing the locale, or a *Closure* that returns the locale.

```php
Formatter::setDefaultLocale($locale);
Expand All @@ -139,7 +139,7 @@ Formatter::setDefaultLocale($locale);

Set the default time zone.

- `$timeZone` is a string representing the time zone.
- `$timeZone` is a string representing the time zone, or a *Closure* that returns the time zone.

```php
Formatter::setDefaultTimeZone($timeZone);
Expand Down
32 changes: 23 additions & 9 deletions src/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,24 @@

namespace Fyre\Utility;

use Closure;
use Fyre\DateTime\DateTime;
use Fyre\DB\TypeParser;
use NumberFormatter;

use function call_user_func;
use function locale_get_default;

/**
* Formatter
*/
abstract class Formatter
{
protected static string $defaultCurrency = 'USD';
protected static Closure|string $defaultCurrency = 'USD';

protected static string|null $defaultLocale = null;
protected static Closure|string|null $defaultLocale = null;

protected static string|null $defaultTimeZone = null;
protected static Closure|string|null $defaultTimeZone = null;

protected static array $numberFormatters = [];

Expand Down Expand Up @@ -83,6 +85,10 @@ public static function datetime(DateTime $value, array $options = []): string
*/
public static function getDefaultCurrency(): string
{
if (static::$defaultCurrency && static::$defaultCurrency instanceof Closure) {
return call_user_func(static::$defaultCurrency);
}

return static::$defaultCurrency;
}

Expand All @@ -93,6 +99,10 @@ public static function getDefaultCurrency(): string
*/
public static function getDefaultLocale(): string
{
if (static::$defaultLocale && static::$defaultLocale instanceof Closure) {
return call_user_func(static::$defaultLocale);
}

return static::$defaultLocale ?? locale_get_default();
}

Expand All @@ -103,6 +113,10 @@ public static function getDefaultLocale(): string
*/
public static function getDefaultTimeZone(): string
{
if (static::$defaultTimeZone && static::$defaultTimeZone instanceof Closure) {
return call_user_func(static::$defaultTimeZone);
}

return static::$defaultTimeZone ?? DateTime::getDefaultTimeZone();
}

Expand Down Expand Up @@ -139,29 +153,29 @@ public static function percent(float|int|string $value, array $options = []): st
/**
* Set the default currency.
*
* @param string $currency The currency.
* @param Closure|string|null $currency The currency.
*/
public static function setDefaultCurrency(string $currency): void
public static function setDefaultCurrency(Closure|string|null $currency): void
{
static::$defaultCurrency = $currency;
}

/**
* Set the default locale.
*
* @param string $locale The locale.
* @param Closure|string|null $locale The locale.
*/
public static function setDefaultLocale(string $locale): void
public static function setDefaultLocale(Closure|string|null $locale): void
{
static::$defaultLocale = $locale;
}

/**
* Set the default time zone.
*
* @param string $timeZone The time zone.
* @param Closure|string|null $timeZone The time zone.
*/
public static function setDefaultTimeZone(string $timeZone): void
public static function setDefaultTimeZone(Closure|string|null $timeZone): void
{
static::$defaultTimeZone = $timeZone;
}
Expand Down
39 changes: 39 additions & 0 deletions tests/FormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,45 @@ public function testPercentString(): void
);
}

public function testSetDefaultCurrencyCallback(): void
{
Formatter::setDefaultCurrency(fn(): string => 'gbp');

$this->assertSame(
'£123.00',
Formatter::currency(123, [
'locale' => 'en-GB',
])
);
}

public function testSetDefaultLocaleCallback(): void
{
Formatter::setDefaultLocale(fn(): string => 'en-GB');

$this->assertSame(
'£123.00',
Formatter::currency(123, [
'currency' => 'gbp',
])
);
}

public function testSetDefaultTimeZoneCallback(): void
{
Formatter::setDefaultTimeZone(fn(): string => 'America/New_York');

$date = new DateTime('2022-01-01 11:59:59');

$this->assertSame(
'٢٠٢٢-٠١-٠١ ٠٦:٥٩:٥٩',
Formatter::datetime($date, [
'locale' => 'ar-AR',
'format' => 'yyyy-MM-dd HH:mm:ss',
])
);
}

public function testTime(): void
{
$date = new DateTime('2022-01-01 11:59:59');
Expand Down

0 comments on commit 7dfa9fd

Please sign in to comment.