Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug #19855, unify locale loading code #4

Merged
merged 3 commits into from Mar 25, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
66 changes: 39 additions & 27 deletions Numbers/Words.php
Expand Up @@ -68,27 +68,15 @@ class Numbers_Words
*/
function toWords($num, $locale = '', $options = array())
{
if (empty($locale)) {
if (empty($locale) && isset($this) && $this instanceof Numbers_Words) {
$locale = $this->locale;
}

if (empty($locale)) {
$locale = 'en_US';
}

require_once "Numbers/Words/lang.${locale}.php";

$classname = "Numbers_Words_${locale}";

if (!class_exists($classname)) {
throw new Numbers_Words_Exception("Unable to include the Numbers/Words/lang.${locale}.php file");
}

$methods = get_class_methods($classname);

if (!in_array('_toWords', $methods) && !in_array('_towords', $methods)) {
throw new Numbers_Words_Exception("Unable to find _toWords method in '$classname' class");
}
$classname = self::loadLocale($locale, '_toWords');

if (!is_int($num)) {
// cast (sanitize) to int without losing precision
Expand Down Expand Up @@ -160,19 +148,7 @@ function toCurrency($num, $locale = 'en_US', $int_curr = '')
{
$ret = $num;

@include_once "Numbers/Words/lang.${locale}.php";

$classname = "Numbers_Words_${locale}";

if (!class_exists($classname)) {
throw new Numbers_Words_Exception("Unable to include the Numbers/Words/lang.${locale}.php file");
}

$methods = get_class_methods($classname);

if (!in_array('toCurrencyWords', $methods) && !in_array('tocurrencywords', $methods)) {
throw new Numbers_Words_Exception("Unable to find toCurrencyWords method in '$classname' class");
}
$classname = self::loadLocale($locale, 'toCurrencyWords');

@$obj = new $classname;

Expand Down Expand Up @@ -264,6 +240,42 @@ function getLocales($locale = null)
return $ret;
}
// }}}

/**
* Load the given locale and return class name
*
* @param string $locale Locale key, e.g. "de" or "en_US"
* @param string $requiredMethod Method that this class needs to have
*
* @return string Locale class name
*
* @throws Numbers_Words_Exception When the class cannot be loaded
*/
function loadLocale($locale, $requiredMethod)
{
$classname = "Numbers_Words_${locale}";
if (!class_exists($classname)) {
@include_once "Numbers/Words/lang.${locale}.php";
}

if (!class_exists($classname)) {
throw new Numbers_Words_Exception(
"Unable to include the Numbers/Words/lang.${locale}.php file"
);
}

$methods = get_class_methods($classname);

if (!in_array($requiredMethod, $methods)
&& !in_array($requiredMethod, $methods)
) {
throw new Numbers_Words_Exception(
"Unable to find method '$requiredMethod' in class '$classname'"
);
}

return $classname;
}
}

// }}}
40 changes: 40 additions & 0 deletions tests/Numbers_WordsTest.php
@@ -0,0 +1,40 @@
<?php
require_once 'Numbers/Words.php';

class Numbers_WordsTest extends PHPUnit_Framework_TestCase
{
function testToWordsStatic()
{
error_reporting(error_reporting() & ~E_STRICT);
$this->assertEquals('one', Numbers_Words::toWords(1));
}

function testToWordsObjectLocale()
{
$nw = new Numbers_Words();
$nw->locale = 'de';
$this->assertEquals('eins', $nw->toWords(1));
}

/**
* @expectedException Numbers_Words_Exception
* @expectedExceptionMessage Unable to include the Numbers/Words/lang.doesnotexist.php file
*/
function testToWordsInvalidLocale()
{
$nw = new Numbers_Words();
$nw->toWords(1, 'doesnotexist');
}

/**
* @expectedException Numbers_Words_Exception
* @expectedExceptionMessage Unable to include the Numbers/Words/lang.doesnotexist.php file
*/
function testToCurrencyInvalidLocale()
{
$nw = new Numbers_Words();
$nw->toCurrency(1, 'doesnotexist');
}
}

?>
8 changes: 8 additions & 0 deletions tests/bootstrap.php
@@ -0,0 +1,8 @@
<?php
if (is_dir(__DIR__ . '/../Numbers')) {
set_include_path(
__DIR__ . '/../' . PATH_SEPARATOR . get_include_path()
);
}

?>
12 changes: 12 additions & 0 deletions tests/phpunit.xml
@@ -0,0 +1,12 @@
<phpunit
colors="true"
bootstrap="bootstrap.php"
verbose="true"
strict="true"
>
<filter>
<whitelist>
<directory suffix=".php">../Numbers/</directory>
</whitelist>
</filter>
</phpunit>