-
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
3 changed files
with
129 additions
and
76 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace ICanBoogie\CLDR; | ||
|
||
/** | ||
* @link https://www.unicode.org/reports/tr35/tr35-72/tr35-dates.html#Date_Format_Patterns | ||
*/ | ||
final class DateFormatPattern | ||
{ | ||
private const QUOTE = "'"; | ||
|
||
/** | ||
* @param string $pattern | ||
* A date format pattern; for example, "yyyy.MM.dd G 'at' HH:mm:ss zzz". | ||
* | ||
* @return array<string|array{ string, int }> | ||
* Where _value_ is either a literal or an array where `0` is a pattern character and `1` its length. | ||
*/ | ||
public static function tokenize(string $pattern): array | ||
{ | ||
static $cache = []; | ||
|
||
return $cache[$pattern] ??= self::do_tokenize($pattern); | ||
} | ||
|
||
/** | ||
* @param string $pattern | ||
* A date format pattern; for example, "yyyy.MM.dd G 'at' HH:mm:ss zzz". | ||
* | ||
* @return array<string|array{ string, int }> | ||
* Where _value_ is either a literal or an array where `0` is a pattern character and `1` its length. | ||
*/ | ||
private static function do_tokenize(string $pattern): array | ||
{ | ||
$tokens = []; | ||
$is_literal = false; | ||
$literal = ''; | ||
$z = mb_strlen($pattern); | ||
|
||
for ($i = 0; $i < $z; ++$i) { | ||
$c = mb_substr($pattern, $i, 1); | ||
|
||
if ($c === self::QUOTE) { | ||
// Two adjacent single vertical quotes (''), which represent a literal single quote, | ||
// either inside or outside a quoted text. | ||
if (mb_substr($pattern, $i + 1, 1) === self::QUOTE) { | ||
$i++; | ||
$literal .= self::QUOTE; | ||
} else { | ||
// Toggle literal | ||
$is_literal = !$is_literal; | ||
} | ||
} elseif ($is_literal) { | ||
$literal .= $c; | ||
} elseif (ctype_alpha($c)) { | ||
if ($literal) { | ||
$tokens[] = $literal; | ||
$literal = ''; | ||
} | ||
|
||
for ($j = $i + 1; $j < $z; ++$j) { | ||
$nc = mb_substr($pattern, $j, 1); | ||
if ($nc !== $c) { | ||
break; | ||
} | ||
} | ||
$tokens[] = [ $c, $j - $i ]; | ||
$i = $j - 1; // because +1 from the for loop | ||
} else { | ||
$literal .= $c; | ||
} | ||
} | ||
|
||
// If the pattern ends with literal (could also be a malformed quote) | ||
if ($literal) { | ||
$tokens[] = $literal; | ||
} | ||
|
||
return $tokens; | ||
} | ||
} |
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,39 @@ | ||
<?php | ||
|
||
namespace Test\ICanBoogie\CLDR; | ||
|
||
use ICanBoogie\CLDR\DateFormatPattern; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class DateTimePatternTest extends TestCase | ||
{ | ||
#[DataProvider("provide_tokenize")] | ||
public function test_tokenize(string $pattern, array $expected): void | ||
{ | ||
$actual = DateFormatPattern::tokenize($pattern); | ||
|
||
$this->assertEquals($expected, $actual); | ||
} | ||
|
||
public static function provide_tokenize(): array | ||
{ | ||
return [ | ||
|
||
[ 'G', [ [ 'G', 1 ] ] ], | ||
[ 'GG', [ [ 'G', 2 ] ] ], | ||
[ 'GGG', [ [ 'G', 3 ] ] ], | ||
[ 'GGGG', [ [ 'G', 4 ] ] ], | ||
[ 'GGGGG', [ [ 'G', 5 ] ] ], | ||
[ 'E d', [ [ 'E', 1 ], ' ', [ 'd', 1 ] ] ], | ||
[ 'E h:mm a', [ [ 'E', 1 ], ' ', [ 'h', 1 ], ':', [ 'm', 2 ], ' ', [ 'a', 1] ] ], | ||
[ 'E d/M/y', [ [ 'E', 1 ], ' ', [ 'd', 1 ], '/', [ 'M', 1 ], '/', [ 'y', 1] ] ], | ||
[ "E 'd/M/'y", [ [ 'E', 1 ], " d/M/", [ 'y', 1] ] ], | ||
[ "'week' W 'of' MMMM", [ "week ", [ 'W', 1 ], " of ", [ 'M', 4 ] ] ], | ||
[ "EEE, MMM d, ''yy", [ [ 'E', 3 ], ", ", [ 'M', 3 ], " ", [ 'd', 1 ], ", '", [ 'y', 2 ] ] ], | ||
[ "h:mm a", [ [ 'h', 1 ], ":", [ 'm', 2 ], " ", [ 'a', 1 ] ] ], | ||
[ "hh 'o''clock' a, zzzz", [ [ 'h', 2 ], " o'clock ", [ 'a', 1 ], ', ', [ 'z', 4 ] ] ], | ||
|
||
]; | ||
} | ||
} |