-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
263 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace h4kuna\Format\Number\NativePhp; | ||
|
||
class NumberFormatter extends \NumberFormatter | ||
{ | ||
public function format(float|int $num, int $type = 0): string | ||
{ | ||
return (string) parent::format($num, $type); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace h4kuna\Format\Number\NativePhp; | ||
|
||
use Locale; | ||
|
||
final class NumberFormatterFactory | ||
{ | ||
private string $locale; | ||
|
||
|
||
public function __construct(?string $locale = null) | ||
{ | ||
$this->locale = $locale ?? Locale::getDefault(); | ||
} | ||
|
||
|
||
/** | ||
* @template T of NumberFormatter | ||
* @param int|class-string<T> $style | ||
* @return T | ||
*/ | ||
public function create( | ||
?int $decimals = null, | ||
?string $locale = null, | ||
int|string $style = NumberFormatter::DECIMAL | ||
): NumberFormatter | ||
{ | ||
$locale ??= $this->locale; | ||
$formatter = is_int($style) | ||
? new NumberFormatter($locale, $style) | ||
: new $style($locale); | ||
|
||
if ($decimals !== null) { | ||
$formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $decimals); | ||
} | ||
|
||
return $formatter; | ||
Check failure on line 38 in src/Number/NativePhp/NumberFormatterFactory.php GitHub Actions / PHPStan
|
||
} | ||
|
||
|
||
public function currency(?string $locale = null): NumberFormatter | ||
{ | ||
return $this->create(null, $locale, NumberFormatter::CURRENCY); | ||
} | ||
|
||
|
||
public function ordinal(?string $locale = null): NumberFormatter | ||
{ | ||
return $this->create(null, $locale, NumberFormatter::ORDINAL); | ||
} | ||
|
||
|
||
public function percent(?int $decimals = 0, ?string $locale = null): NumberFormatterPercent | ||
{ | ||
return $this->create($decimals, $locale, NumberFormatterPercent::class); | ||
} | ||
|
||
|
||
public function spell(?string $locale = null): NumberFormatter | ||
{ | ||
return $this->create(null, $locale, NumberFormatter::SPELLOUT); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace h4kuna\Format\Number\NativePhp; | ||
|
||
use h4kuna\Format\Utils\Percentage; | ||
|
||
final class NumberFormatterPercent extends NumberFormatter | ||
{ | ||
public function __construct(string $locale) | ||
{ | ||
parent::__construct($locale, self::PERCENT); | ||
} | ||
|
||
|
||
public function format(float|int $num, int $type = 0): string | ||
{ | ||
return parent::format(Percentage::smallRatio((float) $num), $type); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
tests/src/Number/NativePhp/NumberFormatterFactoryTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace h4kuna\Format\Tests\Number\NativePhp; | ||
|
||
use Closure; | ||
use h4kuna\Format\Number\NativePhp\NumberFormatterFactory; | ||
use h4kuna\Format\Tests\TestCase; | ||
use h4kuna\Format\Utils\Space; | ||
use NumberFormatter; | ||
use Tester\Assert; | ||
|
||
require_once __DIR__ . '/../../../bootstrap.php'; | ||
|
||
final class NumberFormatterFactoryTest extends TestCase | ||
{ | ||
/** | ||
* @return array<string|int, array{0: Closure(static):void}> | ||
*/ | ||
public function dataNumber(): array | ||
{ | ||
return [ | ||
[ | ||
function (self $self) { | ||
$self->assertNbsp( | ||
self::factory()->create(), | ||
'1 000,345', | ||
); | ||
}, | ||
], | ||
[ | ||
function (self $self) { | ||
$self->assertNbsp( | ||
self::factory()->create(0), | ||
'1 000', | ||
); | ||
}, | ||
], | ||
[ | ||
function (self $self) { | ||
$self->assertNbsp( | ||
self::factory()->create(1), | ||
'1 000,3', | ||
); | ||
}, | ||
], | ||
[ | ||
function (self $self) { | ||
$self->assertNbsp( | ||
self::factory()->create(4), | ||
'1 000,3450', | ||
); | ||
}, | ||
], | ||
[ | ||
function (self $self) { | ||
$self->assertNbsp( | ||
self::factory()->create(2, 'en_GB'), | ||
'1,000.34', | ||
); | ||
}, | ||
], | ||
[ | ||
function (self $self) { | ||
$self->assertNbsp( | ||
self::factory()->currency('en_GB'), | ||
'£1,000.34', | ||
); | ||
}, | ||
], | ||
[ | ||
function (self $self) { | ||
$self->assertNbsp( | ||
self::factory()->currency(), | ||
'1 000,34 Kč', | ||
); | ||
}, | ||
], | ||
[ | ||
function (self $self) { | ||
$self->assertNbsp( | ||
self::factory()->ordinal(), | ||
'1 000.', | ||
); | ||
}, | ||
], | ||
[ | ||
function (self $self) { | ||
$self->assertNumber( | ||
self::factory()->spell(), | ||
'jedna tisíc čárka tři čtyři pět', | ||
); | ||
}, | ||
], | ||
[ | ||
function (self $self) { | ||
$self->assertNbsp( | ||
self::factory()->percent(), | ||
'1 000 %', | ||
); | ||
}, | ||
], | ||
[ | ||
function (self $self) { | ||
$self->assertNbsp( | ||
self::factory()->percent(1), | ||
'1 000,3 %', | ||
); | ||
}, | ||
], | ||
]; | ||
} | ||
|
||
|
||
/** | ||
* @param Closure(static):void $assert | ||
* @dataProvider dataNumber | ||
*/ | ||
public function testNumber(Closure $assert): void | ||
{ | ||
$assert($this); | ||
} | ||
|
||
|
||
public function assertNbsp( | ||
NumberFormatter $formatter, | ||
string $expected, | ||
): void | ||
{ | ||
Assert::same(Space::nbsp($expected), $formatter->format(1000.345)); | ||
} | ||
|
||
|
||
public function assertNumber( | ||
NumberFormatter $formatter, | ||
string $expected, | ||
): void | ||
{ | ||
Assert::same($expected, $formatter->format(1000.345)); | ||
} | ||
|
||
|
||
private static function factory(): NumberFormatterFactory | ||
{ | ||
return new NumberFormatterFactory('cs_CZ'); | ||
} | ||
} | ||
|
||
(new NumberFormatterFactoryTest())->run(); | ||
|