Skip to content

Commit

Permalink
Narrow localized() return types
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Aug 9, 2024
1 parent 59911f0 commit bc4c8a9
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 34 deletions.
22 changes: 14 additions & 8 deletions lib/CurrencyFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,29 @@
namespace ICanBoogie\CLDR;

use ICanBoogie\CLDR\Numbers\Symbols;

use function str_replace;

/**
* A currency formatter.
*
* @implements Localizable<CurrencyFormatter, LocalizedCurrencyFormatter>
*/
final class CurrencyFormatter extends NumberFormatter implements Localizable
final class CurrencyFormatter implements Formatter, Localizable
{
public const DEFAULT_CURRENCY_SYMBOL = '¤';

public function __construct(
private readonly NumberFormatter $number_formatter,
) {
}

/**
* @inheritDoc
* Formats a number with the specified pattern.
*
* @param float|int|numeric-string $number
* The number to format.
* @param string|NumberPattern $pattern
* The pattern used to format the number.
*/
public function format(
float|int|string $number,
Expand All @@ -27,14 +36,11 @@ public function format(
return str_replace(
self::DEFAULT_CURRENCY_SYMBOL,
$currencySymbol,
parent::format($number, $pattern, $symbols)
$this->number_formatter->format($number, $pattern, $symbols)
);
}

/**
* @return LocalizedCurrencyFormatter
*/
public function localized(Locale $locale): LocalizedObject
public function localized(Locale $locale): LocalizedCurrencyFormatter
{
return new LocalizedCurrencyFormatter($this, $locale);
}
Expand Down
5 changes: 1 addition & 4 deletions lib/ListFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,7 @@ private function format_pattern(string $pattern, string $v0, string $v1): string
]);
}

/**
* @return LocalizedListFormatter
*/
public function localized(Locale $locale): LocalizedObject
public function localized(Locale $locale): LocalizedListFormatter
{
return new LocalizedListFormatter($this, $locale);
}
Expand Down
7 changes: 2 additions & 5 deletions lib/Locale.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,7 @@ private function get_units(): Units
return $this->units ??= new Units($this);
}

/**
* @return LocalizedLocale
*/
public function localized(Locale|LocaleId|string $locale): LocalizedObject
public function localized(Locale|LocaleId|string $locale): LocalizedLocale
{
if (!$locale instanceof self) {
$locale = $this->repository->locale_for($locale);
Expand Down Expand Up @@ -238,7 +235,7 @@ public function format_currency(
}

/**
* Formats a variable-length lists of scalars.
* Formats variable-length lists of scalars.
*
* @param scalar[] $list
*
Expand Down
9 changes: 2 additions & 7 deletions lib/NumberFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* @implements Localizable<NumberFormatter, LocalizedNumberFormatter>
*/
class NumberFormatter implements Formatter, Localizable
final class NumberFormatter implements Formatter, Localizable
{
/**
* Formats a number with the specified pattern.
Expand Down Expand Up @@ -56,12 +56,7 @@ public function format(
]);
}

/**
* Localizes the instance.
*
* @return LocalizedNumberFormatter
*/
public function localized(Locale $locale): LocalizedObject
public function localized(Locale $locale): LocalizedNumberFormatter
{
return new LocalizedNumberFormatter($this, $locale);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private function get_number_formatter(): NumberFormatter

private function get_currency_formatter(): CurrencyFormatter
{
return $this->currency_formatter ??= new CurrencyFormatter();
return $this->currency_formatter ??= new CurrencyFormatter($this->get_number_formatter());
}

private ListFormatter $list_formatter;
Expand Down
5 changes: 1 addition & 4 deletions lib/Territory.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,7 @@ public function name_as(string|LocaleId $locale_id): string
return $this->localized($locale_id)->name;
}

/**
* @return LocalizedTerritory
*/
public function localized(Locale|LocaleId|string $locale): LocalizedObject
public function localized(Locale|LocaleId|string $locale): LocalizedTerritory
{
if (!$locale instanceof Locale) {
$locale = $this->repository->locale_for($locale);
Expand Down
11 changes: 6 additions & 5 deletions tests/LocalizedCurrencyFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use ICanBoogie\CLDR\CurrencyFormatter;
use ICanBoogie\CLDR\LocalizedCurrencyFormatter;
use ICanBoogie\CLDR\NumberFormatter;
use ICanBoogie\CLDR\Spaces;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
Expand All @@ -12,11 +13,11 @@ final class LocalizedCurrencyFormatterTest extends TestCase
{
use StringHelpers;

private CurrencyFormatter $formatter;
private CurrencyFormatter $sut;

protected function setUp(): void
{
$this->formatter = new CurrencyFormatter();
$this->sut = new CurrencyFormatter(new NumberFormatter());
}

#[DataProvider('provide_test_format')]
Expand All @@ -27,7 +28,7 @@ public function test_format(
string $expected
): void {
$formatter = new LocalizedCurrencyFormatter(
$this->formatter,
$this->sut,
locale_for($locale_id),
);

Expand Down Expand Up @@ -65,7 +66,7 @@ public function test_format_accounting(
string $expected
): void {
$formatter = new LocalizedCurrencyFormatter(
$this->formatter,
$this->sut,
locale_for($locale_id),
);

Expand Down Expand Up @@ -98,7 +99,7 @@ public static function provide_test_format_accounting(): array
public function test_should_format_with_custom_pattern(): void
{
$sut = new LocalizedCurrencyFormatter(
$this->formatter,
$this->sut,
locale_for('fr'),
);

Expand Down

0 comments on commit bc4c8a9

Please sign in to comment.