Skip to content

Commit df8f1dd

Browse files
authored
feat: use defaults for locale in Intl\Locale and Config (#40)
1 parent 0fad4ce commit df8f1dd

6 files changed

Lines changed: 32 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1515
- Add `UnableToFormatStringException` from which other formatting exceptions will descend.
1616
- Add `UnableToFormatDateTimeException` thrown when we're unable to format a date or time string.
1717
- Allow instantiation of `FormatPHP` without configuration or message collection instances; FormatPHP will use the system's default locale, in this case.
18+
- Instantiation of `Intl\Locale` without a locale argument will default to the system default locale.
19+
- Instantiation of `Config` without a locale argument will create an `Intl\Locale` using the system default locale.
1820

1921
### Changed
2022

src/Config.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
namespace FormatPHP;
2424

2525
use FormatPHP\Extractor\IdInterpolator;
26+
use FormatPHP\Intl\Locale;
2627
use FormatPHP\Intl\LocaleInterface;
2728

2829
/**
@@ -43,12 +44,12 @@ class Config implements ConfigInterface
4344
* @param array<string, callable(string):string> $defaultRichTextElements
4445
*/
4546
public function __construct(
46-
LocaleInterface $locale,
47+
?LocaleInterface $locale = null,
4748
?LocaleInterface $defaultLocale = null,
4849
array $defaultRichTextElements = [],
4950
string $idInterpolatorPattern = IdInterpolator::DEFAULT_ID_INTERPOLATION_PATTERN
5051
) {
51-
$this->locale = $locale;
52+
$this->locale = $locale ?? new Locale();
5253
$this->defaultLocale = $defaultLocale;
5354
$this->idInterpolatorPattern = $idInterpolatorPattern;
5455
$this->defaultRichTextElements = $defaultRichTextElements;

src/FormatPHP.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,9 @@
2727
use Exception as PhpException;
2828
use FormatPHP\Intl\DateTimeFormat;
2929
use FormatPHP\Intl\DateTimeFormatOptions;
30-
use FormatPHP\Intl\Locale;
3130
use FormatPHP\Intl\MessageFormat;
3231
use FormatPHP\Util\MessageCleaner;
3332
use FormatPHP\Util\MessageRetriever;
34-
use Locale as PhpLocale;
3533

3634
use function array_merge;
3735
use function gettype;
@@ -60,7 +58,7 @@ public function __construct(
6058
?ConfigInterface $config = null,
6159
?MessageCollection $messages = null
6260
) {
63-
$this->config = $config ?? new Config(new Locale(PhpLocale::getDefault()));
61+
$this->config = $config ?? new Config();
6462
$this->messages = $messages ?? new MessageCollection();
6563
$this->messageFormat = new MessageFormat($this->config->getLocale());
6664
}

src/Intl/Locale.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
*/
4747
class Locale implements LocaleInterface
4848
{
49-
private const UNDEFINED_LOCALE = 'und';
49+
public const UNDEFINED_LOCALE = 'und';
5050

5151
/**
5252
* PHP's canonicalization (through ICU) converts calendar values to those
@@ -121,9 +121,9 @@ class Locale implements LocaleInterface
121121
/**
122122
* @throws InvalidArgumentException
123123
*/
124-
public function __construct(string $locale, ?LocaleOptions $options = null)
124+
public function __construct(?string $locale = null, ?LocaleOptions $options = null)
125125
{
126-
if (strtolower($locale) === self::UNDEFINED_LOCALE) {
126+
if ($locale === null || strtolower($locale) === self::UNDEFINED_LOCALE) {
127127
$locale = PhpLocale::getDefault();
128128
}
129129

tests/ConfigTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use FormatPHP\Config;
88
use FormatPHP\Extractor\IdInterpolator;
99
use FormatPHP\Intl\Locale;
10+
use Locale as PhpLocale;
1011

1112
class ConfigTest extends TestCase
1213
{
@@ -18,12 +19,24 @@ public function testInstanceConstruction(): void
1819
'em' => fn (string $text): string => '<em class="bar">' . $text . '</em>',
1920
'strong' => fn (string $text): string => '<strong class="foo">' . $text . '</strong>',
2021
];
22+
$idInterpolatorPattern = '[md5:contenthash:base64:16]';
2123

22-
$config = new Config($locale, $defaultLocale, $defaultRichTextElements);
24+
$config = new Config($locale, $defaultLocale, $defaultRichTextElements, $idInterpolatorPattern);
2325

2426
$this->assertSame($locale, $config->getLocale());
2527
$this->assertSame($defaultLocale, $config->getDefaultLocale());
2628
$this->assertSame($defaultRichTextElements, $config->getDefaultRichTextElements());
29+
$this->assertSame($idInterpolatorPattern, $config->getIdInterpolatorPattern());
30+
}
31+
32+
public function testConfigDefaults(): void
33+
{
34+
$systemLocale = new Locale(PhpLocale::getDefault());
35+
$config = new Config();
36+
37+
$this->assertSame($systemLocale->toString(), $config->getLocale()->toString());
38+
$this->assertNull($config->getDefaultLocale());
39+
$this->assertSame([], $config->getDefaultRichTextElements());
2740
$this->assertSame(IdInterpolator::DEFAULT_ID_INTERPOLATION_PATTERN, $config->getIdInterpolatorPattern());
2841
}
2942
}

tests/Intl/LocaleTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,19 @@ public function testLocaleWithOnlyLanguage(): void
9292
public function testLocaleWithUndefinedLocale(): void
9393
{
9494
$defaultLocale = new Locale(PhpLocale::getDefault());
95-
$undefinedLocale = new Locale('und');
95+
$undefinedLocale = new Locale(Locale::UNDEFINED_LOCALE);
9696

9797
$this->assertSame($defaultLocale->toString(), $undefinedLocale->toString());
9898
}
9999

100+
public function testLocaleWithNoLocale(): void
101+
{
102+
$defaultLocale = new Locale(PhpLocale::getDefault());
103+
$noLocale = new Locale();
104+
105+
$this->assertSame($defaultLocale->toString(), $noLocale->toString());
106+
}
107+
100108
public function testBaseNameWithMultipleVariants(): void
101109
{
102110
$locale = new Locale('en-US-variant1-variant2-variant3');

0 commit comments

Comments
 (0)