Skip to content

Loading…

fix bug #19855, unify locale loading code #4

Merged
merged 3 commits into from

2 participants

Showing with 99 additions and 27 deletions.
  1. +39 −27 Numbers/Words.php
  2. +40 −0 tests/Numbers_WordsTest.php
  3. +8 −0 tests/bootstrap.php
  4. +12 −0 tests/phpunit.xml
View
66 Numbers/Words.php
@@ -68,7 +68,7 @@ class Numbers_Words
*/
function toWords($num, $locale = '', $options = array())
{
- if (empty($locale)) {
+ if (empty($locale) && isset($this) && $this instanceof Numbers_Words) {
$locale = $this->locale;
}
@@ -76,19 +76,7 @@ function toWords($num, $locale = '', $options = array())
$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
@@ -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;
@@ -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;
+ }
}
// }}}
View
40 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');
+ }
+}
+
+?>
View
8 tests/bootstrap.php
@@ -0,0 +1,8 @@
+<?php
+if (is_dir(__DIR__ . '/../Numbers')) {
+ set_include_path(
+ __DIR__ . '/../' . PATH_SEPARATOR . get_include_path()
+ );
+}
+
+?>
View
12 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>
Something went wrong with that request. Please try again.