1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
<?php
/*
* This file is part of the ICanBoogie package.
*
* (c) Olivier Laviale <olivier.laviale@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ICanBoogie\CLDR;
use ICanBoogie\Accessor\AccessorTrait;
/**
* Representation of a locale calendar.
*
* @property-read Locale $locale The locale this calendar is defined in.
* @property-read DateTimeFormatter $datetime_formatter A datetime formatter.
* @property-read DateFormatter $date_formatter A date formatter.
* @property-read TimeFormatter $time_formatter A time formatter.
*
* @property-read string $standalone_abbreviated_days Shortcut to `days/stand-alone/abbreviated`.
* @property-read string $standalone_abbreviated_eras Shortcut to `eras/eraAbbr`.
* @property-read string $standalone_abbreviated_months Shortcut to `months/stand-alone/abbreviated`.
* @property-read string $standalone_abbreviated_quarters Shortcut to `quarters/stand-alone/abbreviated`.
* @property-read string $standalone_narrow_days Shortcut to `days/stand-alone/narrow`.
* @property-read string $standalone_narrow_eras Shortcut to `eras/eraNarrow`.
* @property-read string $standalone_narrow_months Shortcut to `months/stand-alone/narrow`.
* @property-read string $standalone_narrow_quarters Shortcut to `quarters/stand-alone/narrow`.
* @property-read string $standalone_short_days Shortcut to `days/stand-alone/short`.
* @property-read string $standalone_short_eras Shortcut to `eras/eraAbbr`.
* @property-read string $standalone_short_months Shortcut to `months/stand-alone/abbreviated`.
* @property-read string $standalone_short_quarters Shortcut to `quarters/stand-alone/abbreviated`.
* @property-read string $standalone_wide_days Shortcut to `days/stand-alone/wide`.
* @property-read string $standalone_wide_eras Shortcut to `eras/eraNames`.
* @property-read string $standalone_wide_months Shortcut to `months/stand-alone/wide`.
* @property-read string $standalone_wide_quarters Shortcut to `quarters/stand-alone/wide`.
* @property-read string $abbreviated_days Shortcut to `days/format/abbreviated`.
* @property-read string $abbreviated_eras Shortcut to `eras/eraAbbr`.
* @property-read string $abbreviated_months Shortcut to `months/format/abbreviated`.
* @property-read string $abbreviated_quarters Shortcut to `quarters/format/abbreviated`.
* @property-read string $narrow_days Shortcut to `days/format/narrow`.
* @property-read string $narrow_eras Shortcut to `eras/eraNarrow`.
* @property-read string $narrow_months Shortcut to `months/format/narrow`.
* @property-read string $narrow_quarters Shortcut to `quarters/format/narrow`.
* @property-read string $short_days Shortcut to `days/format/short`.
* @property-read string $short_eras Shortcut to `eras/eraAbbr`.
* @property-read string $short_months Shortcut to `months/format/abbreviated`.
* @property-read string $short_quarters Shortcut to `quarters/format/abbreviated`.
* @property-read string $wide_days Shortcut to `days/format/wide`.
* @property-read string $wide_eras Shortcut to `eras/eraNames`.
* @property-read string $wide_months Shortcut to `months/format/wide`.
* @property-read string $wide_quarters Shortcut to `quarters/format/wide`.
*/
class Calendar extends \ArrayObject
{
const SHORTHANDS_REGEX = '#^(standalone_)?(abbreviated|narrow|short|wide)_(days|eras|months|quarters)$#';
const WIDTH_ABBR = 'abbreviated';
const WIDTH_NARROW = 'narrow';
const WIDTH_SHORT = 'short';
const WIDTH_WIDE = 'wide';
const ERA_NAMES = 'eraNames';
const ERA_ABBR = 'eraAbbr';
const ERA_NARROW = 'eraNarrow';
const CALENDAR_MONTHS = 'months';
const CALENDAR_DAYS = 'days';
const CALENDAR_QUARTERS = 'quarters';
const CALENDAR_ERAS = 'eras';
const CONTEXT_FORMAT = 'format';
const CONTEXT_STAND_ALONE = 'stand-alone';
use AccessorTrait;
use LocalePropertyTrait;
static private $era_widths_mapping = [
self::WIDTH_ABBR => self::ERA_ABBR,
self::WIDTH_NARROW => self::ERA_NARROW,
self::WIDTH_SHORT => self::ERA_ABBR,
self::WIDTH_WIDE => self::ERA_NAMES
];
/**
* @return DateTimeFormatter
*/
protected function lazy_get_datetime_formatter()
{
return new DateTimeFormatter($this);
}
/**
* @return DateFormatter
*/
protected function lazy_get_date_formatter()
{
return new DateFormatter($this);
}
/**
* @return TimeFormatter
*/
protected function lazy_get_time_formatter()
{
return new TimeFormatter($this);
}
/**
* @var ContextTransforms
*/
private $context_transforms;
/**
* @param Locale $locale
* @param array $data
*/
public function __construct(Locale $locale, array $data)
{
$this->locale = $locale;
$this->context_transforms = $locale->context_transforms;
$data = $this->transform_data($data);
parent::__construct($data);
}
/**
* @inheritdoc
*/
public function __get($property)
{
if (!preg_match(self::SHORTHANDS_REGEX, $property, $matches))
{
return $this->accessor_get($property);
}
list(, $standalone, $width, $type) = $matches;
$data = $this[$type];
if ($type === self::CALENDAR_ERAS)
{
return $this->$property = $data[self::$era_widths_mapping[$width]];
}
$data = $data[$standalone ? self::CONTEXT_STAND_ALONE : self::CONTEXT_FORMAT];
if ($width == self::WIDTH_SHORT && empty($data[$width]))
{
$width = self::WIDTH_ABBR;
}
return $this->$property = $data[$width];
}
/**
* Transforms calendar data according to context transforms rules.
*
* @param array $data
*
* @return array
*
* @uses transform_months
* @uses transform_days
* @uses transform_quarters
*/
private function transform_data(array $data)
{
static $transformable = [
self::CALENDAR_MONTHS,
self::CALENDAR_DAYS,
self::CALENDAR_QUARTERS
];
foreach ($transformable as $name)
{
array_walk($data[$name], function(array &$data, $context) use ($name) {
$is_stand_alone = self::CONTEXT_STAND_ALONE === $context;
array_walk($data, function (array &$names, $width) use ($name, $is_stand_alone) {
$names = $this->{ 'transform_' . $name }($names, $width, $is_stand_alone);
});
});
}
if (isset($data[self::CALENDAR_ERAS]))
{
array_walk($data[self::CALENDAR_ERAS], function(&$names, $width) {
$names = $this->transform_eras($names, $width);
});
}
return $data;
}
/**
* Transforms month names according to context transforms rules.
*
* @param array $names
* @param string $width
* @param bool $standalone
*
* @return string
*/
private function transform_months(array $names, $width, $standalone)
{
return $this->transform_months_or_days(
$names,
$width,
$standalone,
ContextTransforms::USAGE_MONTH_STANDALONE_EXCEPT_NARROW
);
}
/**
* Transforms day names according to context transforms rules.
*
* @param array $names
* @param string $width
* @param bool $standalone
*
* @return string
*/
private function transform_days($names, $width, $standalone)
{
return $this->transform_months_or_days(
$names,
$width,
$standalone,
ContextTransforms::USAGE_DAY_STANDALONE_EXCEPT_NARROW
);
}
/**
* Transforms day names according to context transforms rules.
*
* @param array $names
* @param string $width
* @param bool $standalone
* @param string $usage
*
* @return string
*/
private function transform_months_or_days($names, $width, $standalone, $usage)
{
if ($width == self::WIDTH_NARROW || !$standalone)
{
return $names;
}
return $this->apply_transform(
$names,
$usage,
ContextTransforms::TYPE_STAND_ALONE
);
}
/**
* Transforms era names according to context transforms rules.
*
* @param array $names
* @param string $width
*
* @return string
*/
private function transform_eras(array $names, $width)
{
switch ($width)
{
case self::ERA_ABBR:
return $this->apply_transform(
$names,
ContextTransforms::USAGE_ERA_ABBR,
ContextTransforms::TYPE_STAND_ALONE
);
case self::ERA_NAMES:
return $this->apply_transform(
$names,
ContextTransforms::USAGE_ERA_NAME,
ContextTransforms::TYPE_STAND_ALONE
);
case self::ERA_NARROW:
return $this->apply_transform(
$names,
ContextTransforms::USAGE_ERA_NARROW,
ContextTransforms::TYPE_STAND_ALONE
);
}
return $names; // @codeCoverageIgnore
}
/**
* Transforms quarters names according to context transforms rules.
*
* @param array $names
* @param string $width
* @param bool $standalone
*
* @return string
*/
private function transform_quarters(array $names, $width, $standalone)
{
if ($standalone)
{
if ($width != self::WIDTH_WIDE)
{
return $names;
}
return $this->apply_transform(
$names,
ContextTransforms::USAGE_QUARTER_STANDALONE_WIDE,
ContextTransforms::TYPE_STAND_ALONE
);
}
switch ($width)
{
case self::WIDTH_ABBR:
return $this->apply_transform(
$names,
ContextTransforms::USAGE_QUARTER_ABBREVIATED,
ContextTransforms::TYPE_STAND_ALONE
);
case self::WIDTH_WIDE:
return $this->apply_transform(
$names,
ContextTransforms::USAGE_QUARTER_FORMAT_WIDE,
ContextTransforms::TYPE_STAND_ALONE
);
case self::WIDTH_NARROW:
return $this->apply_transform(
$names,
ContextTransforms::USAGE_QUARTER_NARROW,
ContextTransforms::TYPE_STAND_ALONE
);
}
return $names; // @codeCoverageIgnore
}
/**
* Applies transformation to names.
*
* @param array $names
* @param string $usage
* @param string $type
*
* @return array
*/
private function apply_transform(array $names, $usage, $type)
{
$context_transforms = $this->context_transforms;
return array_map(function ($str) use ($context_transforms, $usage, $type) {
return $context_transforms->transform($str, $usage, $type);
}, $names);
}
}