]>
Commit | Line | Data |
---|---|---|
4f5b44bd NL |
1 | <?php |
2 | ||
3 | /* | |
4 | * This file is part of the Symfony package. | |
5 | * | |
6 | * (c) Fabien Potencier <fabien@symfony.com> | |
7 | * | |
8 | * For the full copyright and license information, please view the LICENSE | |
9 | * file that was distributed with this source code. | |
10 | */ | |
11 | ||
12 | namespace Symfony\Component\Intl\DateFormatter\DateFormat; | |
13 | ||
14 | /** | |
15 | * Parser and formatter for month format | |
16 | * | |
17 | * @author Igor Wiedler <igor@wiedler.ch> | |
18 | */ | |
19 | class MonthTransformer extends Transformer | |
20 | { | |
21 | /** | |
22 | * @var array | |
23 | */ | |
24 | protected static $months = array( | |
25 | 'January', | |
26 | 'February', | |
27 | 'March', | |
28 | 'April', | |
29 | 'May', | |
30 | 'June', | |
31 | 'July', | |
32 | 'August', | |
33 | 'September', | |
34 | 'October', | |
35 | 'November', | |
36 | 'December' | |
37 | ); | |
38 | ||
39 | /** | |
40 | * Short months names (first 3 letters) | |
41 | * @var array | |
42 | */ | |
43 | protected static $shortMonths = array(); | |
44 | ||
45 | /** | |
46 | * Flipped $months array, $name => $index | |
47 | * @var array | |
48 | */ | |
49 | protected static $flippedMonths = array(); | |
50 | ||
51 | /** | |
52 | * Flipped $shortMonths array, $name => $index | |
53 | * @var array | |
54 | */ | |
55 | protected static $flippedShortMonths = array(); | |
56 | ||
57 | /** | |
58 | * Constructor | |
59 | */ | |
60 | public function __construct() | |
61 | { | |
62 | if (0 === count(self::$shortMonths)) { | |
63 | self::$shortMonths = array_map(function($month) { | |
64 | return substr($month, 0, 3); | |
65 | }, self::$months); | |
66 | ||
67 | self::$flippedMonths = array_flip(self::$months); | |
68 | self::$flippedShortMonths = array_flip(self::$shortMonths); | |
69 | } | |
70 | } | |
71 | ||
72 | /** | |
73 | * {@inheritDoc} | |
74 | */ | |
75 | public function format(\DateTime $dateTime, $length) | |
76 | { | |
77 | $matchLengthMap = array( | |
78 | 1 => 'n', | |
79 | 2 => 'm', | |
80 | 3 => 'M', | |
81 | 4 => 'F', | |
82 | ); | |
83 | ||
84 | if (isset($matchLengthMap[$length])) { | |
85 | return $dateTime->format($matchLengthMap[$length]); | |
86 | } | |
87 | ||
88 | if (5 === $length) { | |
89 | return substr($dateTime->format('M'), 0, 1); | |
90 | } | |
91 | ||
92 | return $this->padLeft($dateTime->format('m'), $length); | |
93 | } | |
94 | ||
95 | /** | |
96 | * {@inheritDoc} | |
97 | */ | |
98 | public function getReverseMatchingRegExp($length) | |
99 | { | |
100 | switch ($length) { | |
101 | case 1: | |
102 | $regExp = '\d{1,2}'; | |
103 | break; | |
104 | case 3: | |
105 | $regExp = implode('|', self::$shortMonths); | |
106 | break; | |
107 | case 4: | |
108 | $regExp = implode('|', self::$months); | |
109 | break; | |
110 | case 5: | |
111 | $regExp = '[JFMASOND]'; | |
112 | break; | |
113 | default: | |
114 | $regExp = '\d{'.$length.'}'; | |
115 | break; | |
116 | } | |
117 | ||
118 | return $regExp; | |
119 | } | |
120 | ||
121 | /** | |
122 | * {@inheritDoc} | |
123 | */ | |
124 | public function extractDateOptions($matched, $length) | |
125 | { | |
126 | if (!is_numeric($matched)) { | |
127 | if (3 === $length) { | |
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 | |
133 | $matched = false; | |
134 | } | |
135 | } else { | |
136 | $matched = (int) $matched; | |
137 | } | |
138 | ||
139 | return array( | |
140 | 'month' => $matched, | |
141 | ); | |
142 | } | |
143 | } |