Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ jobs:
- "8.2"
- "8.3"
- "8.4"
- "8.5"
experimental:
- false
include:
- php-version: "8.5"
- php-version: "8.6"
composer-options: "--ignore-platform-reqs"
experimental: true

Expand Down
26 changes: 14 additions & 12 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<phpunit bootstrap="./vendor/autoload.php">
<testsuites>
<testsuite name="event-dispatcher">
<directory>./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="./vendor/autoload.php"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="event-dispatcher">
<directory>./tests</directory>
</testsuite>
</testsuites>
</phpunit>
12 changes: 11 additions & 1 deletion src/DateTimeFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Sunkan\Dictus;

final class DateTimeFormatter implements Formatter, MutableFormatter
final class DateTimeFormatter implements FormatterDateTime, MutableFormatter
{
public function __construct(
private string $format,
Expand All @@ -17,4 +17,14 @@ public function setFormat(string $format): void
{
$this->format = $format;
}

public function formatTimestamp(string $format, \DateTimeImmutable $timestamp): string
{
return $timestamp->format($format);
}

public function formatDate(string $format, \DateTimeInterface $date): string
{
return $date->format($format);
}
}
5 changes: 5 additions & 0 deletions src/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@
interface Formatter
{
public function format(\DateTimeInterface $date): string;

/**
* @deprecated use FormatterDateTime::formatDate
*/
public function formatTimestamp(string $format, \DateTimeImmutable $timestamp): string;
}
8 changes: 8 additions & 0 deletions src/FormatterDateTime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types=1);

namespace Sunkan\Dictus;

interface FormatterDateTime extends Formatter
{
public function formatDate(string $format, \DateTimeInterface $date): string;
}
12 changes: 9 additions & 3 deletions src/Locales/EnGb.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,25 @@ final class EnGb implements LocaleFormat
{
public function resolveFormat(string $format): ?string
{
return match($format) {
return match ($format) {
'LT' => 'H:i',
'LTS' => 'H:i:s',
'L' => 'd/m/Y',
'LL' => 'j F Y',
'LLL' => 'j F H:i',
'LLLL' => 'l, j F Y H:i',
'll' => 'j M Y',
'LLL' => 'j F Y [at] H:i',
'lll' => 'j M Y, H:i',
'LLLL' => 'l, j F Y [at] H:i',
'llll' => 'D, j M Y, H:i',
default => null,
};
}

public function formatChar(string $char, \DateTimeImmutable $dateTime): ?string
{
if ($char === 'M' && $dateTime->format('n') === '9') {
return 'Sept';
}
return null;
}
}
29 changes: 29 additions & 0 deletions src/Locales/EnUs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types=1);

namespace Sunkan\Dictus\Locales;

use Sunkan\Dictus\LocaleFormat;

