Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring and Tidying
Browse files Browse the repository at this point in the history
olvlvl committed Jan 30, 2015
1 parent d8a04c5 commit 6dfc758
Showing 12 changed files with 236 additions and 176 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# CLDR [![Build Status](https://travis-ci.org/ICanBoogie/CLDR.svg?branch=master)](https://travis-ci.org/ICanBoogie/CLDR)
# CLDR

[![Build Status][]](https://travis-ci.org/ICanBoogie/CLDR) [![Code Quality][]](https://scrutinizer-ci.com/g/ICanBoogie/CLDR/?branch=master)

The __CLDR__ package provides means to internationalize your application by leveraging the
data and conventions defined by the [Unicode Common Locale Data Repository](http://cldr.unicode.org/) (CLDR).
@@ -54,14 +56,14 @@ provide a client instance.
<?php

use ICanBoogie\CLDR\FileProvider;
use ICanBoogie\CLDR\PredisProvider;
use ICanBoogie\CLDR\RedisProvider;
use ICanBoogie\CLDR\Repository;
use ICanBoogie\CLDR\RunTimeProvider;
use ICanBoogie\CLDR\WebProvider;

$provider = new RunTimeProvider
(
new PredisProvider
new RedisProvider
(
new FileProvider
(
@@ -707,7 +709,7 @@ clean the directory with the `make clean` command.

The package is continuously tested by [Travis CI](http://about.travis-ci.org/).

[![Build Status](https://travis-ci.org/ICanBoogie/CLDR.svg?branch=master)](https://travis-ci.org/ICanBoogie/CLDR)
[![Build Status][]](https://travis-ci.org/ICanBoogie/CLDR)



@@ -721,14 +723,18 @@ ICanBoogie/CLDR is licensed under the New BSD License - See the [LICENSE](LICENS



[Build Status]: https://travis-ci.org/ICanBoogie/CLDR.svg?branch=master
[Code Quality]: https://scrutinizer-ci.com/g/ICanBoogie/CLDR/badges/quality-score.png?b=master
[CLDR]: http://www.unicode.org/repos/cldr-aux/json/26/
[I18n library]: https://github.com/ICanBoogie/I18n
[ICanBoogie]: https://github.com/ICanBoogie/ICanBoogie
[Calendar]: http://icanboogie.org/docs/class-ICanBoogie.CLDR.Calendar.html
[Currency]: http://icanboogie.org/docs/class-ICanBoogie.CLDR.Currency.html
[FileProvider]: http://icanboogie.org/docs/class-ICanBoogie.CLDR.FileProvider.html
[Repository]: http://icanboogie.org/docs/class-ICanBoogie.CLDR.Repository.html
[ListFormatter]: http://icanboogie.org/docs/class-ICanBoogie.CLDR.ListFormatter.html
[Locale]: http://icanboogie.org/docs/class-ICanBoogie.CLDR.Locale.html
[LocalizationAwareInterface]: http://icanboogie.org/docs/class-ICanBoogie.CLDR.LocalizationAwareInterface.html
[LocalizedDateTime]: http://icanboogie.org/docs/class-ICanBoogie.CLDR.LocalizedDateTime.html
[NumberFormatter]: http://icanboogie.org/docs/class-ICanBoogie.CLDR.NumberFormatter.html
[Territory]: http://icanboogie.org/docs/class-ICanBoogie.CLDR.Territory.html
4 changes: 0 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -27,10 +27,6 @@
"icanboogie/datetime": ">=1.0"
},

"require-dev": {
"predis/predis": "~1.0"
},

"autoload": {
"psr-4": {
"ICanBoogie\\CLDR\\": "lib"
34 changes: 16 additions & 18 deletions lib/Calendar.php
Original file line number Diff line number Diff line change
@@ -57,6 +57,15 @@ class Calendar extends \ArrayObject
use AccessorTrait;
use LocalePropertyTrait;

static private $era_translation = [

'abbreviated' => 'eraAbbr',
'narrow' => 'eraNarrow',
'short' => 'eraAbbr',
'wide' => 'eraNames'

];

/**
* @return DateTimeFormatter
*/
@@ -90,15 +99,6 @@ public function __construct(Locale $locale, array $data)

public function __get($property)
{
static $era_translation = [

'abbreviated' => 'eraAbbr',
'narrow' => 'eraNarrow',
'short' => 'eraAbbr',
'wide' => 'eraNames'

];

if (preg_match('#^(standalone_)?(abbreviated|narrow|short|wide)_(days|eras|months|quarters)$#', $property, $matches))
{
list(, $standalone, $width, $type) = $matches;
@@ -107,19 +107,17 @@ public function __get($property)

if ($type == 'eras')
{
return $this->$property = $data[$era_translation[$width]];
return $this->$property = $data[self::$era_translation[$width]];
}
else
{
$data = $data[$standalone ? 'stand-alone' : 'format'];

if ($width == 'short' && empty($data[$width]))
{
$width = 'abbreviated';
}
$data = $data[$standalone ? 'stand-alone' : 'format'];

return $this->$property = $data[$width];
if ($width == 'short' && empty($data[$width]))
{
$width = 'abbreviated';
}

return $this->$property = $data[$width];
}

return $this->__object_get($property);
13 changes: 2 additions & 11 deletions lib/DateFormatter.php
Original file line number Diff line number Diff line change
@@ -41,19 +41,10 @@ class DateFormatter extends DateTimeFormatter
/**
* Resolves widths defined in `dateFormats` (full, long, medium, short) into a pattern.
*
* @param string $pattern_or_width_or_skeleton
*
* @return string
* @inheritdoc
*/
protected function resolve_pattern($pattern_or_width_or_skeleton)
{
static $widths = [ 'full', 'long', 'medium', 'short' ];

if (in_array($pattern_or_width_or_skeleton, $widths))
{
return $this->calendar['dateFormats'][$pattern_or_width_or_skeleton];
}

return parent::resolve_pattern($pattern_or_width_or_skeleton);
return parent::resolve_pattern($this->resolve_width($pattern_or_width_or_skeleton, 'dateFormats'));
}
}
20 changes: 20 additions & 0 deletions lib/DateTimeFormatter.php
Original file line number Diff line number Diff line change
@@ -253,6 +253,26 @@ protected function resolve_pattern($pattern_or_width_or_skeleton)
return $pattern;
}

/**
* Resolves widths (full, long, medium, short) into a pattern.
*
* @param string $pattern_or_width_or_skeleton
* @param string $from Width Source e.g. "timeFormats".
*
* @return string
*/
protected function resolve_width($pattern_or_width_or_skeleton, $from)
{
static $widths = [ 'full', 'long', 'medium', 'short' ];

if (in_array($pattern_or_width_or_skeleton, $widths))
{
return $this->calendar[$from][$pattern_or_width_or_skeleton];
}

return $pattern_or_width_or_skeleton;
}

/*
* era (G)
*/
20 changes: 7 additions & 13 deletions lib/LocalizedDateTime.php
Original file line number Diff line number Diff line change
@@ -44,6 +44,8 @@
*/
class LocalizedDateTime extends LocalizedObjectWithFormatter
{
static private $format_widths = [ 'full', 'long', 'medium', 'short' ];

/**
* Returns the formatter.
*
@@ -56,13 +58,9 @@ protected function lazy_get_formatter()

public function __get($property)
{
switch ($property)
if (strpos($property, 'as_') === 0 && in_array($width = substr($property, 3), self::$format_widths))
{
case 'as_full':
case 'as_long':
case 'as_medium':
case 'as_short':
return call_user_func([ $this, 'format_' . $property ]);
return $this->{ 'format_as_' . $width }();
}

try
@@ -82,13 +80,9 @@ public function __set($property, $value)

public function __call($method, $arguments)
{
switch ($method)
if (strpos($method, 'format_as_') === 0 && in_array($width = substr($method, 10), self::$format_widths))
{
case 'format_as_full':
case 'format_as_long':
case 'format_as_medium':
case 'format_as_short':
return $this->format(substr($method, 10));
return $this->format($width);
}

return call_user_func_array([ $this->target, $method ], $arguments);
@@ -106,7 +100,7 @@ public function __toString()
*
* @return string
*/
public function format($pattern=null)
public function format($pattern = null)
{
return $this->formatter->format($this->target, $pattern);
}
80 changes: 20 additions & 60 deletions lib/NumberFormatter.php
Original file line number Diff line number Diff line change
@@ -21,8 +21,19 @@ class NumberFormatter
use AccessorTrait;
use RepositoryPropertyTrait;

static private $default_symbols = [

'decimal' => ".",
'group' => ",",
'percentSign' => "%",
'plusSign' => "+",
'minusSign' => "-",
'perMille' => ""

];

/**
* Return the precision of a number.
* Returns the precision of a number.
*
* @param $number
*
@@ -96,78 +107,27 @@ public function __construct(Repository $repository=null)
*
* @return string The formatted number.
*/
public function format($number, $pattern, array $symbols=[])
public function format($number, $pattern, array $symbols = [])
{
if (!($pattern instanceof NumberPattern))
{
$pattern = NumberPattern::from($pattern);
}

$symbols += [

'decimal' => ".",
'group' => ",",
'percentSign' => "%",
'plusSign' => "+",
'minusSign' => "-",
'perMille' => ""

];

#

$negative = $number < 0;
$number = abs($number * $pattern->multiplier);

if ($pattern->max_decimal_digits >= 0)
{
$number = round($number, $pattern->max_decimal_digits);
}

$number = "$number";

if (($pos = strpos($number, '.')) !== false)
{
$integer = substr($number, 0, $pos);
$decimal = substr($number, $pos + 1);
}
else
{
$integer = $number;
$decimal = '';
}

if ($pattern->decimal_digits > strlen($decimal))
{
$decimal = str_pad($decimal, $pattern->decimal_digits, '0');
}
$symbols += self::$default_symbols;

if (strlen($decimal))
{
$decimal = $symbols['decimal'] . $decimal;
}

$integer = str_pad($integer, $pattern->integer_digits, '0', STR_PAD_LEFT);
$group_size1 = $pattern->group_size1;
list($integer, $decimal) = $pattern->parse_number($number);

if ($group_size1 > 0 && strlen($integer) > $pattern->group_size1)
{
$group_size2 = $pattern->group_size2;

$str1 = substr($integer, 0, -$group_size1);
$str2 = substr($integer, -$group_size1);
$size = $group_size2 > 0 ? $group_size2 : $group_size1;
$str1 = str_pad($str1, (int) ((strlen($str1) + $size - 1) / $size) * $size, ' ', STR_PAD_LEFT);
$integer = ltrim(implode($symbols['group'], str_split($str1, $size))) . $symbols['group'] . $str2;
}
$formatted_integer = $pattern->format_integer_with_group($integer, $symbols['group']);
$formatted_number = $pattern->format_integer_with_decimal($formatted_integer, $decimal, $symbols['decimal']);

if ($negative)
if ($number < 0)
{
$number = $pattern->negative_prefix . $integer . $decimal . $pattern->negative_suffix;
$number = $pattern->negative_prefix . $formatted_number . $pattern->negative_suffix;
}
else
{
$number = $pattern->positive_prefix . $integer . $decimal . $pattern->positive_suffix;
$number = $pattern->positive_prefix . $formatted_number . $pattern->positive_suffix;
}

return strtr($number, [
Loading

0 comments on commit 6dfc758

Please sign in to comment.