Skip to content

Commit

Permalink
update to v0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
jiripudil committed Oct 30, 2023
1 parent 84f53ab commit 33aa44e
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 81 deletions.
6 changes: 0 additions & 6 deletions src/Formatter/IntlFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
use function sprintf;
use function str_split;

use const PHP_VERSION_ID;

/**
* Formats the value using the Intl extension.
*/
Expand Down Expand Up @@ -88,10 +86,6 @@ public static function ofPattern(string $locale, string $pattern): self
*/
public static function ofSkeleton(string $locale, string $skeleton): self
{
if (PHP_VERSION_ID < 80100) {
throw new DateTimeFormatException('IntlFormatter::ofSkeleton() is only available in PHP 8.1 and above.');
}

$generator = new IntlDatePatternGenerator($locale);
$pattern = $generator->getBestPattern($skeleton);

Expand Down
18 changes: 9 additions & 9 deletions src/LocalTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -651,15 +651,15 @@ public function jsonSerialize(): string
return $this->toISOString();
}

/**
* Formats this LocalTime using given DateTimeFormatter.
*/
public function format(DateTimeFormatter $formatter): string
{
$context = DateTimeFormatContext::ofLocalTime($this);

return $formatter->format($context);
}
/**
* Formats this LocalTime using given DateTimeFormatter.
*/
public function format(DateTimeFormatter $formatter): string
{
$context = DateTimeFormatContext::ofLocalTime($this);

return $formatter->format($context);
}

