Skip to content

Commit

Permalink
Implement Exceptions, drop html test method
Browse files Browse the repository at this point in the history
  • Loading branch information
CloCkWeRX committed Jul 1, 2012
1 parent fdd564a commit 44a936f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 98 deletions.
26 changes: 5 additions & 21 deletions Numbers/Words.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*/

// {{{ Numbers_Words
require_once 'Numbers/Words/Exception.php';

/**
* The Numbers_Words class provides method to convert arabic numerals to words.
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
// }}}
}

// }}}
2 changes: 2 additions & 0 deletions Numbers/Words/Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
class Numbers_Words_Exception extends Exception {}
1 change: 0 additions & 1 deletion Numbers/Words/lang.id.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
* @link http://pear.php.net/package/Numbers_Words
*/

require_once "PEAR.php";
require_once "Numbers/Words.php";

/**
Expand Down
4 changes: 2 additions & 2 deletions Numbers/Words/lang.pt_BR.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}

/**
Expand Down Expand Up @@ -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.');
}

/**
Expand Down
89 changes: 15 additions & 74 deletions tests/test-numbers-words.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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;

Expand All @@ -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");
Expand All @@ -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";
}
}

Expand All @@ -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.