Skip to content

Commit 0fad4ce

Browse files
authored
feat: allow instantiation of FormatPHP without config or messages (#38)
1 parent 9b5efb0 commit 0fad4ce

4 files changed

Lines changed: 52 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1414
- Provide functionality for formatting dates and times through `Intl\DateTimeFormat`, as well as `FormatPHP::formatDate()` and `FormatPHP::formatTime()` convenience methods.
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.
17+
- Allow instantiation of `FormatPHP` without configuration or message collection instances; FormatPHP will use the system's default locale, in this case.
1718

1819
### Changed
1920

src/FormatPHP.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@
2727
use Exception as PhpException;
2828
use FormatPHP\Intl\DateTimeFormat;
2929
use FormatPHP\Intl\DateTimeFormatOptions;
30+
use FormatPHP\Intl\Locale;
3031
use FormatPHP\Intl\MessageFormat;
3132
use FormatPHP\Util\MessageCleaner;
3233
use FormatPHP\Util\MessageRetriever;
34+
use Locale as PhpLocale;
3335

3436
use function array_merge;
3537
use function gettype;
@@ -55,12 +57,12 @@ class FormatPHP implements FormatterInterface
5557
* @throws Exception\InvalidArgumentException
5658
*/
5759
public function __construct(
58-
ConfigInterface $config,
59-
MessageCollection $messages
60+
?ConfigInterface $config = null,
61+
?MessageCollection $messages = null
6062
) {
61-
$this->config = $config;
62-
$this->messages = $messages;
63-
$this->messageFormat = new MessageFormat($config->getLocale());
63+
$this->config = $config ?? new Config(new Locale(PhpLocale::getDefault()));
64+
$this->messages = $messages ?? new MessageCollection();
65+
$this->messageFormat = new MessageFormat($this->config->getLocale());
6466
}
6567

6668
/**

tests/Console/Command/ExtractCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function testExecuteWithValidation(): void
6868
$errorOutput,
6969
);
7070
$this->assertStringContainsString(
71-
'90 Descriptor argument must have at least one of id, defaultMessage, or',
71+
'91 Descriptor argument must have at least one of id, defaultMessage, or',
7272
$errorOutput,
7373
);
7474
$this->assertStringContainsString(

tests/FormatPHPTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use FormatPHP\Intl\Locale;
1313
use FormatPHP\Message;
1414
use FormatPHP\MessageCollection;
15+
use Locale as PhpLocale;
1516

1617
use function date;
1718

@@ -304,4 +305,46 @@ public function testFormatTimeUsesProvidedHourMinuteOptions(): void
304305
$this->assertSame('2-digit', $options->hour);
305306
$this->assertSame('2-digit', $options->minute);
306307
}
308+
309+
public function testConstructorWithoutConfiguration(): void
310+
{
311+
$date = 1635204852; // Mon, 25 Oct 2021 23:34:12 +0000
312+
313+
$systemLocale = PhpLocale::getDefault();
314+
$locale = new Locale($systemLocale);
315+
$config = new Config($locale);
316+
$formatphpForComparison = new FormatPHP($config);
317+
318+
$formatphpWithoutConfig = new FormatPHP();
319+
320+
$this->assertSame(
321+
$formatphpForComparison->formatDate($date),
322+
$formatphpWithoutConfig->formatDate($date),
323+
);
324+
}
325+
326+
public function testConstructorWithoutMessages(): void
327+
{
328+
$date = 1635204852; // Mon, 25 Oct 2021 23:34:12 +0000
329+
330+
$systemLocale = PhpLocale::getDefault();
331+
$locale = new Locale($systemLocale);
332+
$config = new Config($locale);
333+
$formatphpForComparison = new FormatPHP($config);
334+
335+
$expectedMessageResponse = $formatphpForComparison->formatMessage(
336+
['defaultMessage' => 'Today is {ts, date, ::yyyyMMdd}'],
337+
['ts' => $date],
338+
);
339+
340+
$formatphpWithoutMessages = new FormatPHP();
341+
342+
$this->assertSame(
343+
$expectedMessageResponse,
344+
$formatphpWithoutMessages->formatMessage(
345+
['id' => 'myMessage', 'defaultMessage' => 'Today is {ts, date, ::yyyyMMdd}'],
346+
['ts' => $date],
347+
),
348+
);
349+
}
307350
}

0 commit comments

Comments
 (0)