1 <?php
2
3 /*
4 * This file is part of the ICanBoogie package.
5 *
6 * (c) Olivier Laviale <olivier.laviale@gmail.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 ICanBoogie\CLDR;
13
14 use ICanBoogie\PropertyNotDefined;
15
16 /**
17 * Representation of a locale calendar.
18 *
19 * @property-read Locale $locale The locale this calendar is defined in.
20 * @property-read DataTimeFormatter $datetime_formatter A datetime formatter.
21 *
22 * @property-read string $standalone_abbreviated_days Shortcut to `days/stand-alone/abbreviated`.
23 * @property-read string $standalone_abbreviated_eras Shortcut to `eras/eraAbbr`.
24 * @property-read string $standalone_abbreviated_months Shortcut to `months/stand-alone/abbreviated`.
25 * @property-read string $standalone_abbreviated_quarters Shortcut to `quarters/stand-alone/abbreviated`.
26 * @property-read string $standalone_narrow_days Shortcut to `days/stand-alone/narrow`.
27 * @property-read string $standalone_narrow_eras Shortcut to `eras/eraNarrow`.
28 * @property-read string $standalone_narrow_months Shortcut to `months/stand-alone/narrow`.
29 * @property-read string $standalone_narrow_quarters Shortcut to `quarters/stand-alone/narrow`.
30 * @property-read string $standalone_short_days Shortcut to `days/stand-alone/short`.
31 * @property-read string $standalone_short_eras Shortcut to `eras/eraAbbr`.
32 * @property-read string $standalone_short_months Shortcut to `months/stand-alone/abbreviated`.
33 * @property-read string $standalone_short_quarters Shortcut to `quarters/stand-alone/abbreviated`.
34 * @property-read string $standalone_wide_days Shortcut to `days/stand-alone/wide`.
35 * @property-read string $standalone_wide_eras Shortcut to `eras/eraNames`.
36 * @property-read string $standalone_wide_months Shortcut to `months/stand-alone/wide`.
37 * @property-read string $standalone_wide_quarters Shortcut to `quarters/stand-alone/wide`.
38 * @property-read string $abbreviated_days Shortcut to `days/format/abbreviated`.
39 * @property-read string $abbreviated_eras Shortcut to `eras/eraAbbr`.
40 * @property-read string $abbreviated_months Shortcut to `months/format/abbreviated`.
41 * @property-read string $abbreviated_quarters Shortcut to `quarters/format/abbreviated`.
42 * @property-read string $narrow_days Shortcut to `days/format/narrow`.
43 * @property-read string $narrow_eras Shortcut to `eras/eraNarrow`.
44 * @property-read string $narrow_months Shortcut to `months/format/narrow`.
45 * @property-read string $narrow_quarters Shortcut to `quarters/format/narrow`.
46 * @property-read string $short_days Shortcut to `days/format/short`.
47 * @property-read string $short_eras Shortcut to `eras/eraAbbr`.
48 * @property-read string $short_months Shortcut to `months/format/abbreviated`.
49 * @property-read string $short_quarters Shortcut to `quarters/format/abbreviated`.
50 * @property-read string $wide_days Shortcut to `days/format/wide`.
51 * @property-read string $wide_eras Shortcut to `eras/eraNames`.
52 * @property-read string $wide_months Shortcut to `months/format/wide`.
53 * @property-read string $wide_quarters Shortcut to `quarters/format/wide`.
54 */
55 class Calendar extends \ArrayObject
56 {
57 /**
58 * Representation of the locale defining this calendar.
59 *
60 * @var Locale
61 */
62 protected $locale;
63
64 public function __construct(Locale $locale, array $data)
65 {
66 $this->locale = $locale;
67
68 parent::__construct($data);
69 }
70
71 /**
72 * Datetime formatter.
73 *
74 * @var DateTimeFormatter
75 */
76 private $datetime_formatter;
77
78 /**
79 * Date formatter.
80 *
81 * @var DateFormatter
82 */
83 private $date_formatter;
84
85 /**
86 * Time formatter.
87 *
88 * @var TimeFormatter
89 */
90 private $time_formatter;
91
92 public function __get($property)
93 {
94 static $era_translation = array
95 (
96 'abbreviated' => 'eraAbbr',
97 'narrow' => 'eraNarrow',
98 'short' => 'eraAbbr',
99 'wide' => 'eraNames'
100 );
101
102 switch ($property)
103 {
104 case 'locale':
105
106 return $this->locale;
107
108 case 'datetime_formatter':
109
110 if (!$this->datetime_formatter)
111 {
112 $this->datetime_formatter = new DateTimeFormatter($this);
113 }
114
115 return $this->datetime_formatter;
116
117 case 'date_formatter':
118
119 if (!$this->date_formatter)
120 {
121 $this->date_formatter = new DateFormatter($this);
122 }
123
124 return $this->date_formatter;
125
126 case 'time_formatter':
127
128 if (!$this->time_formatter)
129 {
130 $this->time_formatter = new TimeFormatter($this);
131 }
132
133 return $this->time_formatter;
134 }
135
136 if (preg_match('#^(standalone_)?(abbreviated|narrow|short|wide)_(days|eras|months|quarters)$#', $property, $matches))
137 {
138 list(, $standalone, $width, $type) = $matches;
139
140 $data = $this[$type];
141
142 if ($type == 'eras')
143 {
144 return $this->$property = $data[$era_translation[$width]];
145 }
146 else
147 {
148 $data = $data[$standalone ? 'stand-alone' : 'format'];
149
150 if ($width == 'short' && empty($data[$width]))
151 {
152 $width = 'abbreviated';
153 }
154
155 return $this->$property = $data[$width];
156 }
157 }
158
159 throw new PropertyNotDefined(array($property, $this));
160 }
161 }