4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Intl\DateFormatter\DateFormat
;
15 * Parser and formatter for month format
17 * @author Igor Wiedler <igor@wiedler.ch>
19 class MonthTransformer
extends Transformer
24 protected static $months = array(
40 * Short months names (first 3 letters)
43 protected static $shortMonths = array();
46 * Flipped $months array, $name => $index
49 protected static $flippedMonths = array();
52 * Flipped $shortMonths array, $name => $index
55 protected static $flippedShortMonths = array();
60 public function __construct()
62 if (0 === count(self
::$shortMonths)) {
63 self
::$shortMonths = array_map(function($month) {
64 return substr($month, 0, 3);
67 self
::$flippedMonths = array_flip(self
::$months);
68 self
::$flippedShortMonths = array_flip(self
::$shortMonths);
75 public function format(\DateTime
$dateTime, $length)
77 $matchLengthMap = array(
84 if (isset($matchLengthMap[$length])) {
85 return $dateTime->format($matchLengthMap[$length]);
89 return substr($dateTime->format('M'), 0, 1);
92 return $this->padLeft($dateTime->format('m'), $length);
98 public function getReverseMatchingRegExp($length)
105 $regExp = implode('|', self
::$shortMonths);
108 $regExp = implode('|', self
::$months);
111 $regExp = '[JFMASOND]';
114 $regExp = '\d{'.$length.'}';
124 public function extractDateOptions($matched, $length)
126 if (!is_numeric($matched)) {
128 $matched = self
::$flippedShortMonths[$matched] +
1;
129 } elseif (4 === $length) {
130 $matched = self
::$flippedMonths[$matched] +
1;
131 } elseif (5 === $length) {
132 // IntlDateFormatter::parse() always returns false for MMMMM or LLLLL
136 $matched = (int) $matched;