Skip to content

Commit 1e9f1d8

Browse files
committed
simplify Formatter instantiation and public API
1 parent f0abe34 commit 1e9f1d8

File tree

7 files changed

+59
-70
lines changed

7 files changed

+59
-70
lines changed

config/bakame-intl-formatter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@
3535
|
3636
| Refer to the docs for more information: https://unicode-org.github.io/icu/userguide/format_parse/datetime/
3737
|
38-
| Expected: string
38+
| Expected: ?string
3939
*/
40-
'pattern' => '',
40+
'pattern' => null,
4141
],
4242
'number' => [
4343
/*
@@ -59,9 +59,9 @@
5959
|
6060
| Pattern string if the chosen style requires a pattern.
6161
|
62-
| Expected: string
62+
| Expected: ?string
6363
*/
64-
'pattern' => '',
64+
'pattern' => null,
6565
/*
6666
|--------------------------------------------------------------------------
6767
| Number Attributes

src/Configuration.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ final class Configuration
1313
/** @readonly */
1414
public int $style;
1515
/** @readonly */
16-
public string $datePattern;
16+
public ?string $datePattern;
1717
/** @readonly */
18-
public string $numberPattern;
18+
public ?string $numberPattern;
1919
/**
2020
* @readonly
2121
* @var array<int, int|float>
@@ -41,8 +41,8 @@ public function __construct(
4141
int $dateType,
4242
int $timeType,
4343
int $style,
44-
string $datePattern = '',
45-
string $numberPattern = '',
44+
?string $datePattern = null,
45+
?string $numberPattern = null,
4646
array $attributes = [],
4747
array $textAttributes = [],
4848
array $symbolAttributes = []
@@ -62,11 +62,11 @@ public function __construct(
6262
* date:array{
6363
* dateType:int,
6464
* timeType:int,
65-
* pattern:string
65+
* pattern:?string
6666
* },
6767
* number:array{
6868
* style:int,
69-
* pattern:string,
69+
* pattern:?string,
7070
* attributes:array<int, int|float>,
7171
* textAttributes:array<int,string>,
7272
* symbolAttributes:array<int,string>

src/Formatter.php

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -84,40 +84,19 @@ final class Formatter
8484
'after_suffix' => NumberFormatter::PAD_AFTER_SUFFIX,
8585
];
8686

87-
private IntlDateFormatter $dateFormatterPrototype;
8887
private Configuration $configuration;
8988
private DateResolver $dateResolver;
9089
/** @var array<IntlDateFormatter> */
9190
private array $dateFormatters = [];
9291
/** @var array<NumberFormatter> */
9392
private array $numberFormatters = [];
9493

95-
public function __construct(
96-
IntlDateFormatter $dateFormatterPrototype,
97-
Configuration $configuration,
98-
DateResolver $dateResolver
99-
) {
100-
$this->dateFormatterPrototype = $dateFormatterPrototype;
94+
public function __construct(Configuration $configuration, DateResolver $dateResolver)
95+
{
10196
$this->configuration = $configuration;
10297
$this->dateResolver = $dateResolver;
10398
}
10499

105-
public static function fromApplication(Configuration $configuration, DateResolver $dateResolver): self
106-
{
107-
return new self(
108-
new IntlDateFormatter(
109-
Locale::getDefault(),
110-
$configuration->dateType,
111-
$configuration->timeType,
112-
null,
113-
null,
114-
$configuration->datePattern
115-
),
116-
$configuration,
117-
$dateResolver
118-
);
119-
}
120-
121100
public function getCountryName(?string $country, string $locale = null): string
122101
{
123102
if (null === $country) {
@@ -231,14 +210,17 @@ public function formatCurrency($amount, string $currency, array $attrs = [], str
231210
public function formatNumber(
232211
$number,
233212
array $attrs = [],
234-
string $style = 'decimal',
213+
?string $style = null,
235214
string $type = 'default',
236215
string $locale = null
237216
): string {
238217
if (!isset(self::NUMBER_TYPES[$type])) {
239218
throw FailedFormatting::dueToUnknownNumberType($type, self::NUMBER_TYPES);
240219
}
241220

221+
/** @var string $style */
222+
$style = $style ?? array_search($this->configuration->style, self::NUMBER_STYLES, true);
223+
242224
$formatter = $this->createNumberFormatter($locale, $style, $attrs);
243225
if (false === $ret = $formatter->format($number, self::NUMBER_TYPES[$type])) {
244226
// @codeCoverageIgnoreStart
@@ -257,9 +239,9 @@ public function formatNumber(
257239
*/
258240
public function formatDateTime(
259241
$date,
260-
?string $dateFormat = 'medium',
261-
?string $timeFormat = 'medium',
262-
?string $pattern = '',
242+
?string $dateFormat = null,
243+
?string $timeFormat = null,
244+
?string $pattern = null,
263245
$timezone = null,
264246
string $calendar = 'gregorian',
265247
string $locale = null
@@ -286,8 +268,8 @@ public function formatDateTime(
286268
*/
287269
public function formatDate(
288270
$date,
289-
?string $dateFormat = 'medium',
290-
?string $pattern = '',
271+
?string $dateFormat = null,
272+
?string $pattern = null,
291273
$timezone = null,
292274
string $calendar = 'gregorian',
293275
string $locale = null
@@ -301,8 +283,8 @@ public function formatDate(
301283
*/
302284
public function formatTime(
303285
$date,
304-
?string $timeFormat = 'medium',
305-
?string $pattern = '',
286+
?string $timeFormat = null,
287+
?string $pattern = null,
306288
$timezone = null,
307289
string $calendar = 'gregorian',
308290
string $locale = null
@@ -327,14 +309,18 @@ private function createDateFormatter(
327309
}
328310

329311
$locale = $locale ?? Locale::getDefault();
330-
$calendar = 'gregorian' === $calendar ? IntlDateFormatter::GREGORIAN : IntlDateFormatter::TRADITIONAL;
331-
$dateFormatValue = self::DATE_FORMATS[$dateFormat] ?? $this->dateFormatterPrototype->getDateType();
332-
$timeFormatValue = self::DATE_FORMATS[$timeFormat] ?? $this->dateFormatterPrototype->getTimeType();
333-
$pattern = $pattern ?? $this->dateFormatterPrototype->getPattern();
312+
$calendar = 'gregorian' === strtolower($calendar) ? IntlDateFormatter::GREGORIAN : IntlDateFormatter::TRADITIONAL;
313+
$dateFormatValue = self::DATE_FORMATS[$dateFormat] ?? $this->configuration->dateType;
314+
$timeFormatValue = self::DATE_FORMATS[$timeFormat] ?? $this->configuration->timeType;
315+
$pattern = $pattern ?? $this->configuration->datePattern;
334316

335317
$hash = $locale.'|'.$dateFormatValue.'|'.$timeFormatValue.'|'.$timezone->getName().'|'.$calendar.'|'.$pattern;
336318
if (!isset($this->dateFormatters[$hash])) {
337-
$this->dateFormatters[$hash] = new IntlDateFormatter($locale, $dateFormatValue, $timeFormatValue, $timezone, $calendar, $pattern);
319+
$dateFormatter = new IntlDateFormatter($locale, $dateFormatValue, $timeFormatValue, $timezone, $calendar);
320+
if (null !== $pattern) {
321+
$dateFormatter->setPattern($pattern);
322+
}
323+
$this->dateFormatters[$hash] = $dateFormatter;
338324
}
339325

340326
return $this->dateFormatters[$hash];
@@ -408,5 +394,9 @@ private function addDefaultAttributes(NumberFormatter $numberFormatter): void
408394
foreach ($this->configuration->symbolAttributes as $attribute => $value) {
409395
$numberFormatter->setSymbol($attribute, $value);
410396
}
397+
398+
if (null !== $this->configuration->numberPattern) {
399+
$numberFormatter->setPattern($this->configuration->numberPattern);
400+
}
411401
}
412402
}

src/FormatterTest.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ protected function setUp(): void
2222
{
2323
parent::setUp();
2424

25-
$this->formatter = Formatter::fromApplication(
25+
$this->formatter = new Formatter(
2626
new Configuration(
2727
IntlDateFormatter::FULL,
2828
IntlDateFormatter::FULL,
@@ -39,15 +39,14 @@ public function it_can_be_instantiated_with_a_different_configuration(): void
3939
IntlDateFormatter::FULL,
4040
IntlDateFormatter::FULL,
4141
NumberFormatter::DECIMAL,
42-
'',
43-
'',
42+
null,
43+
null,
4444
[NumberFormatter::FRACTION_DIGITS => 1],
4545
[NumberFormatter::POSITIVE_PREFIX => '++'],
4646
[NumberFormatter::DECIMAL_SEPARATOR_SYMBOL => 'x'],
4747
);
4848

4949
$formatter = new Formatter(
50-
new IntlDateFormatter('fr', IntlDateFormatter::FULL, IntlDateFormatter::FULL),
5150
$configuration,
5251
new CarbonDateResolver()
5352
);
@@ -62,19 +61,19 @@ public function it_can_handle_date_type(): void
6261
$dateImmutable = new DateTimeImmutable('2019-08-07 23:39:12');
6362
$date = new DateTime('2019-08-07 23:39:12');
6463

65-
self::assertSame('Jun 3, 2022', $this->formatter->formatDate(1654247542));
66-
self::assertSame('Jun 3, 2022', $this->formatter->formatDate('1654247542'));
64+
self::assertSame('Jun 3, 2022', $this->formatter->formatDate(1654247542, 'medium'));
65+
self::assertSame('Jun 3, 2022', $this->formatter->formatDate('1654247542', 'medium'));
6766
self::assertSame($this->formatter->formatDate(null), $this->formatter->formatDate('NoW'));
6867
self::assertSame($this->formatter->formatDate($date), $this->formatter->formatDate($dateImmutable));
6968
self::assertSame($this->formatter->formatDate($date), $this->formatter->formatDate($dateString));
7069
self::assertSame(
71-
$this->formatter->formatDate($dateString, 'short', null, 'Africa/Kinshasa'),
72-
$this->formatter->formatDate($dateString, 'short', null, new DateTimeZone('Africa/Kinshasa'))
70+
$this->formatter->formatDate($dateString, 'full', null, 'Africa/Kinshasa'),
71+
$this->formatter->formatDate($dateString, 'full', null, new DateTimeZone('Africa/Kinshasa'))
7372
);
7473

7574
self::assertNotSame(
76-
$this->formatter->formatDate($dateString, 'short', null, 'Africa/Kinshasa'),
77-
$this->formatter->formatDate($dateString, 'short', null, false)
75+
$this->formatter->formatDateTime($dateString, 'full', 'full', null, 'Africa/Kinshasa'),
76+
$this->formatter->formatDateTime($dateString, 'full', 'full', null, false)
7877
);
7978
}
8079

src/Provider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function register(): void
2121
$this->app->singleton('bakame.intl.formatter.config', fn ($app): Configuration => Configuration::fromApplication(
2222
$app->make('config')->get('bakame.intl.formatter.settings')
2323
));
24-
$this->app->singleton('bakame.intl.formatter', fn ($app): Formatter => Formatter::fromApplication(
24+
$this->app->singleton('bakame.intl.formatter', fn ($app): Formatter => new Formatter(
2525
$app->make('bakame.intl.formatter.config'),
2626
$app->make('bakame.date.resolver')
2727
));

src/helpers.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ function format_currency(
7979
function format_number(
8080
$number,
8181
array $attrs = [],
82-
string $style = 'decimal',
82+
?string $style = null,
8383
string $type = 'default',
8484
string $locale = null
8585
): string {
@@ -94,9 +94,9 @@ function format_number(
9494
*/
9595
function format_datetime(
9696
$date,
97-
?string $dateFormat = 'medium',
98-
?string $timeFormat = 'medium',
99-
?string $pattern = '',
97+
?string $dateFormat = null,
98+
?string $timeFormat = null,
99+
?string $pattern = null,
100100
$timezone = null,
101101
string $calendar = 'gregorian',
102102
string $locale = null
@@ -120,8 +120,8 @@ function format_datetime(
120120
*/
121121
function format_date(
122122
$date,
123-
?string $dateFormat = 'medium',
124-
?string $pattern = '',
123+
?string $dateFormat = null,
124+
?string $pattern = null,
125125
$timezone = null,
126126
string $calendar = 'gregorian',
127127
string $locale = null
@@ -144,8 +144,8 @@ function format_date(
144144
*/
145145
function format_time(
146146
$date,
147-
?string $timeFormat = 'medium',
148-
?string $pattern = '',
147+
?string $timeFormat = null,
148+
?string $pattern = null,
149149
$timezone = null,
150150
string $calendar = 'gregorian',
151151
string $locale = null

test_files/format_date.blade.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{{ format_datetime('2019-08-07 23:39:12') }}
2-
{{ format_datetime('2019-08-07 23:39:12', 'medium', 'medium', '', null, 'gregorian', 'fr') }}
3-
{{ format_datetime('2019-08-07 23:39:12', 'none', 'short', '', null, 'gregorian', 'fr') }}
4-
{{ format_datetime('2019-08-07 23:39:12', 'short', 'none', '', null, 'gregorian', 'fr') }}
5-
{{ format_datetime('2019-08-07 23:39:12', 'full', 'full', '', null, 'gregorian', 'fr') }}
2+
{{ format_datetime('2019-08-07 23:39:12', 'medium', 'medium', null, null, 'gregorian', 'fr') }}
3+
{{ format_datetime('2019-08-07 23:39:12', 'none', 'short', null, null, 'gregorian', 'fr') }}
4+
{{ format_datetime('2019-08-07 23:39:12', 'short', 'none', null, null, 'gregorian', 'fr') }}
5+
{{ format_datetime('2019-08-07 23:39:12', 'full', 'full', null, null, 'gregorian', 'fr') }}
66
{{ format_datetime('2019-08-07 23:39:12', 'medium', 'medium', "hh 'oclock' a, zzzz") }}
77

88
{{ format_date('2019-08-07 23:39:12') }}
9-
{{ format_date('2019-08-07 23:39:12', 'medium', '', null, 'grogorian', 'fr') }}
9+
{{ format_date('2019-08-07 23:39:12', 'medium', null, null, 'gregorian', 'fr') }}
1010
{{ format_time('2019-08-07 23:39:12') }}

0 commit comments

Comments
 (0)