Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Sep 15, 2016
1 parent 64d1a15 commit e1e79d4
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 30 deletions.
91 changes: 78 additions & 13 deletions lib/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,27 @@ class Calendar extends \ArrayObject
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_translation = [
static private $era_widths_mapping = [

self::WIDTH_ABBR => 'eraAbbr',
self::WIDTH_NARROW => 'eraNarrow',
self::WIDTH_SHORT => 'eraAbbr',
self::WIDTH_WIDE => 'eraNames'
self::WIDTH_ABBR => self::ERA_ABBR,
self::WIDTH_NARROW => self::ERA_NARROW,
self::WIDTH_SHORT => self::ERA_ABBR,
self::WIDTH_WIDE => self::ERA_NAMES

];

Expand Down Expand Up @@ -99,13 +111,21 @@ 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);
}
Expand All @@ -124,19 +144,64 @@ public function __get($property)

$data = $this[$type];

if ($type == 'eras')
if ($type === self::CALENDAR_ERAS)
{
return $this->$property = $data[self::$era_translation[$width]];
return $this->$property = $data[self::$era_widths_mapping[$width]];
}

$data = $data[$standalone ? 'stand-alone' : 'format'];
$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 = $this->{ "transform_$type" }($data[$width], $width, $standalone);
return $this->$property = $data[$width];
}

/**
* Transforms calendar data according to context transforms rules.
*
* @param array $data
*
* @return array
*/
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;
}

/**
Expand Down Expand Up @@ -174,23 +239,23 @@ private function transform_eras(array $names, $width)
{
switch ($width)
{
case self::WIDTH_ABBR:
case self::ERA_ABBR:

return $this->apply_transform(
$names,
ContextTransforms::USAGE_ERA_ABBR,
ContextTransforms::TYPE_STAND_ALONE
);

case self::WIDTH_WIDE:
case self::ERA_NAMES:

return $this->apply_transform(
$names,
ContextTransforms::USAGE_ERA_NAME,
ContextTransforms::TYPE_STAND_ALONE
);

case self::WIDTH_NARROW:
case self::ERA_NARROW:

return $this->apply_transform(
$names,
Expand Down Expand Up @@ -292,7 +357,7 @@ private function transform_quarters(array $names, $width, $standalone)
*/
private function apply_transform(array $names, $usage, $type)
{
$context_transforms = $this->locale->context_transforms;
$context_transforms = $this->context_transforms;

return array_map(function ($str) use ($context_transforms, $usage, $type) {

Expand Down
19 changes: 2 additions & 17 deletions lib/DateTimeFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,11 +453,7 @@ protected function format_standalone_month(DateTimeAccessor $datetime, $length)
case 5: return $this->calendar->standalone_narrow_months[$month];
}

return $this->calendar->locale->context_transform(
$str,
ContextTransforms::USAGE_MONTH_FORMAT_EXCEPT_NARROW,
ContextTransforms::TYPE_STAND_ALONE
);
return $str;
}

/*
Expand Down Expand Up @@ -645,19 +641,8 @@ protected function format_day_in_week_stand_alone(DateTimeAccessor $datetime, $l
}

$code = $this->resolve_day_code($day);
$calendar = $this->calendar;
$str = $calendar->{ 'standalone_' . $mapping[$length] . '_days' }[$code];

if ($length != 5)
{
return $calendar->locale->context_transform(
$str,
ContextTransforms::USAGE_DAY_STANDALONE_EXCEPT_NARROW,
ContextTransforms::TYPE_STAND_ALONE
);
}

return $str;
return $this->calendar->{ 'standalone_' . $mapping[$length] . '_days' }[$code];
}

/**
Expand Down

0 comments on commit e1e79d4

Please sign in to comment.