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

support of ->toCurrencyWords() for LV #26

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions Numbers/Words.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ class Numbers_Words
*/
public $decimalPoint = '.';

/**
* Use abbreviation as decimal name
* @var boolean
* @access public
*/
static public $useAbbrAsDecimalNames = false;

// }}}
// {{{ toWords()

Expand Down Expand Up @@ -189,10 +196,10 @@ function toCurrency($num, $locale = 'en_US', $intCurr = '', $decimalPoint = null
} elseif ($len > 2) {
// get the 3rd digit after the comma
$round_digit = substr($currency[1], 2, 1);

// cut everything after the 2nd digit
$currency[1] = substr($currency[1], 0, 2);

if ($round_digit >= 5) {
// round up without losing precision
include_once "Math/BigInteger.php";
Expand Down Expand Up @@ -278,13 +285,13 @@ public static function getLocales($locales = null)
public static function loadLocale($locale, $requiredMethod)
{
$classname = 'Numbers_Words_Locale_' . $locale;
if (!class_exists($classname)) {
if (!class_exists($classname, false)) {
$file = str_replace('_', '/', $classname) . '.php';
if (stream_resolve_include_path($file)) {
include_once $file;
}

if (!class_exists($classname)) {
if (!class_exists($classname, false)) {
throw new Numbers_Words_Exception(
'Unable to load locale class ' . $classname
);
Expand Down
42 changes: 26 additions & 16 deletions Numbers/Words/Locale/en/US.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ class Numbers_Words_Locale_en_US extends Numbers_Words
'RUB' => array(array('Russian Federation rouble'), array('kopiejka')),
'SEK' => array(array('Swedish krona'), array('oere')),
'SIT' => array(array('Tolar'), array('stotinia')),
'SKK' => array(array('Slovak koruna'), array()),
'SKK' => array(array('Slovak koruna'), array('halier')),
'TRL' => array(array('lira'), array('kuru�')),
'UAH' => array(array('hryvna'), array('cent')),
'USD' => array(array('dollar'), array('cent')),
Expand Down Expand Up @@ -482,23 +482,33 @@ function _toWords($num, $power = 0, $powsuffix = '')
*/
function toCurrencyWords($int_curr, $decimal, $fraction = false, $convert_fraction = true)
{
$int_curr = strtoupper($int_curr);
if (!isset($this->_currency_names[$int_curr])) {
$int_curr = $this->def_currency;
}
$curr_names = $this->_currency_names[$int_curr];
if (is_array($int_curr))
$curr_names = $int_curr;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tabs vs spaces - run PHPCS

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

else
{
$int_curr = strtoupper($int_curr);
if (!isset($this->_currency_names[$int_curr])) {
$int_curr = $this->def_currency;
}
$curr_names = $this->_currency_names[$int_curr];
}

$ret = trim($this->_toWords($decimal));
$lev = ($decimal == 1) ? 0 : 1;
if ($lev > 0) {
if (count($curr_names[0]) > 1) {
$ret .= $this->_sep . $curr_names[0][$lev];
} else {
$ret .= $this->_sep . $curr_names[0][0] . 's';
}
} else {
$ret .= $this->_sep . $curr_names[0][0];
}
if (Numbers_Words::$useAbbrAsDecimalNames)
$ret .= $this->_sep . $int_curr;
else
{
$lev = ($decimal == 1) ? 0 : 1;
if ($lev > 0) {
if (count($curr_names[0]) > 1) {
$ret .= $this->_sep . $curr_names[0][$lev];
} else {
$ret .= $this->_sep . $curr_names[0][0] . 's';
}
} else {
$ret .= $this->_sep . $curr_names[0][0];
}
}

if ($fraction !== false) {
if ($convert_fraction) {
Expand Down
119 changes: 119 additions & 0 deletions Numbers/Words/Locale/lv.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,56 @@ class Numbers_Words_Locale_lv extends Numbers_Words
*/
var $_sep = ' ';

/**
* The currency names (based on the below links,
* informations from central bank websites and on encyclopedias)
*
* @var array
* @link http://www.jhall.demon.co.uk/currency/by_abbrev.html World currencies
* @link http://www.rusimpex.ru/Content/Reference/Refinfo/valuta.htm Foreign currencies names
* @link http://www.cofe.ru/Finance/money.asp Currencies names
* @access private
*/
// TODO: recheck
var $_currency_names = array(
'ALL' => array(array('lek'), array('qindarka')),
'AUD' => array(array('Austrālijas dolārs', 'Austrālijas dolāri'), array('cents', 'centi')),
'BAM' => array(array('konvertējamas marka'), array('fenig')),
'BGN' => array(array('lev'), array('stotinka')),
'BRL' => array(array('real'), array('centavos')),
'BYR' => array(array('Baltkrievijas rublis', 'Baltkrievijas rubļi'), array('kapeika', 'kapeikās')),
'CAD' => array(array('Kanādas dolārs', 'Kanādas dolāri'), array('cents', 'centi')),
'CHF' => array(array('Šveices franks', 'Šveices franki'), array('rapp')),
'CYP' => array(array('Kipras mārciņa', 'Kipras mārciņas'), array('cents', 'centi')),
'CZK' => array(array('Čehijas krona', 'Čehijas kronās'), array('halerz')),
'DKK' => array(array('Dāniska krona', 'Dāniskas kronas'), array('ore')),
'EEK' => array(array('kroon'), array('senti')),
'EUR' => array(array('eiro'), array('eiro-cents', 'eiro-centi')),
'GBP' => array(array('mārciņa', 'mārciņas'), array('pensi')),
'HKD' => array(array('Honkongas dolārs', 'Honkongas dolāri'), array('cent')),
'HRK' => array(array('Horvātijas kuna', 'Horvātijas kunas'), array('lipa')),
'HUF' => array(array('forint'), array('filler')),
'ILS' => array(array('jauna šekelis','jauni šekeli'), array('agora','agorot')),
'ISK' => array(array('Islandes krona', 'Islandes kronas'), array('aurar')),
'JPY' => array(array('yen'), array('sen')),
'LTL' => array(array('lit'), array('cents', 'centi')),
'LVL' => array(array('lat'), array('sentim')),
'MKD' => array(array('Maķedonijas dinārs', 'Maķedonijas dināri'), array('deni')),
'MTL' => array(array('Maltas lira', 'Maltas liras'), array('centym')),
'NOK' => array(array('Norvēģiska krona', 'Norvēģiskas kronas'), array('oere')),
'PLN' => array(array('zlot'), array('grosz')),
'ROL' => array(array('Rumānijas leja', 'Rumānijas lejas'), array('bani')),
'RUB' => array(array('Krievijas rublis', 'Krievijas rubļi'), array('kapeika', 'kapeikās')),
'SEK' => array(array('Zviedru krona', 'Zviedru kronas'), array('oere')),
'SIT' => array(array('Tolars', 'Tolari'), array('stotinia')),
'SKK' => array(array('Slovākijas krona', 'Slovākijas kronas'), array('halier')),
'TRL' => array(array('lira'), array('kuruю')),
'UAH' => array(array('hryvna'), array('cents', 'centi')),
'USD' => array(array('dolārs'), array('cents', 'centi')),
'YUM' => array(array('dinārs', 'dinārsi'), array('para')),
'ZAR' => array(array('rand'), array('cents', 'centi'))
);

/**
* The default currency name
* @var string
Expand Down Expand Up @@ -315,5 +365,74 @@ function _toWords($num, $power = 0, $powsuffix = '')
return $ret;
}
// }}}
// {{{ toCurrencyWords()

/**
* Converts a currency value to its word representation
* (with monetary units) in Latvian language
*
* @param integer $int_curr An international currency symbol
* as defined by the ISO 4217 standard (three characters)
* @param integer $decimal A money total amount without fraction part (e.g. amount of dollars)
* @param integer $fraction Fractional part of the money amount (e.g. amount of cents)
* Optional. Defaults to false.
* @param integer $convert_fraction Convert fraction to words (left as numeric if set to false).
* Optional. Defaults to true.
*
* @return string The corresponding word representation for the currency
*
* @access public
* @author MaxB
*/
function toCurrencyWords($int_curr, $decimal, $fraction = false, $convert_fraction = true)
{
if (is_array($int_curr))
$curr_names = $int_curr;
else
{
$int_curr = strtoupper($int_curr);
if (!isset($this->_currency_names[$int_curr])) {
$int_curr = $this->def_currency;
}
$curr_names = $this->_currency_names[$int_curr];
}

$ret = trim($this->_toWords($decimal));
if (Numbers_Words::$useAbbrAsDecimalNames)
$ret .= $this->_sep . $int_curr;
else
{
$lev = ($decimal == 1) ? 0 : 1;
if ($lev > 0) {
if (count($curr_names[0]) > 1) {
$ret .= $this->_sep . $curr_names[0][$lev];
} else {
$ret .= $this->_sep . $curr_names[0][0];
}
} else {
$ret .= $this->_sep . $curr_names[0][0];
}
}

if ($fraction !== false) {
if ($convert_fraction) {
$ret .= $this->_sep . trim($this->_toWords($fraction));
} else {
$ret .= $this->_sep . $fraction;
}
$lev = ($fraction == 1) ? 0 : 1;
if ($lev > 0) {
if (count($curr_names[1]) > 1) {
$ret .= $this->_sep . $curr_names[1][$lev];
} else {
$ret .= $this->_sep . $curr_names[1][0];
}
} else {
$ret .= $this->_sep . $curr_names[1][0];
}
}
return $ret;
}
// }}}

}
48 changes: 28 additions & 20 deletions Numbers/Words/Locale/ru.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ class Numbers_Words_Locale_ru extends Numbers_Words
* @access public
*/
var $lang_native = 'Русский';

/**
* The word for the minus sign
* @var string
* @access private
*/
var $_minus = 'минус'; // minus sign

/**
* The sufixes for exponents (singular)
* Names partly based on:
Expand Down Expand Up @@ -242,7 +242,7 @@ class Numbers_Words_Locale_ru extends Numbers_Words
);

/**
* The array containing the digits
* The array containing the digits
* for neutral, male and female
* @var array
* @access private
Expand Down Expand Up @@ -272,23 +272,23 @@ class Numbers_Words_Locale_ru extends Numbers_Words
*/
var $_currency_names = array(
'ALL' => array(
array(1, 'лек', 'лека', 'леков'),
array(1, 'лек', 'лека', 'леков'),
array(2, 'киндарка', 'киндарки', 'киндарок')
),
'AUD' => array(
array(1, 'австралийский доллар', 'австралийских доллара', 'австралийских долларов'),
array(1, 'цент', 'цента', 'центов')
),
'BGN' => array(
array(1, 'лев', 'лева', 'левов'),
array(1, 'лев', 'лева', 'левов'),
array(2, 'стотинка', 'стотинки', 'стотинок')
),
'BRL' => array(
array(1, 'бразильский реал', 'бразильских реала', 'бразильских реалов'),
array(1, 'бразильский реал', 'бразильских реала', 'бразильских реалов'),
array(1, 'сентаво', 'сентаво', 'сентаво')
),
'BYR' => array(
array(1, 'белорусский рубль', 'белорусских рубля', 'белорусских рублей'),
array(1, 'белорусский рубль', 'белорусских рубля', 'белорусских рублей'),
array(2, 'копейка', 'копейки', 'копеек')
),
'CAD' => array(
Expand Down Expand Up @@ -439,7 +439,7 @@ class Numbers_Words_Locale_ru extends Numbers_Words
* @author Andrey Demenev <demenev@on-line.jar.ru>
* @since Numbers_Words 0.16.3
*/
function _toWords($num, $options = array())
function _toWords($num, $options = array())
{
$dummy = null;
$gender = 1;
Expand Down Expand Up @@ -472,9 +472,9 @@ function _toWordsWithCase($num, &$case, $gender = 1)
{
$ret = '';
$case = 3;

$num = trim($num);

$sign = "";
if (substr($num, 0, 1) == '-') {
$sign = $this->_minus . $this->_sep;
Expand Down Expand Up @@ -552,7 +552,7 @@ function _toWordsWithCase($num, &$case, $gender = 1)
*/
function _groupToWords($num, $gender, &$case)
{
$ret = '';
$ret = '';
$case = 3;

if ((int)$num == 0) {
Expand All @@ -562,7 +562,7 @@ function _groupToWords($num, $gender, &$case)
if ($num == 1) {
$case = 1;
} elseif ($num < 5) {
$case = 2;
$case = 2;
} else {
$case = 3;
}
Expand Down Expand Up @@ -632,15 +632,23 @@ function _groupToWords($num, $gender, &$case)
*/
function toCurrencyWords($int_curr, $decimal, $fraction = false, $convert_fraction = true)
{
$int_curr = strtoupper($int_curr);
if (!isset($this->_currency_names[$int_curr])) {
$int_curr = $this->def_currency;
}

$curr_names = $this->_currency_names[$int_curr];

if (is_array($int_curr))
$curr_names = $int_curr;
else
{
$int_curr = strtoupper($int_curr);
if (!isset($this->_currency_names[$int_curr])) {
$int_curr = $this->def_currency;
}
$curr_names = $this->_currency_names[$int_curr];
}

$case = null;
$ret = trim($this->_toWordsWithCase($decimal, $case, $curr_names[0][0]));
$ret .= $this->_sep . $curr_names[0][$case];
if (Numbers_Words::$useAbbrAsDecimalNames)
$ret .= $this->_sep . $int_curr;
else
$ret .= $this->_sep . $curr_names[0][$case];

if ($fraction !== false) {
if ($convert_fraction) {
Expand Down