/** * Initializes the instance. * * @param string $locale * @param bool $caseSensitive */ public function __construct($locale = null, $caseSensitive = false) { $this->cache = array(); $this->locale = isset($locale) ? $locale : \Punic\Data::getDefaultLocale(); $this->caseSensitive = (bool) $caseSensitive; $this->collator = class_exists('\\Collator') ? new \Collator($this->locale) : null; $this->iconv = function_exists('iconv'); }
/** * Initializes the instance. * * @param string $locale * @param bool $caseSensitive */ public function __construct($locale = null, $caseSensitive = false) { $this->cache = array(); $this->locale = isset($locale) ? $locale : \Punic\Data::getDefaultLocale(); $this->caseSensitive = (bool) $caseSensitive; $this->collator = null; if (class_exists('\\Collator')) { try { $this->collator = new Collator($this->locale); } catch (PHPException $x) { } } $this->iconv = function_exists('iconv'); }
/** * Format a date and/or time * @param \DateTime $value The \DateTime instance for which you want the localized textual representation * @param string $format The ISO format that specify how to render the date/time * @param string $locale = '' The locale to use. If empty we'll use the default locale set in \Punic\Data * @return string Returns an empty string if $value is empty, the localized textual representation otherwise * @throws \Punic\Exception Throws an exception in case of problems * @link http://cldr.unicode.org/translation/date-time-patterns * @link http://cldr.unicode.org/translation/date-time * @link http://www.unicode.org/reports/tr35/tr35-dates.html#Date_Format_Patterns */ public static function format($value, $format, $locale = '') { static $decodeCache = array(); static $decoderFunctions = array('G' => 'decodeEra', 'y' => 'decodeYear', 'Y' => 'decodeYearWeekOfYear', 'u' => 'decodeYearExtended', 'U' => 'decodeYearCyclicName', 'r' => 'decodeYearRelatedGregorian', 'Q' => 'decodeQuarter', 'q' => 'decodeQuarterAlone', 'M' => 'decodeMonth', 'L' => 'decodeMonthAlone', 'w' => 'decodeWeekOfYear', 'W' => 'decodeWeekOfMonth', 'd' => 'decodeDayOfMonth', 'D' => 'decodeDayOfYear', 'F' => 'decodeWeekdayInMonth', 'g' => 'decodeModifiedGiulianDay', 'E' => 'decodeDayOfWeek', 'e' => 'decodeDayOfWeekLocal', 'c' => 'decodeDayOfWeekLocalAlone', 'a' => 'decodeDayperiod', 'h' => 'decodeHour12', 'H' => 'decodeHour24', 'K' => 'decodeHour12From0', 'k' => 'decodeHour24From1', 'm' => 'decodeMinute', 's' => 'decodeSecond', 'S' => 'decodeFranctionsOfSeconds', 'A' => 'decodeMsecInDay', 'z' => 'decodeTimezoneNoLocationSpecific', 'Z' => 'decodeTimezoneDelta', 'O' => 'decodeTimezoneShortGMT', 'v' => 'decodeTimezoneNoLocationGeneric', 'V' => 'decodeTimezoneID', 'X' => 'decodeTimezoneWithTimeZ', 'x' => 'decodeTimezoneWithTime'); $result = ''; if (!empty($value)) { if (!is_a($value, '\\DateTime')) { throw new Exception\BadArgumentType($value, '\\DateTime'); } $length = is_string($format) ? strlen($format) : 0; if ($length === 0) { throw new Exception\BadArgumentType($format, 'date/time ISO format'); } $cacheKey = empty($locale) ? \Punic\Data::getDefaultLocale() : $locale; if (!array_key_exists($cacheKey, $decodeCache)) { $decodeCache[$cacheKey] = array(); } if (!array_key_exists($format, $decodeCache[$cacheKey])) { $decoder = array(); $lengthM1 = $length - 1; $quoted = false; for ($index = 0; $index < $length; $index++) { $char = $format[$index]; if ($char === "'") { if ($quoted) { $quoted = false; } elseif ($index < $lengthM1 && $format[$index + 1] === "'") { $decoder[] = "'"; $index++; } else { $quoted = true; } } elseif ($quoted) { $decoder[] = $char; } else { $count = 1; for ($j = $index + 1; $j < $length && $format[$j] === $char; $j++) { $count++; $index++; } if (array_key_exists($char, $decoderFunctions)) { $decoder[] = array($decoderFunctions[$char], $count); } else { $decoder[] = str_repeat($char, $count); } } } $decodeCache[$cacheKey][$format] = $decoder; } else { $decoder = $decodeCache[$cacheKey][$format]; } foreach ($decoder as $chunk) { if (is_string($chunk)) { $result .= $chunk; } else { $functionName = $chunk[0]; $count = $chunk[1]; $result .= static::$functionName($value, $count, $locale); } } } return $result; }