From 4f5b44bd3bd490309eb2ba7b44df4769816ba729 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Sat, 3 Aug 2013 19:26:54 +0200 Subject: twig implementation --- .../DateFormatter/DateFormat/AmPmTransformer.php | 46 ++ .../DateFormat/DayOfWeekTransformer.php | 59 ++ .../DateFormat/DayOfYearTransformer.php | 46 ++ .../DateFormatter/DateFormat/DayTransformer.php | 46 ++ .../DateFormatter/DateFormat/FullTransformer.php | 356 ++++++++++++ .../DateFormat/Hour1200Transformer.php | 62 ++ .../DateFormat/Hour1201Transformer.php | 62 ++ .../DateFormat/Hour2400Transformer.php | 61 ++ .../DateFormat/Hour2401Transformer.php | 64 +++ .../DateFormatter/DateFormat/HourTransformer.php | 30 + .../DateFormatter/DateFormat/MinuteTransformer.php | 48 ++ .../DateFormatter/DateFormat/MonthTransformer.php | 143 +++++ .../DateFormat/QuarterTransformer.php | 64 +++ .../DateFormatter/DateFormat/SecondTransformer.php | 48 ++ .../DateFormat/TimeZoneTransformer.php | 99 ++++ .../Intl/DateFormatter/DateFormat/Transformer.php | 64 +++ .../DateFormatter/DateFormat/YearTransformer.php | 50 ++ .../Intl/DateFormatter/IntlDateFormatter.php | 631 +++++++++++++++++++++ 18 files changed, 1979 insertions(+) create mode 100644 vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/AmPmTransformer.php create mode 100644 vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/DayOfWeekTransformer.php create mode 100644 vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/DayOfYearTransformer.php create mode 100644 vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/DayTransformer.php create mode 100644 vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/FullTransformer.php create mode 100644 vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/Hour1200Transformer.php create mode 100644 vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/Hour1201Transformer.php create mode 100644 vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/Hour2400Transformer.php create mode 100644 vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/Hour2401Transformer.php create mode 100644 vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/HourTransformer.php create mode 100644 vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/MinuteTransformer.php create mode 100644 vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/MonthTransformer.php create mode 100644 vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/QuarterTransformer.php create mode 100644 vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/SecondTransformer.php create mode 100644 vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/TimeZoneTransformer.php create mode 100644 vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/Transformer.php create mode 100644 vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/YearTransformer.php create mode 100644 vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/IntlDateFormatter.php (limited to 'vendor/symfony/intl/Symfony/Component/Intl/DateFormatter') diff --git a/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/AmPmTransformer.php b/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/AmPmTransformer.php new file mode 100644 index 00000000..1a9601d8 --- /dev/null +++ b/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/AmPmTransformer.php @@ -0,0 +1,46 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Intl\DateFormatter\DateFormat; + +/** + * Parser and formatter for AM/PM markers format + * + * @author Igor Wiedler + */ +class AmPmTransformer extends Transformer +{ + /** + * {@inheritDoc} + */ + public function format(\DateTime $dateTime, $length) + { + return $dateTime->format('A'); + } + + /** + * {@inheritDoc} + */ + public function getReverseMatchingRegExp($length) + { + return 'AM|PM'; + } + + /** + * {@inheritDoc} + */ + public function extractDateOptions($matched, $length) + { + return array( + 'marker' => $matched + ); + } +} diff --git a/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/DayOfWeekTransformer.php b/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/DayOfWeekTransformer.php new file mode 100644 index 00000000..ee53a4e6 --- /dev/null +++ b/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/DayOfWeekTransformer.php @@ -0,0 +1,59 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Intl\DateFormatter\DateFormat; + +/** + * Parser and formatter for day of week format + * + * @author Igor Wiedler + */ +class DayOfWeekTransformer extends Transformer +{ + /** + * {@inheritDoc} + */ + public function format(\DateTime $dateTime, $length) + { + $dayOfWeek = $dateTime->format('l'); + switch ($length) { + case 4: + return $dayOfWeek; + case 5: + return $dayOfWeek[0]; + default: + return substr($dayOfWeek, 0, 3); + } + } + + /** + * {@inheritDoc} + */ + public function getReverseMatchingRegExp($length) + { + switch ($length) { + case 4: + return 'Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday'; + case 5: + return '[MTWFS]'; + default: + return 'Mon|Tue|Wed|Thu|Fri|Sat|Sun'; + } + } + + /** + * {@inheritDoc} + */ + public function extractDateOptions($matched, $length) + { + return array(); + } +} diff --git a/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/DayOfYearTransformer.php b/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/DayOfYearTransformer.php new file mode 100644 index 00000000..2c33888c --- /dev/null +++ b/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/DayOfYearTransformer.php @@ -0,0 +1,46 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Intl\DateFormatter\DateFormat; + +/** + * Parser and formatter for day of year format + * + * @author Igor Wiedler + */ +class DayOfYearTransformer extends Transformer +{ + /** + * {@inheritDoc} + */ + public function format(\DateTime $dateTime, $length) + { + $dayOfYear = $dateTime->format('z') + 1; + + return $this->padLeft($dayOfYear, $length); + } + + /** + * {@inheritDoc} + */ + public function getReverseMatchingRegExp($length) + { + return '\d{'.$length.'}'; + } + + /** + * {@inheritDoc} + */ + public function extractDateOptions($matched, $length) + { + return array(); + } +} diff --git a/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/DayTransformer.php b/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/DayTransformer.php new file mode 100644 index 00000000..19d30e74 --- /dev/null +++ b/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/DayTransformer.php @@ -0,0 +1,46 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Intl\DateFormatter\DateFormat; + +/** + * Parser and formatter for day format + * + * @author Igor Wiedler + */ +class DayTransformer extends Transformer +{ + /** + * {@inheritDoc} + */ + public function format(\DateTime $dateTime, $length) + { + return $this->padLeft($dateTime->format('j'), $length); + } + + /** + * {@inheritDoc} + */ + public function getReverseMatchingRegExp($length) + { + return 1 === $length ? '\d{1,2}' : '\d{'.$length.'}'; + } + + /** + * {@inheritDoc} + */ + public function extractDateOptions($matched, $length) + { + return array( + 'day' => (int) $matched, + ); + } +} diff --git a/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/FullTransformer.php b/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/FullTransformer.php new file mode 100644 index 00000000..b89db363 --- /dev/null +++ b/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/FullTransformer.php @@ -0,0 +1,356 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Intl\DateFormatter\DateFormat; + +use Symfony\Component\Intl\Exception\NotImplementedException; +use Symfony\Component\Intl\Globals\IntlGlobals; +use Symfony\Component\Intl\DateFormatter\DateFormat\MonthTransformer; + +/** + * Parser and formatter for date formats + * + * @author Igor Wiedler + */ +class FullTransformer +{ + private $quoteMatch = "'(?:[^']+|'')*'"; + private $implementedChars = 'MLydQqhDEaHkKmsz'; + private $notImplementedChars = 'GYuwWFgecSAZvVW'; + private $regExp; + + /** + * @var Transformer[] + */ + private $transformers; + + private $pattern; + private $timezone; + + /** + * Constructor + * + * @param string $pattern The pattern to be used to format and/or parse values + * @param string $timezone The timezone to perform the date/time calculations + */ + public function __construct($pattern, $timezone) + { + $this->pattern = $pattern; + $this->timezone = $timezone; + + $implementedCharsMatch = $this->buildCharsMatch($this->implementedChars); + $notImplementedCharsMatch = $this->buildCharsMatch($this->notImplementedChars); + $this->regExp = "/($this->quoteMatch|$implementedCharsMatch|$notImplementedCharsMatch)/"; + + $this->transformers = array( + 'M' => new MonthTransformer(), + 'L' => new MonthTransformer(), + 'y' => new YearTransformer(), + 'd' => new DayTransformer(), + 'q' => new QuarterTransformer(), + 'Q' => new QuarterTransformer(), + 'h' => new Hour1201Transformer(), + 'D' => new DayOfYearTransformer(), + 'E' => new DayOfWeekTransformer(), + 'a' => new AmPmTransformer(), + 'H' => new Hour2400Transformer(), + 'K' => new Hour1200Transformer(), + 'k' => new Hour2401Transformer(), + 'm' => new MinuteTransformer(), + 's' => new SecondTransformer(), + 'z' => new TimeZoneTransformer(), + ); + } + + /** + * Return the array of Transformer objects + * + * @return Transformer[] Associative array of Transformer objects (format char => Transformer) + */ + public function getTransformers() + { + return $this->transformers; + } + + /** + * Format a DateTime using ICU dateformat pattern + * + * @param \DateTime $dateTime A DateTime object to be used to generate the formatted value + * + * @return string The formatted value + */ + public function format(\DateTime $dateTime) + { + $that = $this; + + $formatted = preg_replace_callback($this->regExp, function($matches) use ($that, $dateTime) { + return $that->formatReplace($matches[0], $dateTime); + }, $this->pattern); + + return $formatted; + } + + /** + * Return the formatted ICU value for the matched date characters + * + * @param string $dateChars The date characters to be replaced with a formatted ICU value + * @param DateTime $dateTime A DateTime object to be used to generate the formatted value + * + * @return string The formatted value + * + * @throws NotImplementedException When it encounters a not implemented date character + */ + public function formatReplace($dateChars, $dateTime) + { + $length = strlen($dateChars); + + if ($this->isQuoteMatch($dateChars)) { + return $this->replaceQuoteMatch($dateChars); + } + + if (isset($this->transformers[$dateChars[0]])) { + $transformer = $this->transformers[$dateChars[0]]; + + return $transformer->format($dateTime, $length); + } + + // handle unimplemented characters + if (false !== strpos($this->notImplementedChars, $dateChars[0])) { + throw new NotImplementedException(sprintf("Unimplemented date character '%s' in format '%s'", $dateChars[0], $this->pattern)); + } + } + + /** + * Parse a pattern based string to a timestamp value + * + * @param \DateTime $dateTime A configured DateTime object to use to perform the date calculation + * @param string $value String to convert to a time value + * + * @return int The corresponding Unix timestamp + * + * @throws \InvalidArgumentException When the value can not be matched with pattern + */ + public function parse(\DateTime $dateTime, $value) + { + $reverseMatchingRegExp = $this->getReverseMatchingRegExp($this->pattern); + $reverseMatchingRegExp = '/^'.$reverseMatchingRegExp.'$/'; + + $options = array(); + + if (preg_match($reverseMatchingRegExp, $value, $matches)) { + $matches = $this->normalizeArray($matches); + + foreach ($this->transformers as $char => $transformer) { + if (isset($matches[$char])) { + $length = strlen($matches[$char]['pattern']); + $options = array_merge($options, $transformer->extractDateOptions($matches[$char]['value'], $length)); + } + } + + // reset error code and message + IntlGlobals::setError(IntlGlobals::U_ZERO_ERROR); + + return $this->calculateUnixTimestamp($dateTime, $options); + } + + // behave like the intl extension + IntlGlobals::setError(IntlGlobals::U_PARSE_ERROR, 'Date parsing failed'); + + return false; + } + + /** + * Retrieve a regular expression to match with a formatted value. + * + * @param string $pattern The pattern to create the reverse matching regular expression + * + * @return string The reverse matching regular expression with named captures being formed by the + * transformer index in the $transformer array + */ + public function getReverseMatchingRegExp($pattern) + { + $that = $this; + + $escapedPattern = preg_quote($pattern, '/'); + + // ICU 4.8 recognizes slash ("/") in a value to be parsed as a dash ("-") and vice-versa + // when parsing a date/time value + $escapedPattern = preg_replace('/\\\[\-|\/]/', '[\/\-]', $escapedPattern); + + $reverseMatchingRegExp = preg_replace_callback($this->regExp, function($matches) use ($that) { + $length = strlen($matches[0]); + $transformerIndex = $matches[0][0]; + + $dateChars = $matches[0]; + if ($that->isQuoteMatch($dateChars)) { + return $that->replaceQuoteMatch($dateChars); + } + + $transformers = $that->getTransformers(); + if (isset($transformers[$transformerIndex])) { + $transformer = $transformers[$transformerIndex]; + $captureName = str_repeat($transformerIndex, $length); + + return "(?P<$captureName>".$transformer->getReverseMatchingRegExp($length).')'; + } + }, $escapedPattern); + + return $reverseMatchingRegExp; + } + + /** + * Check if the first char of a string is a single quote + * + * @param string $quoteMatch The string to check + * + * @return Boolean true if matches, false otherwise + */ + public function isQuoteMatch($quoteMatch) + { + return ("'" === $quoteMatch[0]); + } + + /** + * Replaces single quotes at the start or end of a string with two single quotes + * + * @param string $quoteMatch The string to replace the quotes + * + * @return string A string with the single quotes replaced + */ + public function replaceQuoteMatch($quoteMatch) + { + if (preg_match("/^'+$/", $quoteMatch)) { + return str_replace("''", "'", $quoteMatch); + } + + return str_replace("''", "'", substr($quoteMatch, 1, -1)); + } + + /** + * Builds a chars match regular expression + * + * @param string $specialChars A string of chars to build the regular expression + * + * @return string The chars match regular expression + */ + protected function buildCharsMatch($specialChars) + { + $specialCharsArray = str_split($specialChars); + + $specialCharsMatch = implode('|', array_map(function($char) { + return $char.'+'; + }, $specialCharsArray)); + + return $specialCharsMatch; + } + + /** + * Normalize a preg_replace match array, removing the numeric keys and returning an associative array + * with the value and pattern values for the matched Transformer + * + * @param array $data + * + * @return array + */ + protected function normalizeArray(array $data) + { + $ret = array(); + + foreach ($data as $key => $value) { + if (!is_string($key)) { + continue; + } + + $ret[$key[0]] = array( + 'value' => $value, + 'pattern' => $key + ); + } + + return $ret; + } + + /** + * Calculates the Unix timestamp based on the matched values by the reverse matching regular + * expression of parse() + * + * @param \DateTime $dateTime The DateTime object to be used to calculate the timestamp + * @param array $options An array with the matched values to be used to calculate the timestamp + * + * @return Boolean|int The calculated timestamp or false if matched date is invalid + */ + protected function calculateUnixTimestamp(\DateTime $dateTime, array $options) + { + $options = $this->getDefaultValueForOptions($options); + + $year = $options['year']; + $month = $options['month']; + $day = $options['day']; + $hour = $options['hour']; + $hourInstance = $options['hourInstance']; + $minute = $options['minute']; + $second = $options['second']; + $marker = $options['marker']; + $timezone = $options['timezone']; + + // If month is false, return immediately (intl behavior) + if (false === $month) { + IntlGlobals::setError(IntlGlobals::U_PARSE_ERROR, 'Date parsing failed'); + + return false; + } + + // Normalize hour + if ($hourInstance instanceof HourTransformer) { + $hour = $hourInstance->normalizeHour($hour, $marker); + } + + // Set the timezone if different from the default one + if (null !== $timezone && $timezone !== $this->timezone) { + $dateTime->setTimezone(new \DateTimeZone($timezone)); + } + + // Normalize yy year + preg_match_all($this->regExp, $this->pattern, $matches); + if (in_array('yy', $matches[0])) { + $dateTime->setTimestamp(time()); + $year = $year > $dateTime->format('y') + 20 ? 1900 + $year : 2000 + $year; + } + + $dateTime->setDate($year, $month, $day); + $dateTime->setTime($hour, $minute, $second); + + return $dateTime->getTimestamp(); + } + + /** + * Add sensible default values for missing items in the extracted date/time options array. The values + * are base in the beginning of the Unix era + * + * @param array $options + * + * @return array + */ + private function getDefaultValueForOptions(array $options) + { + return array( + 'year' => isset($options['year']) ? $options['year'] : 1970, + 'month' => isset($options['month']) ? $options['month'] : 1, + 'day' => isset($options['day']) ? $options['day'] : 1, + 'hour' => isset($options['hour']) ? $options['hour'] : 0, + 'hourInstance' => isset($options['hourInstance']) ? $options['hourInstance'] : null, + 'minute' => isset($options['minute']) ? $options['minute'] : 0, + 'second' => isset($options['second']) ? $options['second'] : 0, + 'marker' => isset($options['marker']) ? $options['marker'] : null, + 'timezone' => isset($options['timezone']) ? $options['timezone'] : null, + ); + } +} diff --git a/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/Hour1200Transformer.php b/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/Hour1200Transformer.php new file mode 100644 index 00000000..8c8f5ef4 --- /dev/null +++ b/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/Hour1200Transformer.php @@ -0,0 +1,62 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Intl\DateFormatter\DateFormat; + +/** + * Parser and formatter for 12 hour format (0-11) + * + * @author Igor Wiedler + */ +class Hour1200Transformer extends HourTransformer +{ + /** + * {@inheritDoc} + */ + public function format(\DateTime $dateTime, $length) + { + $hourOfDay = $dateTime->format('g'); + $hourOfDay = '12' == $hourOfDay ? '0' : $hourOfDay; + + return $this->padLeft($hourOfDay, $length); + } + + /** + * {@inheritDoc} + */ + public function normalizeHour($hour, $marker = null) + { + if ('PM' === $marker) { + $hour += 12; + } + + return $hour; + } + + /** + * {@inheritDoc} + */ + public function getReverseMatchingRegExp($length) + { + return '\d{1,2}'; + } + + /** + * {@inheritDoc} + */ + public function extractDateOptions($matched, $length) + { + return array( + 'hour' => (int) $matched, + 'hourInstance' => $this + ); + } +} diff --git a/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/Hour1201Transformer.php b/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/Hour1201Transformer.php new file mode 100644 index 00000000..a8c43702 --- /dev/null +++ b/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/Hour1201Transformer.php @@ -0,0 +1,62 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Intl\DateFormatter\DateFormat; + +/** + * Parser and formatter for 12 hour format (1-12) + * + * @author Igor Wiedler + */ +class Hour1201Transformer extends HourTransformer +{ + /** + * {@inheritDoc} + */ + public function format(\DateTime $dateTime, $length) + { + return $this->padLeft($dateTime->format('g'), $length); + } + + /** + * {@inheritDoc} + */ + public function normalizeHour($hour, $marker = null) + { + if ('PM' !== $marker && 12 === $hour) { + $hour = 0; + } elseif ('PM' === $marker && 12 !== $hour) { + // If PM and hour is not 12 (1-12), sum 12 hour + $hour += 12; + } + + return $hour; + } + + /** + * {@inheritDoc} + */ + public function getReverseMatchingRegExp($length) + { + return '\d{1,2}'; + } + + /** + * {@inheritDoc} + */ + public function extractDateOptions($matched, $length) + { + return array( + 'hour' => (int) $matched, + 'hourInstance' => $this + ); + } +} diff --git a/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/Hour2400Transformer.php b/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/Hour2400Transformer.php new file mode 100644 index 00000000..8f22da13 --- /dev/null +++ b/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/Hour2400Transformer.php @@ -0,0 +1,61 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Intl\DateFormatter\DateFormat; + +/** + * Parser and formatter for 24 hour format (0-23) + * + * @author Igor Wiedler + */ +class Hour2400Transformer extends HourTransformer +{ + /** + * {@inheritDoc} + */ + public function format(\DateTime $dateTime, $length) + { + return $this->padLeft($dateTime->format('G'), $length); + } + + /** + * {@inheritDoc} + */ + public function normalizeHour($hour, $marker = null) + { + if ('AM' == $marker) { + $hour = 0; + } elseif ('PM' == $marker) { + $hour = 12; + } + + return $hour; + } + + /** + * {@inheritDoc} + */ + public function getReverseMatchingRegExp($length) + { + return '\d{1,2}'; + } + + /** + * {@inheritDoc} + */ + public function extractDateOptions($matched, $length) + { + return array( + 'hour' => (int) $matched, + 'hourInstance' => $this + ); + } +} diff --git a/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/Hour2401Transformer.php b/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/Hour2401Transformer.php new file mode 100644 index 00000000..b0f486b9 --- /dev/null +++ b/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/Hour2401Transformer.php @@ -0,0 +1,64 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Intl\DateFormatter\DateFormat; + +/** + * Parser and formatter for 24 hour format (1-24) + * + * @author Igor Wiedler + */ +class Hour2401Transformer extends HourTransformer +{ + /** + * {@inheritDoc} + */ + public function format(\DateTime $dateTime, $length) + { + $hourOfDay = $dateTime->format('G'); + $hourOfDay = ('0' == $hourOfDay) ? '24' : $hourOfDay; + + return $this->padLeft($hourOfDay, $length); + } + + /** + * {@inheritDoc} + */ + public function normalizeHour($hour, $marker = null) + { + if ((null === $marker && 24 === $hour) || 'AM' == $marker) { + $hour = 0; + } elseif ('PM' == $marker) { + $hour = 12; + } + + return $hour; + } + + /** + * {@inheritDoc} + */ + public function getReverseMatchingRegExp($length) + { + return '\d{1,2}'; + } + + /** + * {@inheritDoc} + */ + public function extractDateOptions($matched, $length) + { + return array( + 'hour' => (int) $matched, + 'hourInstance' => $this + ); + } +} diff --git a/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/HourTransformer.php b/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/HourTransformer.php new file mode 100644 index 00000000..51097d92 --- /dev/null +++ b/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/HourTransformer.php @@ -0,0 +1,30 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Intl\DateFormatter\DateFormat; + +/** + * Base class for hour transformers + * + * @author Eriksen Costa + */ +abstract class HourTransformer extends Transformer +{ + /** + * Returns a normalized hour value suitable for the hour transformer type + * + * @param int $hour The hour value + * @param string $marker An optional AM/PM marker + * + * @return int The normalized hour value + */ + abstract public function normalizeHour($hour, $marker = null); +} diff --git a/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/MinuteTransformer.php b/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/MinuteTransformer.php new file mode 100644 index 00000000..b48de292 --- /dev/null +++ b/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/MinuteTransformer.php @@ -0,0 +1,48 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Intl\DateFormatter\DateFormat; + +/** + * Parser and formatter for minute format + * + * @author Igor Wiedler + */ +class MinuteTransformer extends Transformer +{ + /** + * {@inheritDoc} + */ + public function format(\DateTime $dateTime, $length) + { + $minuteOfHour = (int) $dateTime->format('i'); + + return $this->padLeft($minuteOfHour, $length); + } + + /** + * {@inheritDoc} + */ + public function getReverseMatchingRegExp($length) + { + return 1 === $length ? '\d{1,2}' : '\d{'.$length.'}'; + } + + /** + * {@inheritDoc} + */ + public function extractDateOptions($matched, $length) + { + return array( + 'minute' => (int) $matched, + ); + } +} diff --git a/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/MonthTransformer.php b/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/MonthTransformer.php new file mode 100644 index 00000000..30c15af7 --- /dev/null +++ b/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/MonthTransformer.php @@ -0,0 +1,143 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Intl\DateFormatter\DateFormat; + +/** + * Parser and formatter for month format + * + * @author Igor Wiedler + */ +class MonthTransformer extends Transformer +{ + /** + * @var array + */ + protected static $months = array( + 'January', + 'February', + 'March', + 'April', + 'May', + 'June', + 'July', + 'August', + 'September', + 'October', + 'November', + 'December' + ); + + /** + * Short months names (first 3 letters) + * @var array + */ + protected static $shortMonths = array(); + + /** + * Flipped $months array, $name => $index + * @var array + */ + protected static $flippedMonths = array(); + + /** + * Flipped $shortMonths array, $name => $index + * @var array + */ + protected static $flippedShortMonths = array(); + + /** + * Constructor + */ + public function __construct() + { + if (0 === count(self::$shortMonths)) { + self::$shortMonths = array_map(function($month) { + return substr($month, 0, 3); + }, self::$months); + + self::$flippedMonths = array_flip(self::$months); + self::$flippedShortMonths = array_flip(self::$shortMonths); + } + } + + /** + * {@inheritDoc} + */ + public function format(\DateTime $dateTime, $length) + { + $matchLengthMap = array( + 1 => 'n', + 2 => 'm', + 3 => 'M', + 4 => 'F', + ); + + if (isset($matchLengthMap[$length])) { + return $dateTime->format($matchLengthMap[$length]); + } + + if (5 === $length) { + return substr($dateTime->format('M'), 0, 1); + } + + return $this->padLeft($dateTime->format('m'), $length); + } + + /** + * {@inheritDoc} + */ + public function getReverseMatchingRegExp($length) + { + switch ($length) { + case 1: + $regExp = '\d{1,2}'; + break; + case 3: + $regExp = implode('|', self::$shortMonths); + break; + case 4: + $regExp = implode('|', self::$months); + break; + case 5: + $regExp = '[JFMASOND]'; + break; + default: + $regExp = '\d{'.$length.'}'; + break; + } + + return $regExp; + } + + /** + * {@inheritDoc} + */ + public function extractDateOptions($matched, $length) + { + if (!is_numeric($matched)) { + if (3 === $length) { + $matched = self::$flippedShortMonths[$matched] + 1; + } elseif (4 === $length) { + $matched = self::$flippedMonths[$matched] + 1; + } elseif (5 === $length) { + // IntlDateFormatter::parse() always returns false for MMMMM or LLLLL + $matched = false; + } + } else { + $matched = (int) $matched; + } + + return array( + 'month' => $matched, + ); + } +} diff --git a/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/QuarterTransformer.php b/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/QuarterTransformer.php new file mode 100644 index 00000000..8e83dc75 --- /dev/null +++ b/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/QuarterTransformer.php @@ -0,0 +1,64 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Intl\DateFormatter\DateFormat; + +/** + * Parser and formatter for quarter format + * + * @author Igor Wiedler + */ +class QuarterTransformer extends Transformer +{ + /** + * {@inheritDoc} + */ + public function format(\DateTime $dateTime, $length) + { + $month = (int) $dateTime->format('n'); + $quarter = (int) floor(($month - 1) / 3) + 1; + switch ($length) { + case 1: + case 2: + return $this->padLeft($quarter, $length); + case 3: + return 'Q'.$quarter; + default: + $map = array(1 => '1st quarter', 2 => '2nd quarter', 3 => '3rd quarter', 4 => '4th quarter'); + + return $map[$quarter]; + } + } + + /** + * {@inheritDoc} + */ + public function getReverseMatchingRegExp($length) + { + switch ($length) { + case 1: + case 2: + return '\d{'.$length.'}'; + case 3: + return 'Q\d'; + default: + return '(?:1st|2nd|3rd|4th) quarter'; + } + } + + /** + * {@inheritDoc} + */ + public function extractDateOptions($matched, $length) + { + return array(); + } +} diff --git a/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/SecondTransformer.php b/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/SecondTransformer.php new file mode 100644 index 00000000..ccbcdb41 --- /dev/null +++ b/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/SecondTransformer.php @@ -0,0 +1,48 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Intl\DateFormatter\DateFormat; + +/** + * Parser and formatter for the second format + * + * @author Igor Wiedler + */ +class SecondTransformer extends Transformer +{ + /** + * {@inheritDoc} + */ + public function format(\DateTime $dateTime, $length) + { + $secondOfMinute = (int) $dateTime->format('s'); + + return $this->padLeft($secondOfMinute, $length); + } + + /** + * {@inheritDoc} + */ + public function getReverseMatchingRegExp($length) + { + return 1 === $length ? '\d{1,2}' : '\d{'.$length.'}'; + } + + /** + * {@inheritDoc} + */ + public function extractDateOptions($matched, $length) + { + return array( + 'second' => (int) $matched, + ); + } +} diff --git a/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/TimeZoneTransformer.php b/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/TimeZoneTransformer.php new file mode 100644 index 00000000..7d74bd36 --- /dev/null +++ b/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/TimeZoneTransformer.php @@ -0,0 +1,99 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Intl\DateFormatter\DateFormat; + +use Symfony\Component\Intl\Exception\NotImplementedException; + +/** + * Parser and formatter for time zone format + * + * @author Igor Wiedler + */ +class TimeZoneTransformer extends Transformer +{ + /** + * {@inheritDoc} + * + * @throws NotImplementedException When time zone is different than UTC or GMT (Etc/GMT) + */ + public function format(\DateTime $dateTime, $length) + { + $timeZone = substr($dateTime->getTimezone()->getName(), 0, 3); + + if (!in_array($timeZone, array('Etc', 'UTC'))) { + throw new NotImplementedException('Time zone different than GMT or UTC is not supported as a formatting output.'); + } + + // From ICU >= 4.8, the zero offset is not more used, example: GMT instead of GMT+00:00 + $format = (0 !== (int) $dateTime->format('O')) ? '\G\M\TP' : '\G\M\T'; + + return $dateTime->format($format); + } + + /** + * {@inheritDoc} + */ + public function getReverseMatchingRegExp($length) + { + return 'GMT[+-]\d{2}:?\d{2}'; + } + + /** + * {@inheritDoc} + */ + public function extractDateOptions($matched, $length) + { + return array( + 'timezone' => self::getEtcTimeZoneId($matched) + ); + } + + /** + * Get an Etc/GMT timezone identifier for the specified timezone + * + * The PHP documentation for timezones states to not use the 'Other' time zones because them exists + * "for backwards compatibility". However all Etc/GMT time zones are in the tz database 'etcetera' file, + * which indicates they are not deprecated (neither are old names). + * + * Only GMT, Etc/Universal, Etc/Zulu, Etc/Greenwich, Etc/GMT-0, Etc/GMT+0 and Etc/GMT0 are old names and + * are linked to Etc/GMT or Etc/UTC. + * + * @param string $formattedTimeZone A GMT timezone string (GMT-03:00, e.g.) + * + * @return string A timezone identifier + * + * @see http://php.net/manual/en/timezones.others.php + * @see http://www.twinsun.com/tz/tz-link.htm + * + * @throws NotImplementedException When the GMT time zone have minutes offset different than zero + * @throws \InvalidArgumentException When the value can not be matched with pattern + */ + public static function getEtcTimeZoneId($formattedTimeZone) + { + if (preg_match('/GMT(?P[+-])(?P\d{2}):?(?P\d{2})/', $formattedTimeZone, $matches)) { + $hours = (int) $matches['hours']; + $minutes = (int) $matches['minutes']; + $signal = $matches['signal'] == '-' ? '+' : '-'; + + if (0 < $minutes) { + throw new NotImplementedException(sprintf( + 'It is not possible to use a GMT time zone with minutes offset different than zero (0). GMT time zone tried: %s.', + $formattedTimeZone + )); + } + + return 'Etc/GMT'.($hours !== 0 ? $signal.$hours : ''); + } + + throw new \InvalidArgumentException('The GMT time zone \'%s\' does not match with the supported formats GMT[+-]HH:MM or GMT[+-]HHMM.'); + } +} diff --git a/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/Transformer.php b/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/Transformer.php new file mode 100644 index 00000000..0e67f8ae --- /dev/null +++ b/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/Transformer.php @@ -0,0 +1,64 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Intl\DateFormatter\DateFormat; + +/** + * Parser and formatter for date formats + * + * @author Igor Wiedler + */ +abstract class Transformer +{ + /** + * Format a value using a configured DateTime as date/time source + * + * + * @param \DateTime $dateTime A DateTime object to be used to generate the formatted value + * @param int $length The formatted value string length + * + * @return string The formatted value + */ + abstract public function format(\DateTime $dateTime, $length); + + /** + * Returns a reverse matching regular expression of a string generated by format() + * + * @param int $length The length of the value to be reverse matched + * + * @return string The reverse matching regular expression + */ + abstract public function getReverseMatchingRegExp($length); + + /** + * Extract date options from a matched value returned by the processing of the reverse matching + * regular expression + * + * @param string $matched The matched value + * @param int $length The length of the Transformer pattern string + * + * @return array An associative array + */ + abstract public function extractDateOptions($matched, $length); + + /** + * Pad a string with zeros to the left + * + * @param string $value The string to be padded + * @param int $length The length to pad + * + * @return string The padded string + */ + protected function padLeft($value, $length) + { + return str_pad($value, $length, '0', STR_PAD_LEFT); + } +} diff --git a/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/YearTransformer.php b/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/YearTransformer.php new file mode 100644 index 00000000..c3bd699d --- /dev/null +++ b/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/DateFormat/YearTransformer.php @@ -0,0 +1,50 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Intl\DateFormatter\DateFormat; + +/** + * Parser and formatter for year format + * + * @author Igor Wiedler + */ +class YearTransformer extends Transformer +{ + /** + * {@inheritDoc} + */ + public function format(\DateTime $dateTime, $length) + { + if (2 === $length) { + return $dateTime->format('y'); + } + + return $this->padLeft($dateTime->format('Y'), $length); + } + + /** + * {@inheritDoc} + */ + public function getReverseMatchingRegExp($length) + { + return 2 === $length ? '\d{2}' : '\d{4}'; + } + + /** + * {@inheritDoc} + */ + public function extractDateOptions($matched, $length) + { + return array( + 'year' => (int) $matched, + ); + } +} diff --git a/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/IntlDateFormatter.php b/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/IntlDateFormatter.php new file mode 100644 index 00000000..33a499a8 --- /dev/null +++ b/vendor/symfony/intl/Symfony/Component/Intl/DateFormatter/IntlDateFormatter.php @@ -0,0 +1,631 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Intl\DateFormatter; + +use Symfony\Component\Intl\Globals\IntlGlobals; +use Symfony\Component\Intl\DateFormatter\DateFormat\FullTransformer; +use Symfony\Component\Intl\Exception\MethodNotImplementedException; +use Symfony\Component\Intl\Exception\MethodArgumentNotImplementedException; +use Symfony\Component\Intl\Exception\MethodArgumentValueNotImplementedException; +use Symfony\Component\Intl\Locale\Locale; + +/** + * Replacement for PHP's native {@link \IntlDateFormatter} class. + * + * The only methods currently supported in this class are: + * + * - {@link __construct} + * - {@link create} + * - {@link format} + * - {@link getCalendar} + * - {@link getDateType} + * - {@link getErrorCode} + * - {@link getErrorMessage} + * - {@link getLocale} + * - {@link getPattern} + * - {@link getTimeType} + * - {@link getTimeZoneId} + * - {@link isLenient} + * - {@link parse} + * - {@link setLenient} + * - {@link setPattern} + * - {@link setTimeZoneId} + * - {@link setTimeZone} + * + * @author Igor Wiedler + * @author Bernhard Schussek + */ +class IntlDateFormatter +{ + /** + * The error code from the last operation + * + * @var integer + */ + protected $errorCode = IntlGlobals::U_ZERO_ERROR; + + /** + * The error message from the last operation + * + * @var string + */ + protected $errorMessage = 'U_ZERO_ERROR'; + + /* date/time format types */ + const NONE = -1; + const FULL = 0; + const LONG = 1; + const MEDIUM = 2; + const SHORT = 3; + + /* calendar formats */ + const TRADITIONAL = 0; + const GREGORIAN = 1; + + /** + * Patterns used to format the date when no pattern is provided + * + * @var array + */ + private $defaultDateFormats = array( + self::NONE => '', + self::FULL => 'EEEE, LLLL d, y', + self::LONG => 'LLLL d, y', + self::MEDIUM => 'LLL d, y', + self::SHORT => 'M/d/yy', + ); + + /** + * Patterns used to format the time when no pattern is provided + * + * @var array + */ + private $defaultTimeFormats = array( + self::FULL => 'h:mm:ss a zzzz', + self::LONG => 'h:mm:ss a z', + self::MEDIUM => 'h:mm:ss a', + self::SHORT => 'h:mm a', + ); + + /** + * @var int + */ + private $datetype; + + /** + * @var int + */ + private $timetype; + + /** + * @var string + */ + private $pattern; + + /** + * @var \DateTimeZone + */ + private $dateTimeZone; + + /** + * @var Boolean + */ + private $unitializedTimeZoneId = false; + + /** + * @var string + */ + private $timeZoneId; + + /** + * Constructor + * + * @param string $locale The locale code. The only currently supported locale is "en". + * @param int $datetype Type of date formatting, one of the format type constants + * @param int $timetype Type of time formatting, one of the format type constants + * @param string $timezone Timezone identifier + * @param int $calendar Calendar to use for formatting or parsing. The only currently + * supported value is IntlDateFormatter::GREGORIAN. + * @param string $pattern Optional pattern to use when formatting + * + * @see http://www.php.net/manual/en/intldateformatter.create.php + * @see http://userguide.icu-project.org/formatparse/datetime + * + * @throws MethodArgumentValueNotImplementedException When $locale different than "en" is passed + * @throws MethodArgumentValueNotImplementedException When $calendar different than GREGORIAN is passed + */ + public function __construct($locale, $datetype, $timetype, $timezone = null, $calendar = self::GREGORIAN, $pattern = null) + { + if ('en' !== $locale) { + throw new MethodArgumentValueNotImplementedException(__METHOD__, 'locale', $locale, 'Only the locale "en" is supported'); + } + + if (self::GREGORIAN !== $calendar) { + throw new MethodArgumentValueNotImplementedException(__METHOD__, 'calendar', $calendar, 'Only the GREGORIAN calendar is supported'); + } + + $this->datetype = $datetype; + $this->timetype = $timetype; + + $this->setPattern($pattern); + $this->setTimeZoneId($timezone); + } + + /** + * Static constructor + * + * @param string $locale The locale code. The only currently supported locale is "en". + * @param int $datetype Type of date formatting, one of the format type constants + * @param int $timetype Type of time formatting, one of the format type constants + * @param string $timezone Timezone identifier + * @param int $calendar Calendar to use for formatting or parsing; default is Gregorian. + * One of the calendar constants. + * @param string $pattern Optional pattern to use when formatting + * + * @return IntlDateFormatter + * + * @see http://www.php.net/manual/en/intldateformatter.create.php + * @see http://userguide.icu-project.org/formatparse/datetime + * + * @throws MethodArgumentValueNotImplementedException When $locale different than "en" is passed + * @throws MethodArgumentValueNotImplementedException When $calendar different than GREGORIAN is passed + */ + public static function create($locale, $datetype, $timetype, $timezone = null, $calendar = self::GREGORIAN, $pattern = null) + { + return new self($locale, $datetype, $timetype, $timezone, $calendar, $pattern); + } + + /** + * Format the date/time value (timestamp) as a string + * + * @param integer|\DateTime $timestamp The timestamp to format. \DateTime objects + * are supported as of PHP 5.3.4. + * + * @return string|Boolean The formatted value or false if formatting failed. + * + * @see http://www.php.net/manual/en/intldateformatter.format.php + * + * @throws MethodArgumentValueNotImplementedException If one of the formatting characters is not implemented + */ + public function format($timestamp) + { + // intl allows timestamps to be passed as arrays - we don't + if (is_array($timestamp)) { + $message = version_compare(PHP_VERSION, '5.3.4', '>=') ? + 'Only integer unix timestamps and DateTime objects are supported' : + 'Only integer unix timestamps are supported'; + + throw new MethodArgumentValueNotImplementedException(__METHOD__, 'timestamp', $timestamp, $message); + } + + // behave like the intl extension + $argumentError = null; + if (version_compare(PHP_VERSION, '5.3.4', '<') && !is_int($timestamp)) { + $argumentError = 'datefmt_format: takes either an array or an integer timestamp value '; + } elseif (version_compare(PHP_VERSION, '5.3.4', '>=') && !is_int($timestamp) && !$timestamp instanceof \DateTime) { + $argumentError = 'datefmt_format: takes either an array or an integer timestamp value or a DateTime object'; + if (version_compare(PHP_VERSION, '5.5.0-dev', '>=') && !is_int($timestamp)) { + $argumentError = sprintf('datefmt_format: string \'%s\' is not numeric, which would be required for it to be a valid date', $timestamp); + } + } + + if (null !== $argumentError) { + IntlGlobals::setError(IntlGlobals::U_ILLEGAL_ARGUMENT_ERROR, $argumentError); + $this->errorCode = IntlGlobals::getErrorCode(); + $this->errorMessage = IntlGlobals::getErrorMessage(); + + return false; + } + + // As of PHP 5.3.4, IntlDateFormatter::format() accepts DateTime instances + if (version_compare(PHP_VERSION, '5.3.4', '>=') && $timestamp instanceof \DateTime) { + $timestamp = $timestamp->getTimestamp(); + } + + $transformer = new FullTransformer($this->getPattern(), $this->getTimeZoneId()); + $formatted = $transformer->format($this->createDateTime($timestamp)); + + // behave like the intl extension + IntlGlobals::setError(IntlGlobals::U_ZERO_ERROR); + $this->errorCode = IntlGlobals::getErrorCode(); + $this->errorMessage = IntlGlobals::getErrorMessage(); + + return $formatted; + } + + /** + * Not supported. Formats an object + * + * @param object $object + * @param mixed $format + * @param string $locale + * + * @return string The formatted value + * + * @see http://www.php.net/manual/en/intldateformatter.formatobject.php + * + * @throws MethodNotImplementedException + */ + public function formatObject($object, $format = null, $locale = null) + { + throw new MethodNotImplementedException(__METHOD__); + } + + /** + * Returns the formatter's calendar + * + * @return int The calendar being used by the formatter. Currently always returns + * IntlDateFormatter::GREGORIAN. + * + * @see http://www.php.net/manual/en/intldateformatter.getcalendar.php + */ + public function getCalendar() + { + return self::GREGORIAN; + } + + /** + * Not supported. Returns the formatter's calendar object + * + * @return object The calendar's object being used by the formatter + * + * @see http://www.php.net/manual/en/intldateformatter.getcalendarobject.php + * + * @throws MethodNotImplementedException + */ + public function getCalendarObject() + { + throw new MethodNotImplementedException(__METHOD__); + } + + /** + * Returns the formatter's datetype + * + * @return int The current value of the formatter + * + * @see http://www.php.net/manual/en/intldateformatter.getdatetype.php + */ + public function getDateType() + { + return $this->datetype; + } + + /** + * Returns formatter's last error code. Always returns the U_ZERO_ERROR class constant value + * + * @return int The error code from last formatter call + * + * @see http://www.php.net/manual/en/intldateformatter.geterrorcode.php + */ + public function getErrorCode() + { + return $this->errorCode; + } + + /** + * Returns formatter's last error message. Always returns the U_ZERO_ERROR_MESSAGE class constant value + * + * @return string The error message from last formatter call + * + * @see http://www.php.net/manual/en/intldateformatter.geterrormessage.php + */ + public function getErrorMessage() + { + return $this->errorMessage; + } + + /** + * Returns the formatter's locale + * + * @param int $type Not supported. The locale name type to return (Locale::VALID_LOCALE or Locale::ACTUAL_LOCALE) + * + * @return string The locale used to create the formatter. Currently always + * returns "en". + * + * @see http://www.php.net/manual/en/intldateformatter.getlocale.php + */ + public function getLocale($type = Locale::ACTUAL_LOCALE) + { + return 'en'; + } + + /** + * Returns the formatter's pattern + * + * @return string The pattern string used by the formatter + * + * @see http://www.php.net/manual/en/intldateformatter.getpattern.php + */ + public function getPattern() + { + return $this->pattern; + } + + /** + * Returns the formatter's time type + * + * @return string The time type used by the formatter + * + * @see http://www.php.net/manual/en/intldateformatter.gettimetype.php + */ + public function getTimeType() + { + return $this->timetype; + } + + /** + * Returns the formatter's timezone identifier + * + * @return string The timezone identifier used by the formatter + * + * @see http://www.php.net/manual/en/intldateformatter.gettimezoneid.php + */ + public function getTimeZoneId() + { + if (!$this->unitializedTimeZoneId) { + return $this->timeZoneId; + } + + // In PHP 5.5 default timezone depends on `date_default_timezone_get()` method + if (version_compare(PHP_VERSION, '5.5.0-dev', '>=')) { + return date_default_timezone_get(); + } + + return null; + } + + /** + * Not supported. Returns the formatter's timezone + * + * @return mixed The timezone used by the formatter + * + * @see http://www.php.net/manual/en/intldateformatter.gettimezone.php + * + * @throws MethodNotImplementedException + */ + public function getTimeZone() + { + throw new MethodNotImplementedException(__METHOD__); + } + + /** + * Returns whether the formatter is lenient + * + * @return Boolean Currently always returns false. + * + * @see http://www.php.net/manual/en/intldateformatter.islenient.php + * + * @throws MethodNotImplementedException + */ + public function isLenient() + { + return false; + } + + /** + * Not supported. Parse string to a field-based time value + * + * @param string $value String to convert to a time value + * @param int $position Position at which to start the parsing in $value (zero-based). + * If no error occurs before $value is consumed, $parse_pos will + * contain -1 otherwise it will contain the position at which parsing + * ended. If $parse_pos > strlen($value), the parse fails immediately. + * + * @return string Localtime compatible array of integers: contains 24 hour clock value in tm_hour field + * + * @see http://www.php.net/manual/en/intldateformatter.localtime.php + * + * @throws MethodNotImplementedException + */ + public function localtime($value, &$position = 0) + { + throw new MethodNotImplementedException(__METHOD__); + } + + /** + * Parse string to a timestamp value + * + * @param string $value String to convert to a time value + * @param int $position Not supported. Position at which to start the parsing in $value (zero-based). + * If no error occurs before $value is consumed, $parse_pos will + * contain -1 otherwise it will contain the position at which parsing + * ended. If $parse_pos > strlen($value), the parse fails immediately. + * + * @return string Parsed value as a timestamp + * + * @see http://www.php.net/manual/en/intldateformatter.parse.php + * + * @throws MethodArgumentNotImplementedException When $position different than null, behavior not implemented + */ + public function parse($value, &$position = null) + { + // We don't calculate the position when parsing the value + if (null !== $position) { + throw new MethodArgumentNotImplementedException(__METHOD__, 'position'); + } + + $dateTime = $this->createDateTime(0); + $transformer = new FullTransformer($this->getPattern(), $this->getTimeZoneId()); + + $timestamp = $transformer->parse($dateTime, $value); + + // behave like the intl extension. FullTransformer::parse() set the proper error + $this->errorCode = IntlGlobals::getErrorCode(); + $this->errorMessage = IntlGlobals::getErrorMessage(); + + return $timestamp; + } + + /** + * Not supported. Set the formatter's calendar + * + * @param string $calendar The calendar to use. Default is IntlDateFormatter::GREGORIAN. + * + * @return Boolean true on success or false on failure + * + * @see http://www.php.net/manual/en/intldateformatter.setcalendar.php + * + * @throws MethodNotImplementedException + */ + public function setCalendar($calendar) + { + throw new MethodNotImplementedException(__METHOD__); + } + + /** + * Set the leniency of the parser + * + * Define if the parser is strict or lenient in interpreting inputs that do not match the pattern + * exactly. Enabling lenient parsing allows the parser to accept otherwise flawed date or time + * patterns, parsing as much as possible to obtain a value. Extra space, unrecognized tokens, or + * invalid values ("February 30th") are not accepted. + * + * @param Boolean $lenient Sets whether the parser is lenient or not. Currently + * only false (strict) is supported. + * + * @return Boolean true on success or false on failure + * + * @see http://www.php.net/manual/en/intldateformatter.setlenient.php + * + * @throws MethodArgumentValueNotImplementedException When $lenient is true + */ + public function setLenient($lenient) + { + if ($lenient) { + throw new MethodArgumentValueNotImplementedException(__METHOD__, 'lenient', $lenient, 'Only the strict parser is supported'); + } + + return true; + } + + /** + * Set the formatter's pattern + * + * @param string $pattern A pattern string in conformance with the ICU IntlDateFormatter documentation + * + * @return Boolean true on success or false on failure + * + * @see http://www.php.net/manual/en/intldateformatter.setpattern.php + * @see http://userguide.icu-project.org/formatparse/datetime + */ + public function setPattern($pattern) + { + if (null === $pattern) { + $pattern = $this->getDefaultPattern(); + } + + $this->pattern = $pattern; + + return true; + } + + /** + * Set the formatter's timezone identifier + * + * @param string $timeZoneId The time zone ID string of the time zone to use. + * If NULL or the empty string, the default time zone for the + * runtime is used. + * + * @return Boolean true on success or false on failure + * + * @see http://www.php.net/manual/en/intldateformatter.settimezoneid.php + */ + public function setTimeZoneId($timeZoneId) + { + if (null === $timeZoneId) { + // In PHP 5.5 if $timeZoneId is null it fallbacks to `date_default_timezone_get()` method + if (version_compare(PHP_VERSION, '5.5.0-dev', '>=')) { + $timeZoneId = date_default_timezone_get(); + } else { + // TODO: changes were made to ext/intl in PHP 5.4.4 release that need to be investigated since it will + // use ini's date.timezone when the time zone is not provided. As a not well tested workaround, uses UTC. + // See the first two items of the commit message for more information: + // https://github.com/php/php-src/commit/eb346ef0f419b90739aadfb6cc7b7436c5b521d9 + $timeZoneId = getenv('TZ') ?: 'UTC'; + } + + $this->unitializedTimeZoneId = true; + } + + // Backup original passed time zone + $timeZone = $timeZoneId; + + // Get an Etc/GMT time zone that is accepted for \DateTimeZone + if ('GMT' !== $timeZoneId && 0 === strpos($timeZoneId, 'GMT')) { + try { + $timeZoneId = DateFormat\TimeZoneTransformer::getEtcTimeZoneId($timeZoneId); + } catch (\InvalidArgumentException $e) { + // Does nothing, will fallback to UTC + } + } + + try { + $this->dateTimeZone = new \DateTimeZone($timeZoneId); + } catch (\Exception $e) { + $this->dateTimeZone = new \DateTimeZone('UTC'); + } + + $this->timeZoneId = $timeZone; + + return true; + } + + /** + * This method was added in PHP 5.5 as replacement for `setTimeZoneId()` + * + * @param mixed $timeZone + * + * @return Boolean true on success or false on failure + * + * @see http://www.php.net/manual/en/intldateformatter.settimezone.php + */ + public function setTimeZone($timeZone) + { + return $this->setTimeZoneId($timeZone); + } + + /** + * Create and returns a DateTime object with the specified timestamp and with the + * current time zone + * + * @param int $timestamp + * + * @return \DateTime + */ + protected function createDateTime($timestamp) + { + $dateTime = new \DateTime(); + $dateTime->setTimestamp($timestamp); + $dateTime->setTimezone($this->dateTimeZone); + + return $dateTime; + } + + /** + * Returns a pattern string based in the datetype and timetype values + * + * @return string + */ + protected function getDefaultPattern() + { + $patternParts = array(); + if (self::NONE !== $this->datetype) { + $patternParts[] = $this->defaultDateFormats[$this->datetype]; + } + if (self::NONE !== $this->timetype) { + $patternParts[] = $this->defaultTimeFormats[$this->timetype]; + } + $pattern = implode(' ', $patternParts); + + return $pattern; + } +} -- cgit v1.2.3