final class EnUs implements LocaleFormat
{
public function resolveFormat(string $format): ?string
{
return match($format) {
'LT' => 'g:i A',
'LTS' => 'g:i:s A',
'L' => 'n/j/y',
'LL' => 'F j, Y',
'll' => 'M j, Y',
'LLL' => 'F j, Y [at] g:i A',
'lll' => 'M j, Y, g:i A',
'LLLL' => 'l, F j, Y [at] g:i A',
'llll' => 'D, M j, Y, g:i A',
default => null,
};
}

public function formatChar(string $char, \DateTimeImmutable $dateTime): ?string
{
return null;
}
}
45 changes: 24 additions & 21 deletions src/Locales/IsIs.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
final class IsIs implements LocaleFormat
{
private const DAYS_SHORT = [
1 => 'mán',
2 => 'þri',
3 => 'mið',
4 => 'fim',
5 => 'fös',
6 => 'lau',
7 => 'sun',
1 => 'mán.',
2 => 'þri.',
3 => 'mið.',
4 => 'fim.',
5 => 'fös.',
6 => 'lau.',
7 => 'sun.',
];

private const DAYS_LONG = [
Expand All @@ -27,18 +27,18 @@ final class IsIs implements LocaleFormat
];

private const MONTHS_SHORT = [
1 => 'jan',
2 => 'feb',
3 => 'mar',
4 => 'apr',
1 => 'jan.',
2 => 'feb.',
3 => 'mar.',
4 => 'apr.',
5 => 'maí',
6 => 'jún',
7 => 'júl',
8 => 'ágú',
9 => 'sep',
10 => 'okt',
11 => 'nóv',
12 => 'des',
6 => 'jún.',
7 => 'júl.',
8 => 'ágú.',
9 => 'sep.',
10 => 'okt.',
11 => 'nóv.',
12 => 'des.',
];

private const MONTHS_LONG = [
Expand Down Expand Up @@ -72,10 +72,13 @@ public function resolveFormat(string $format): ?string
return match($format) {
'LT' => 'H:i',
'LTS' => 'H:i:s',
'L' => 'd.m.Y',
'L' => 'j.n.Y',
'LL' => 'j. F Y',
'LLL' => 'j. F [kl.] H:i',
'LLLL' => 'l j. F Y [kl.] H:i',
'll' => 'j. M Y',
'LLL' => 'j. F Y [kl.] H:i',
'lll' => 'j. M Y, H:i',
'LLLL' => 'l, j. F Y [kl.] H:i',
'llll' => 'D j. M Y, H:i',
default => null,
};
}
Expand Down
71 changes: 37 additions & 34 deletions src/Locales/SvSe.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,53 @@
final class SvSe implements LocaleFormat
{
private const MONTHS_SHORT = [
1 => 'Jan',
2 => 'Feb',
3 => 'Mar',
4 => 'Apr',
5 => 'Maj',
6 => 'Jun',
7 => 'Jul',
8 => 'Aug',
9 => 'Sep',
10 => 'Okt',
11 => 'Nov',
12 => 'Dec',
1 => 'jan.',
2 => 'feb.',
3 => 'mars',
4 => 'apr.',
5 => 'maj',
6 => 'juni',
7 => 'juli',
8 => 'aug.',
9 => 'sep.',
10 => 'okt.',
11 => 'nov.',
12 => 'dec.',
];

private const MONTHS_LONG = [
1 => 'Januari',
2 => 'Februari',
3 => 'Mars',
4 => 'April',
5 => 'Maj',
6 => 'Juni',
7 => 'Juli',
8 => 'Augusti',
9 => 'September',
10 => 'Oktober',
11 => 'November',
12 => 'December',
1 => 'januari',
2 => 'februari',
3 => 'mars',
4 => 'april',
5 => 'maj',
6 => 'juni',
7 => 'juli',
8 => 'augusti',
9 => 'september',
10 => 'oktober',
11 => 'november',
12 => 'december',
];

private const DAYS_SHORT = [
1 => 'mån',
2 => 'tis',
3 => 'ons',
4 => 'tor',
4 => 'tors',
5 => 'fre',
6 => 'lör',
7 => 'sön',
];

private const DAYS_LONG = [
1 => 'Måndag',
2 => 'Tisdag',
3 => 'Onsdag',
4 => 'Torsdag',
5 => 'Fredag',
6 => 'Lördag',
7 => 'Söndag',
1 => 'måndag',
2 => 'tisdag',
3 => 'onsdag',
4 => 'torsdag',
5 => 'fredag',
6 => 'lördag',
7 => 'söndag',
];

public function formatChar(string $char, \DateTimeImmutable $dateTime): ?string
Expand All @@ -72,10 +72,13 @@ public function resolveFormat(string $format): ?string
return match($format) {
'LT' => 'H:i',
'LTS' => 'H:i:s',
'L' => 'd.m.Y',
'L' => 'Y-m-d',
'LL' => 'j F Y',
'LLL' => 'j F [kl.] H:i',
'll' => 'j M Y',
'LLL' => 'j F Y [kl.] H:i',
'lll' => 'j M Y H:i',
'LLLL' => 'l j F Y [kl.] H:i',
'llll' => 'D j M Y H:i',
default => null,
};
}
Expand Down
50 changes: 32 additions & 18 deletions src/LocalizedDateTimeFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@

namespace Sunkan\Dictus;

final class LocalizedDateTimeFormatter implements LocalizedFormatter, MutableFormatter
final class LocalizedDateTimeFormatter implements LocalizedFormatter, MutableFormatter, FormatterDateTime
{
private const LOCALIZED_SHORT_FORMATS = [
// Sorting this list correctly is important because of how we replace from it
'LTS',
'LT',
'LLLL',
'llll',
'LLL',
'lll',
'LL',
'll',
'L',
];
/** @var array<string, LocaleFormat> */
private static array $localFormats = [];

Expand Down Expand Up @@ -34,11 +46,27 @@ public function setLocale(string $locale): void

public function format(\DateTimeInterface $date): string
{
return $this->formatTimestamp($this->format, \DateTimeImmutable::createFromInterface($date), $this->locale);
return $this->formatDate($this->format, $date);
}

public function formatTimestamp(string $format, \DateTimeImmutable $timestamp, string $locale): string
public function formatTimestamp(string $format, \DateTimeImmutable $timestamp): string
{
return $this->formatDate($format, $timestamp);
}

public function formatDate(string $format, \DateTimeInterface $date): string
{
$timestamp = \DateTimeImmutable::createFromInterface($date);
foreach (self::LOCALIZED_SHORT_FORMATS as $localizedFormat) {
if (!str_contains($format, $localizedFormat)) {
continue;
}
$tmpFormat = $this->localeFormat->resolveFormat($localizedFormat);
if ($tmpFormat !== null) {
$format = str_replace($localizedFormat, $tmpFormat, $format);
}
}

$result = '';
$length = mb_strlen($format);
$inEscaped = false;
Expand Down Expand Up @@ -70,25 +98,11 @@ public function formatTimestamp(string $format, \DateTimeImmutable $timestamp, s
continue;
}
$localResult = $this->localeFormat->formatChar($char, $timestamp);
if ($localResult) {
if ($localResult) {
$result .= $localResult;
continue;
}

$input = mb_substr($format, $i);
if ($char === 'L' && preg_match('/^(LTS|LT|L{1,4})/', $input, $match)) {
$code = $match[0];
$newFormat = $this->localeFormat->resolveFormat($code);
if ($newFormat) {
$result .= $this->formatTimestamp($newFormat, $timestamp, $locale);
}
else {
$result .= $code;
}
$i += mb_strlen($code) - 1;
continue;
}

$result .= $timestamp->format($char);
}
return $result;
Expand Down
Loading