Permalink
Browse files

Implement Exceptions, drop html test method

  • Loading branch information...
1 parent fdd564a commit 44a936fddb2d556426fc2ad0a4190585732a85db @CloCkWeRX committed Jul 1, 2012
Showing with 24 additions and 98 deletions.
  1. +5 −21 Numbers/Words.php
  2. +2 −0 Numbers/Words/Exception.php
  3. +0 −1 Numbers/Words/lang.id.php
  4. +2 −2 Numbers/Words/lang.pt_BR.php
  5. +15 −74 tests/test-numbers-words.php
View
@@ -25,6 +25,7 @@
*/
// {{{ Numbers_Words
+require_once 'Numbers/Words/Exception.php';
/**
* The Numbers_Words class provides method to convert arabic numerals to words.
@@ -80,13 +81,13 @@ function toWords($num, $locale = '', $options = array())
$classname = "Numbers_Words_${locale}";
if (!class_exists($classname)) {
- return Numbers_Words::raiseError("Unable to include the Numbers/Words/lang.${locale}.php file");
+ 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)) {
- return Numbers_Words::raiseError("Unable to find _toWords method in '$classname' class");
+ throw new Numbers_Words_Exception("Unable to find _toWords method in '$classname' class");
}
if (!is_int($num)) {
@@ -164,13 +165,13 @@ function toCurrency($num, $locale = 'en_US', $int_curr = '')
$classname = "Numbers_Words_${locale}";
if (!class_exists($classname)) {
- return Numbers_Words::raiseError("Unable to include the Numbers/Words/lang.${locale}.php file");
+ 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)) {
- return Numbers_Words::raiseError("Unable to find toCurrencyWords method in '$classname' class");
+ throw new Numbers_Words_Exception("Unable to find toCurrencyWords method in '$classname' class");
}
@$obj = new $classname;
@@ -263,23 +264,6 @@ function getLocales($locale = null)
return $ret;
}
// }}}
-
- // {{{ raiseError()
- /**
- * Trigger a PEAR error
- *
- * To improve performances, the PEAR.php file is included dynamically.
- *
- * @param string $msg error message
- *
- * @return PEAR_Error
- */
- function raiseError($msg)
- {
- include_once 'PEAR.php';
- return PEAR::raiseError($msg);
- }
- // }}}
}
// }}}
@@ -0,0 +1,2 @@
+<?php
+class Numbers_Words_Exception extends Exception {}
@@ -23,7 +23,6 @@
* @link http://pear.php.net/package/Numbers_Words
*/
-require_once "PEAR.php";
require_once "Numbers/Words.php";
/**
@@ -270,7 +270,7 @@ function _toWords($num)
* Testing Range
*/
if (!array_key_exists($index, $this->_exponent)) {
- return Numbers_Words::raiseError('Number out of range.');
+ throw new Numbers_Words_Exception('Number out of range.');
}
/**
@@ -489,7 +489,7 @@ function toCurrencyWords($int_curr, $decimal, $fraction = false, $convert_fracti
* Testing Range
*/
if ($num < 0 || $num > 99) {
- return Numbers_Words::raiseError('Fraction out of range.');
+ throw new Numbers_Words_Exception('Fraction out of range.');
}
/**
@@ -3,7 +3,7 @@
/**
* Numbers_Words test script
*
- * PHP versions 4 and 5
+ * PHP version 5
*
* LICENSE: This source file is subject to version 3.01 of the PHP license
* that is available through the world-wide-web at the following URI:
@@ -20,24 +20,13 @@
* @link http://pear.php.net/package/Numbers_Words
*/
/* vim: set expandtab tabstop=4 shiftwidth=4: */
-
-error_reporting(E_ALL);
-
-require_once('PEAR.php');
-require_once('Numbers/Words.php');
+require_once 'Numbers/Words.php';
$num = "1121771141";
-$html_on = 0;
-if (isset($_REQUEST)) {
- $html_on = 1;
-}
-if (isset($_REQUEST) && is_array($_REQUEST) && isset($_REQUEST['num'])) {
- $num = $_REQUEST['num'];
-} elseif (isset($argv) && is_array($argv) && isset($argv[1])) {
+if (isset($argv) && is_array($argv) && isset($argv[1])) {
$num = $argv[1];
}
-$html_on = 0;
$shownum = $num;
@@ -48,26 +37,11 @@
$shownum = preg_replace("/(...)/", "\\1 ", $shownum);
$shownum = preg_replace("/ $/", "", $shownum);
-if ($html_on)
- echo "<center>Test number: <b><u>$shownum</u></b></center><p>\n";
-else
- echo sprintf("%42s", 'Test number: ') . $shownum . "\n\n";
+echo sprintf("%42s", 'Test number: ') . $shownum . "\n\n";
$lang = Numbers_Words::getLocales();
$langs = array();
-if ($html_on) {
-?>
-<center>
-<table width="100%" border="1" cellspacing="0">
-<tr>
- <th width="50">Symbol</th>
- <th width="100">Number system</th>
- <th width="100%">String</th>
-</tr>
-<?
-}
-
foreach ($lang as $loc_symbol) {
$classname = "Numbers_Words_" . $loc_symbol;
@include_once("Numbers/Words/lang.${loc_symbol}.php");
@@ -78,26 +52,14 @@
foreach ($lang as $loc_symbol) {
$classname = "Numbers_Words_" . $loc_symbol;
$obj =& new $classname;
- $ret = $obj->toWords($num);
- if (PEAR::isError($ret)) {
- if ($html_on) {
- }
- echo "Error ($loc_symbol): " . $ret->message . "\n";
- if ($html_on) {
- }
- } else {
+
+ try {
+ $ret = $obj->toWords($num);
$loc_name = $obj->lang;
$langs[$loc_symbol] = $loc_name;
- if ($html_on) {
- ?>
- <tr>
- <td align="center"><?php echo $loc_symbol; ?></td>
- <td><nobr><i><?php echo $loc_name; ?></i></nobr></td>
- <td><b><?php echo $ret; ?></b></td>
- </tr><?
- } else {
- echo sprintf("%30s: '", $loc_name . ' (' . $loc_symbol . ')') . $ret . "'\n";
- }
+ echo sprintf("%30s: '", $loc_name . ' (' . $loc_symbol . ')') . $ret . "'\n";
+ } catch (Numbers_Words_Exception $nwe) {
+ echo (string)$nwe . "\n";
}
}
@@ -107,32 +69,11 @@
$handle = new Numbers_Words();
while (list ($loc_symbol, $loc_name) = each ($langs)) {
- $ret = $handle->toCurrency($num, $loc_symbol);
- if (PEAR::isError($ret)) {
- if ($html_on) {
- }
- echo "Error ($loc_symbol): " . $ret->message . "\n";
- if ($html_on) {
- }
- } else {
- if ($html_on) {
- ?>
- <tr>
- <td align="center"><?php echo $loc_symbol; ?></td>
- <td><nobr><i><?php echo $loc_name; ?></i></nobr></td>
- <td><b><?php echo $ret; ?></b></td>
- </tr><?
- } else {
- echo sprintf("%30s: ", $loc_name . ' (' . $loc_symbol . ')') . $ret . "\n";
- }
+ try {
+ $ret = $handle->toCurrency($num, $loc_symbol);
+ echo sprintf("%30s: ", $loc_name . ' (' . $loc_symbol . ')') . $ret . "\n";
+ } catch (Numbers_Words_Exception $nwe) {
+ echo (string)$nwe . "\n";
}
}
-if ($html_on) {
-?>
-</table>
-</center>
-<?php
-}
-
-?>

0 comments on commit 44a936f

Please sign in to comment.