/**
* Returns the ISO 8601 representation of this time.
Expand Down
92 changes: 46 additions & 46 deletions tests/Formatter/DateTimeFormatContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,51 +20,51 @@ public function testOfLocalDate(): void
$localDate = LocalDate::of(2022, 6, 8);
$context = DateTimeFormatContext::ofLocalDate($localDate);

$this->assertSame('8', $context->getField(Field\DayOfMonth::NAME));
$this->assertSame('3', $context->getField(Field\DayOfWeek::NAME));
$this->assertSame('159', $context->getField(Field\DayOfYear::NAME));
$this->assertSame('23', $context->getField(Field\WeekOfYear::NAME));
$this->assertSame('6', $context->getField(Field\MonthOfYear::NAME));
$this->assertSame('2022', $context->getField(Field\Year::NAME));

$this->assertFalse($context->hasField(Field\HourOfDay::NAME));
$this->assertFalse($context->hasField(Field\TimeZoneRegion::NAME));
self::assertSame('8', $context->getField(Field\DayOfMonth::NAME));
self::assertSame('3', $context->getField(Field\DayOfWeek::NAME));
self::assertSame('159', $context->getField(Field\DayOfYear::NAME));
self::assertSame('23', $context->getField(Field\WeekOfYear::NAME));
self::assertSame('6', $context->getField(Field\MonthOfYear::NAME));
self::assertSame('2022', $context->getField(Field\Year::NAME));

self::assertFalse($context->hasField(Field\HourOfDay::NAME));
self::assertFalse($context->hasField(Field\TimeZoneRegion::NAME));
}

public function testOfLocalTime(): void
{
$localTime = LocalTime::of(13, 37, 42, 999999999);
$context = DateTimeFormatContext::ofLocalTime($localTime);

$this->assertSame('13', $context->getField(Field\HourOfDay::NAME));
$this->assertSame('37', $context->getField(Field\MinuteOfHour::NAME));
$this->assertSame('42', $context->getField(Field\SecondOfMinute::NAME));
$this->assertSame('999999999', $context->getField(Field\NanoOfSecond::NAME));
$this->assertSame('999999999', $context->getField(Field\FractionOfSecond::NAME));
self::assertSame('13', $context->getField(Field\HourOfDay::NAME));
self::assertSame('37', $context->getField(Field\MinuteOfHour::NAME));
self::assertSame('42', $context->getField(Field\SecondOfMinute::NAME));
self::assertSame('999999999', $context->getField(Field\NanoOfSecond::NAME));
self::assertSame('999999999', $context->getField(Field\FractionOfSecond::NAME));

$this->assertFalse($context->hasField(Field\DayOfMonth::NAME));
$this->assertFalse($context->hasField(Field\TimeZoneRegion::NAME));
self::assertFalse($context->hasField(Field\DayOfMonth::NAME));
self::assertFalse($context->hasField(Field\TimeZoneRegion::NAME));
}

public function testOfLocalDateTime(): void
{
$localDateTime = LocalDateTime::of(2022, 6, 8, 13, 37, 42, 999999999);
$context = DateTimeFormatContext::ofLocalDateTime($localDateTime);

$this->assertSame('8', $context->getField(Field\DayOfMonth::NAME));
$this->assertSame('3', $context->getField(Field\DayOfWeek::NAME));
$this->assertSame('159', $context->getField(Field\DayOfYear::NAME));
$this->assertSame('23', $context->getField(Field\WeekOfYear::NAME));
$this->assertSame('6', $context->getField(Field\MonthOfYear::NAME));
$this->assertSame('2022', $context->getField(Field\Year::NAME));
self::assertSame('8', $context->getField(Field\DayOfMonth::NAME));
self::assertSame('3', $context->getField(Field\DayOfWeek::NAME));
self::assertSame('159', $context->getField(Field\DayOfYear::NAME));
self::assertSame('23', $context->getField(Field\WeekOfYear::NAME));
self::assertSame('6', $context->getField(Field\MonthOfYear::NAME));
self::assertSame('2022', $context->getField(Field\Year::NAME));

$this->assertSame('13', $context->getField(Field\HourOfDay::NAME));
$this->assertSame('37', $context->getField(Field\MinuteOfHour::NAME));
$this->assertSame('42', $context->getField(Field\SecondOfMinute::NAME));
$this->assertSame('999999999', $context->getField(Field\NanoOfSecond::NAME));
$this->assertSame('999999999', $context->getField(Field\FractionOfSecond::NAME));
self::assertSame('13', $context->getField(Field\HourOfDay::NAME));
self::assertSame('37', $context->getField(Field\MinuteOfHour::NAME));
self::assertSame('42', $context->getField(Field\SecondOfMinute::NAME));
self::assertSame('999999999', $context->getField(Field\NanoOfSecond::NAME));
self::assertSame('999999999', $context->getField(Field\FractionOfSecond::NAME));

$this->assertFalse($context->hasField(Field\TimeZoneRegion::NAME));
self::assertFalse($context->hasField(Field\TimeZoneRegion::NAME));
}

public function testOfZonedDateTime(): void
Expand All @@ -73,23 +73,23 @@ public function testOfZonedDateTime(): void
$zonedDateTime = ZonedDateTime::of($localDateTime, TimeZoneRegion::of('Europe/Prague'));
$context = DateTimeFormatContext::ofZonedDateTime($zonedDateTime);

$this->assertSame('8', $context->getField(Field\DayOfMonth::NAME));
$this->assertSame('3', $context->getField(Field\DayOfWeek::NAME));
$this->assertSame('159', $context->getField(Field\DayOfYear::NAME));
$this->assertSame('23', $context->getField(Field\WeekOfYear::NAME));
$this->assertSame('6', $context->getField(Field\MonthOfYear::NAME));
$this->assertSame('2022', $context->getField(Field\Year::NAME));

$this->assertSame('13', $context->getField(Field\HourOfDay::NAME));
$this->assertSame('37', $context->getField(Field\MinuteOfHour::NAME));
$this->assertSame('42', $context->getField(Field\SecondOfMinute::NAME));
$this->assertSame('999999999', $context->getField(Field\NanoOfSecond::NAME));
$this->assertSame('999999999', $context->getField(Field\FractionOfSecond::NAME));

$this->assertSame('2', $context->getField(Field\TimeZoneOffsetHour::NAME));
$this->assertSame('0', $context->getField(Field\TimeZoneOffsetMinute::NAME));
$this->assertSame('+', $context->getField(Field\TimeZoneOffsetSign::NAME));
$this->assertSame('7200', $context->getField(Field\TimeZoneOffsetTotalSeconds::NAME));
$this->assertSame('Europe/Prague', $context->getField(Field\TimeZoneRegion::NAME));
self::assertSame('8', $context->getField(Field\DayOfMonth::NAME));
self::assertSame('3', $context->getField(Field\DayOfWeek::NAME));
self::assertSame('159', $context->getField(Field\DayOfYear::NAME));
self::assertSame('23', $context->getField(Field\WeekOfYear::NAME));
self::assertSame('6', $context->getField(Field\MonthOfYear::NAME));
self::assertSame('2022', $context->getField(Field\Year::NAME));

self::assertSame('13', $context->getField(Field\HourOfDay::NAME));
self::assertSame('37', $context->getField(Field\MinuteOfHour::NAME));
self::assertSame('42', $context->getField(Field\SecondOfMinute::NAME));
self::assertSame('999999999', $context->getField(Field\NanoOfSecond::NAME));
self::assertSame('999999999', $context->getField(Field\FractionOfSecond::NAME));

self::assertSame('2', $context->getField(Field\TimeZoneOffsetHour::NAME));
self::assertSame('0', $context->getField(Field\TimeZoneOffsetMinute::NAME));
self::assertSame('+', $context->getField(Field\TimeZoneOffsetSign::NAME));
self::assertSame('7200', $context->getField(Field\TimeZoneOffsetTotalSeconds::NAME));
self::assertSame('Europe/Prague', $context->getField(Field\TimeZoneRegion::NAME));
}
}
38 changes: 19 additions & 19 deletions tests/Formatter/IntlFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testOfDate($value, int $format, $expectedResult): void
}

$formatted = $value->format($formatter);
$this->assertSame($expectedResult, $formatted);
self::assertSame($expectedResult, $formatted);
}

public function provideTestOfDateData(): iterable
Expand Down Expand Up @@ -72,7 +72,7 @@ public function testOfDateTime($value, int $dateFormat, int $timeFormat, $expect
}

$formatted = $value->format($formatter);
$this->assertSame($expectedResult, $formatted);
self::assertSame($expectedResult, $formatted);
}

public function provideTestOfDateTimeData(): iterable
Expand All @@ -83,23 +83,23 @@ public function provideTestOfDateTimeData(): iterable
$dateTime = LocalDateTime::of(2022, 6, 8, 13, 37, 42, 999999999);
yield [$dateTime, IntlFormatter::FULL, IntlFormatter::FULL, new DateTimeFormatException('IntlFormatter with a long or full time part cannot be used to format Brick\DateTime\LocalDateTime.')];
yield [$dateTime, IntlFormatter::FULL, IntlFormatter::LONG, new DateTimeFormatException('IntlFormatter with a long or full time part cannot be used to format Brick\DateTime\LocalDateTime.')];
yield [$dateTime, IntlFormatter::FULL, IntlFormatter::MEDIUM, 'Wednesday, June 8, 2022 at 1:37:42 PM'];
yield [$dateTime, IntlFormatter::FULL, IntlFormatter::SHORT, 'Wednesday, June 8, 2022 at 1:37 PM'];
yield [$dateTime, IntlFormatter::LONG, IntlFormatter::SHORT, 'June 8, 2022 at 1:37 PM'];
yield [$dateTime, IntlFormatter::MEDIUM, IntlFormatter::MEDIUM, 'Jun 8, 2022, 1:37:42 PM'];
yield [$dateTime, IntlFormatter::FULL, IntlFormatter::MEDIUM, 'Wednesday, June 8, 2022 at 1:37:42PM'];
yield [$dateTime, IntlFormatter::FULL, IntlFormatter::SHORT, 'Wednesday, June 8, 2022 at 1:37PM'];
yield [$dateTime, IntlFormatter::LONG, IntlFormatter::SHORT, 'June 8, 2022 at 1:37PM'];
yield [$dateTime, IntlFormatter::MEDIUM, IntlFormatter::MEDIUM, 'Jun 8, 2022, 1:37:42PM'];
yield [$dateTime, IntlFormatter::SHORT, IntlFormatter::LONG, new DateTimeFormatException('IntlFormatter with a long or full time part cannot be used to format Brick\DateTime\LocalDateTime.')];

$time = LocalTime::of(13, 37, 42, 999999999);
yield [$time, IntlFormatter::FULL, IntlFormatter::FULL, new DateTimeFormatException('IntlFormatter with a date part cannot be used to format Brick\DateTime\LocalTime.')];

$zoned = ZonedDateTime::of($dateTime, TimeZoneRegion::of('Europe/Prague'));
yield [$zoned, IntlFormatter::FULL, IntlFormatter::FULL, 'Wednesday, June 8, 2022 at 1:37:42 PM Central European Summer Time'];
yield [$zoned, IntlFormatter::FULL, IntlFormatter::LONG, 'Wednesday, June 8, 2022 at 1:37:42 PM GMT+2'];
yield [$zoned, IntlFormatter::FULL, IntlFormatter::MEDIUM, 'Wednesday, June 8, 2022 at 1:37:42 PM'];
yield [$zoned, IntlFormatter::FULL, IntlFormatter::SHORT, 'Wednesday, June 8, 2022 at 1:37 PM'];
yield [$zoned, IntlFormatter::LONG, IntlFormatter::SHORT, 'June 8, 2022 at 1:37 PM'];
yield [$zoned, IntlFormatter::MEDIUM, IntlFormatter::MEDIUM, 'Jun 8, 2022, 1:37:42 PM'];
yield [$zoned, IntlFormatter::SHORT, IntlFormatter::LONG, '6/8/22, 1:37:42 PM GMT+2'];
yield [$zoned, IntlFormatter::FULL, IntlFormatter::FULL, 'Wednesday, June 8, 2022 at 1:37:42PM Central European Summer Time'];
yield [$zoned, IntlFormatter::FULL, IntlFormatter::LONG, 'Wednesday, June 8, 2022 at 1:37:42PM GMT+2'];
yield [$zoned, IntlFormatter::FULL, IntlFormatter::MEDIUM, 'Wednesday, June 8, 2022 at 1:37:42PM'];
yield [$zoned, IntlFormatter::FULL, IntlFormatter::SHORT, 'Wednesday, June 8, 2022 at 1:37PM'];
yield [$zoned, IntlFormatter::LONG, IntlFormatter::SHORT, 'June 8, 2022 at 1:37PM'];
yield [$zoned, IntlFormatter::MEDIUM, IntlFormatter::MEDIUM, 'Jun 8, 2022, 1:37:42PM'];
yield [$zoned, IntlFormatter::SHORT, IntlFormatter::LONG, '6/8/22, 1:37:42PM GMT+2'];
}

/**
Expand All @@ -118,7 +118,7 @@ public function testOfTime($value, int $format, $expectedResult): void
}

$formatted = $value->format($formatter);
$this->assertSame($expectedResult, $formatted);
self::assertSame($expectedResult, $formatted);
}

public function provideTestOfTimeData(): iterable
Expand All @@ -132,11 +132,11 @@ public function provideTestOfTimeData(): iterable
$time = LocalTime::of(13, 37, 42, 999999999);
yield [$time, IntlFormatter::FULL, new DateTimeFormatException('IntlFormatter with a long or full time part cannot be used to format Brick\DateTime\LocalTime.')];
yield [$time, IntlFormatter::LONG, new DateTimeFormatException('IntlFormatter with a long or full time part cannot be used to format Brick\DateTime\LocalTime.')];
yield [$time, IntlFormatter::MEDIUM, '1:37:42 PM'];
yield [$time, IntlFormatter::SHORT, '1:37 PM'];
yield [$time, IntlFormatter::MEDIUM, '1:37:42PM'];
yield [$time, IntlFormatter::SHORT, '1:37PM'];

$zoned = ZonedDateTime::of($dateTime, TimeZoneRegion::of('Europe/Prague'));
yield [$zoned, IntlFormatter::FULL, '1:37:42 PM Central European Summer Time'];
yield [$zoned, IntlFormatter::FULL, '1:37:42PM Central European Summer Time'];
}

/**
Expand All @@ -155,7 +155,7 @@ public function testOfPattern($value, string $pattern, $expectedResult): void
}

$formatted = $value->format($formatter);
$this->assertSame($expectedResult, $formatted);
self::assertSame($expectedResult, $formatted);
}

public function provideTestOfPatternData(): iterable
Expand Down Expand Up @@ -204,7 +204,7 @@ public function testOfSkeleton($value, string $skeleton, $expectedResult): void
}

$formatted = $value->format($formatter);
$this->assertSame($expectedResult, $formatted);
self::assertSame($expectedResult, $formatted);
}

public function provideTestOfSkeletonData(): iterable
Expand Down
2 changes: 1 addition & 1 deletion tests/Formatter/NativeFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testFormat($value, string $format, $expectedResult): void
}

$formatted = $value->format($formatter);
$this->assertSame($expectedResult, $formatted);
self::assertSame($expectedResult, $formatted);
}

public function provideTestFormatData(): iterable
Expand Down

0 comments on commit 33aa44e

Please sign in to comment.