/** * @param string $locale * @return MessageFormatter */ public static function createDateValueFormatter(string $locale) { $valueTypeCheck = function ($value) { if (!is_int($value) && !$value instanceof DateTime && !$value instanceof IntlCalendar) { throw InvalidValueException::invalidValueType($value, ['integer', DateTime::class, IntlCalendar::class]); } }; $dateTypeSpecifier = ['date' => '{0,date}', 'date_short' => '{0,date,short}', 'date_medium' => '{0,date,medium}', 'date_long' => '{0,date,long}', 'date_full' => '{0,date,full}', 'time' => '{0,time}', 'time_short' => '{0,time,short}', 'time_medium' => '{0,time,medium}', 'time_long' => '{0,time,long}', 'time_full' => '{0,time,full}', 'date_year' => '{0,date,y}', 'date_month' => '{0,date,M}', 'date_month_name' => '{0,date,MMMM}', 'date_day' => '{0,date,d}', 'date_weekday' => '{0,date,EEEE}']; return new self($locale, $dateTypeSpecifier, $valueTypeCheck); }
/** * @param string $locale */ public function __construct(string $locale) { $this->locale = $locale; $this->formatFunctions = ['language' => function ($value) use($locale) { $language = Locale::getDisplayLanguage($value, $locale); if ($value === $language) { throw InvalidValueException::invalidLocale($value); } return $language; }, 'region' => function ($value) use($locale) { $region = Locale::getDisplayRegion($value, $locale); if ('' === $region) { throw InvalidValueException::invalidLocale($value); } return $region; }]; }
/** * @inheritdoc */ public function formatValue(string $typeSpecifier, $value) : string { $intlCalendar = $this->createIntlCalendar(); if ($value instanceof DateTimeInterface) { $intlCalendar->setTime($value->getTimestamp() * 1000); $value = $value->getTimezone(); } if ($value instanceof DateTimeZone) { $value = IntlTimeZone::fromDateTimeZone($value); } if (!$value instanceof IntlTimeZone) { throw InvalidValueException::invalidValueType($value, [DateTimeInterface::class, DateTimeZone::class, IntlTimeZone::class]); } $intlCalendar->setTimeZone($value); $inDaylight = $intlCalendar->inDaylightTime(); $timeZoneMetaData = [self::TYPE_SPECIFIER_ID => $value->getID(), self::TYPE_SPECIFIER_LONG_NAME => $value->getDisplayName($inDaylight, IntlTimeZone::DISPLAY_LONG, $this->locale), self::TYPE_SPECIFIER_SHORT_NAME => $value->getDisplayName($inDaylight, IntlTimeZone::DISPLAY_SHORT, $this->locale)]; return $timeZoneMetaData[$typeSpecifier]; }
/** * @expectedException \Budgegeria\IntlFormat\Exception\InvalidValueException * @expectedExceptionMessage "foo" is not a valid locale. */ public function testInvalidLocale() { throw InvalidValueException::invalidLocale('foo'); }