FullTransformer.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Polyfill\Intl\Icu\DateFormat;
  11. use Symfony\Polyfill\Intl\Icu\Exception\NotImplementedException;
  12. use Symfony\Polyfill\Intl\Icu\Icu;
  13. /**
  14. * Parser and formatter for date formats.
  15. *
  16. * @author Igor Wiedler <igor@wiedler.ch>
  17. *
  18. * @internal
  19. */
  20. class FullTransformer
  21. {
  22. private $quoteMatch = "'(?:[^']+|'')*'";
  23. private $implementedChars = 'MLydQqhDEaHkKmsz';
  24. private $notImplementedChars = 'GYuwWFgecSAZvVW';
  25. private $regExp;
  26. /**
  27. * @var Transformer[]
  28. */
  29. private $transformers;
  30. private $pattern;
  31. private $timezone;
  32. /**
  33. * @param string $pattern The pattern to be used to format and/or parse values
  34. * @param string $timezone The timezone to perform the date/time calculations
  35. */
  36. public function __construct(string $pattern, string $timezone)
  37. {
  38. $this->pattern = $pattern;
  39. $this->timezone = $timezone;
  40. $implementedCharsMatch = $this->buildCharsMatch($this->implementedChars);
  41. $notImplementedCharsMatch = $this->buildCharsMatch($this->notImplementedChars);
  42. $this->regExp = "/($this->quoteMatch|$implementedCharsMatch|$notImplementedCharsMatch)/";
  43. $this->transformers = [
  44. 'M' => new MonthTransformer(),
  45. 'L' => new MonthTransformer(),
  46. 'y' => new YearTransformer(),
  47. 'd' => new DayTransformer(),
  48. 'q' => new QuarterTransformer(),
  49. 'Q' => new QuarterTransformer(),
  50. 'h' => new Hour1201Transformer(),
  51. 'D' => new DayOfYearTransformer(),
  52. 'E' => new DayOfWeekTransformer(),
  53. 'a' => new AmPmTransformer(),
  54. 'H' => new Hour2400Transformer(),
  55. 'K' => new Hour1200Transformer(),
  56. 'k' => new Hour2401Transformer(),
  57. 'm' => new MinuteTransformer(),
  58. 's' => new SecondTransformer(),
  59. 'z' => new TimezoneTransformer(),
  60. ];
  61. }
  62. /**
  63. * Format a DateTime using ICU dateformat pattern.
  64. *
  65. * @return string The formatted value
  66. */
  67. public function format(\DateTime $dateTime): string
  68. {
  69. $formatted = preg_replace_callback($this->regExp, function ($matches) use ($dateTime) {
  70. return $this->formatReplace($matches[0], $dateTime);
  71. }, $this->pattern);
  72. return $formatted;
  73. }
  74. /**
  75. * Return the formatted ICU value for the matched date characters.
  76. *
  77. * @throws NotImplementedException When it encounters a not implemented date character
  78. */
  79. private function formatReplace(string $dateChars, \DateTime $dateTime): string
  80. {
  81. $length = \strlen($dateChars);
  82. if ($this->isQuoteMatch($dateChars)) {
  83. return $this->replaceQuoteMatch($dateChars);
  84. }
  85. if (isset($this->transformers[$dateChars[0]])) {
  86. $transformer = $this->transformers[$dateChars[0]];
  87. return $transformer->format($dateTime, $length);
  88. }
  89. // handle unimplemented characters
  90. if (false !== strpos($this->notImplementedChars, $dateChars[0])) {
  91. throw new NotImplementedException(sprintf('Unimplemented date character "%s" in format "%s".', $dateChars[0], $this->pattern));
  92. }
  93. return '';
  94. }
  95. /**
  96. * Parse a pattern based string to a timestamp value.
  97. *
  98. * @param \DateTime $dateTime A configured DateTime object to use to perform the date calculation
  99. * @param string $value String to convert to a time value
  100. *
  101. * @return int|false The corresponding Unix timestamp
  102. *
  103. * @throws \InvalidArgumentException When the value can not be matched with pattern
  104. */
  105. public function parse(\DateTime $dateTime, string $value)
  106. {
  107. $reverseMatchingRegExp = $this->getReverseMatchingRegExp($this->pattern);
  108. $reverseMatchingRegExp = '/^'.$reverseMatchingRegExp.'$/';
  109. $options = [];
  110. if (preg_match($reverseMatchingRegExp, $value, $matches)) {
  111. $matches = $this->normalizeArray($matches);
  112. foreach ($this->transformers as $char => $transformer) {
  113. if (isset($matches[$char])) {
  114. $length = \strlen($matches[$char]['pattern']);
  115. $options = array_merge($options, $transformer->extractDateOptions($matches[$char]['value'], $length));
  116. }
  117. }
  118. // reset error code and message
  119. Icu::setError(Icu::U_ZERO_ERROR);
  120. return $this->calculateUnixTimestamp($dateTime, $options);
  121. }
  122. // behave like the intl extension
  123. Icu::setError(Icu::U_PARSE_ERROR, 'Date parsing failed');
  124. return false;
  125. }
  126. /**
  127. * Retrieve a regular expression to match with a formatted value.
  128. *
  129. * @return string The reverse matching regular expression with named captures being formed by the
  130. * transformer index in the $transformer array
  131. */
  132. private function getReverseMatchingRegExp(string $pattern): string
  133. {
  134. $escapedPattern = preg_quote($pattern, '/');
  135. // ICU 4.8 recognizes slash ("/") in a value to be parsed as a dash ("-") and vice-versa
  136. // when parsing a date/time value
  137. $escapedPattern = preg_replace('/\\\[\-|\/]/', '[\/\-]', $escapedPattern);
  138. $reverseMatchingRegExp = preg_replace_callback($this->regExp, function ($matches) {
  139. $length = \strlen($matches[0]);
  140. $transformerIndex = $matches[0][0];
  141. $dateChars = $matches[0];
  142. if ($this->isQuoteMatch($dateChars)) {
  143. return $this->replaceQuoteMatch($dateChars);
  144. }
  145. if (isset($this->transformers[$transformerIndex])) {
  146. $transformer = $this->transformers[$transformerIndex];
  147. $captureName = str_repeat($transformerIndex, $length);
  148. return "(?P<$captureName>".$transformer->getReverseMatchingRegExp($length).')';
  149. }
  150. return null;
  151. }, $escapedPattern);
  152. return $reverseMatchingRegExp;
  153. }
  154. /**
  155. * Check if the first char of a string is a single quote.
  156. */
  157. private function isQuoteMatch(string $quoteMatch): bool
  158. {
  159. return "'" === $quoteMatch[0];
  160. }
  161. /**
  162. * Replaces single quotes at the start or end of a string with two single quotes.
  163. */
  164. private function replaceQuoteMatch(string $quoteMatch): string
  165. {
  166. if (preg_match("/^'+$/", $quoteMatch)) {
  167. return str_replace("''", "'", $quoteMatch);
  168. }
  169. return str_replace("''", "'", substr($quoteMatch, 1, -1));
  170. }
  171. /**
  172. * Builds a chars match regular expression.
  173. */
  174. private function buildCharsMatch(string $specialChars): string
  175. {
  176. $specialCharsArray = str_split($specialChars);
  177. $specialCharsMatch = implode('|', array_map(function ($char) {
  178. return $char.'+';
  179. }, $specialCharsArray));
  180. return $specialCharsMatch;
  181. }
  182. /**
  183. * Normalize a preg_replace match array, removing the numeric keys and returning an associative array
  184. * with the value and pattern values for the matched Transformer.
  185. */
  186. private function normalizeArray(array $data): array
  187. {
  188. $ret = [];
  189. foreach ($data as $key => $value) {
  190. if (!\is_string($key)) {
  191. continue;
  192. }
  193. $ret[$key[0]] = [
  194. 'value' => $value,
  195. 'pattern' => $key,
  196. ];
  197. }
  198. return $ret;
  199. }
  200. /**
  201. * Calculates the Unix timestamp based on the matched values by the reverse matching regular
  202. * expression of parse().
  203. *
  204. * @return bool|int The calculated timestamp or false if matched date is invalid
  205. */
  206. private function calculateUnixTimestamp(\DateTime $dateTime, array $options)
  207. {
  208. $options = $this->getDefaultValueForOptions($options);
  209. $year = $options['year'];
  210. $month = $options['month'];
  211. $day = $options['day'];
  212. $hour = $options['hour'];
  213. $hourInstance = $options['hourInstance'];
  214. $minute = $options['minute'];
  215. $second = $options['second'];
  216. $marker = $options['marker'];
  217. $timezone = $options['timezone'];
  218. // If month is false, return immediately (intl behavior)
  219. if (false === $month) {
  220. Icu::setError(Icu::U_PARSE_ERROR, 'Date parsing failed');
  221. return false;
  222. }
  223. // Normalize hour
  224. if ($hourInstance instanceof HourTransformer) {
  225. $hour = $hourInstance->normalizeHour($hour, $marker);
  226. }
  227. // Set the timezone if different from the default one
  228. if (null !== $timezone && $timezone !== $this->timezone) {
  229. $dateTime->setTimezone(new \DateTimeZone($timezone));
  230. }
  231. // Normalize yy year
  232. preg_match_all($this->regExp, $this->pattern, $matches);
  233. if (\in_array('yy', $matches[0])) {
  234. $dateTime->setTimestamp(time());
  235. $year = $year > (int) $dateTime->format('y') + 20 ? 1900 + $year : 2000 + $year;
  236. }
  237. $dateTime->setDate($year, $month, $day);
  238. $dateTime->setTime($hour, $minute, $second);
  239. return $dateTime->getTimestamp();
  240. }
  241. /**
  242. * Add sensible default values for missing items in the extracted date/time options array. The values
  243. * are base in the beginning of the Unix era.
  244. */
  245. private function getDefaultValueForOptions(array $options): array
  246. {
  247. return [
  248. 'year' => $options['year'] ?? 1970,
  249. 'month' => $options['month'] ?? 1,
  250. 'day' => $options['day'] ?? 1,
  251. 'hour' => $options['hour'] ?? 0,
  252. 'hourInstance' => $options['hourInstance'] ?? null,
  253. 'minute' => $options['minute'] ?? 0,
  254. 'second' => $options['second'] ?? 0,
  255. 'marker' => $options['marker'] ?? null,
  256. 'timezone' => $options['timezone'] ?? null,
  257. ];
  258. }
  259. }