yf_common_num2string class| File | plugins/common/classes/common/yf_common_num2string.class.php |
|---|---|
| Use | _class('common_num2string', 'classes/common/') |
A site sets them in its config with $PROJECT_CONF['common_num2string']['NAME'] = ...;
| Property | Default | Description |
|---|---|---|
$LANG_MAParray |
['uk' => 'UA', 'ua' => 'UA', 'en' => 'EN'] |
site language (conf('language')) => words key, used for the default language |
public function cent_number($force = null)
public function cent_number($force = null)
{
if ($force === null) {
$result = $this->_cent_number_force;
} else {
$result = (bool) $force;
$this->_cent_number_force = $result;
}
return $result;
}
public function cent_zero($force = null)
public function cent_zero($force = null)
{
if ($force === null) {
$result = $this->_cent_zero_force;
} else {
$result = (bool) $force;
$this->_cent_zero_force = $result;
}
return $result;
}
public function currency_id($currency_id = null, $set = true)
public function currency_id($currency_id = null, $set = true)
{
$_lang_id = $this->lang_id();
$_currency_id = &$this->_currency_id;
$currency = &$this->words[$_lang_id]['currency'];
if (empty($currency_id)) {
$result = $_currency_id;
} else {
$result = strtoupper($currency_id);
$result = isset($currency[$result]) ? $result : $_currency_id;
$set && $_currency_id = $result;
}
return $result;
}
public function lang_id($lang_id = null, $set = true)
public function lang_id($lang_id = null, $set = true)
{
$_lang_id = &$this->_lang_id;
$_lang_id ??= $this->_default_lang_id();
if (empty($lang_id)) {
$result = $_lang_id;
} else {
$words = &$this->words;
$result = strtoupper($lang_id);
$result = isset($words[$result]) ? $result : $_lang_id;
$set && $_lang_id = $result;
}
return $result;
}
public function num2str($float, $currency_id = null, $lang_id = null, $set = false)
Returns number to words for money (number spelling).
public function num2str($float, $currency_id = null, $lang_id = null, $set = false)
{
$lang_id = $this->lang_id($lang_id, $set);
$currency_id = $this->currency_id($currency_id, $set);
$words = &$this->words[$lang_id];
$digits = &$words['digits'];
$signs = &$words['signs'];
$units = &$words['units'];
// prepare options
$sign_force = $this->sign();
$cent_number_force = $this->cent_number();
$cent_zero_force = $this->cent_zero();
$units[0] = &$words['currency'][$currency_id][0];
$units[1] = &$words['currency'][$currency_id][1];
// separate float on sign, integer, fractional
$result = [];
$number = trim((float) $float);
// trim fractional by 2 digits
$number_format = localeconv();
$decimal_point = $number_format['decimal_point'];
$thousands_sep = [' ', '.', ',', "'", '`'];
[$part1, $part2] = explode($decimal_point, $number) + [1 => ''];
$part2 = substr($part2 . '00', 0, 2);
$number = str_replace($thousands_sep, '', $part1) . '0' . $part2;
// add sign word
$sign_minus = substr($number, 0, 1) == '-';
// minus
$sign_minus && $result[] = $signs[1];
// plus
! $sign_minus > 0 && $sign_force && $result[] = $signs[0];
// remove sign
$sign_minus && $number = substr($number, 1);
// format number by 3 digits
while (strlen($number) % 3) {
$number = '0' . $number;
}
// processing number digits
$digits_array = str_split($number, 3);
$units_count = count((array) $digits_array) - 1;
foreach ($digits_array as $unit => $digits3) {
$unit = $units_count - $unit;
// skip zero if not dollar or cent
if ((int) $digits3 == 0 && ($unit > 1 || ! ($cent_zero_force || $unit))) {
continue;
}
if ($unit < 2 && ! (int) $digits3) {
// zero if dollar or cent
$result[] = $cent_number_force && ! $unit ? 0 : $words['zero'];
} else {
if ($cent_number_force && ! $unit) {
// cent as int
$result[] = (int) $digits3;
} else {
// separate by 1 digit
$d3 = substr($digits3, 0, 1);
$d2 = substr($digits3, 1, 1);
$d1 = substr($digits3, 2, 1);
// 1xx-9xx
if ($d3 > 0) {
$result[] = $digits[3][$d3];
}
// 20-99
if ($d2 > 1) {
$result[] = $digits[2][$d2];
}
// 10-19
if ($d2 == 1) {
$result[] = $digits[1][$d1];
}
// 1-9
if ($d1 > 0 && $d2 != 1) {
$gender = $units[$unit][3];
if ($gender > -1) {
$result[] = $digits[0][$gender][$d1];
}
}
}
}
$result[] = $this->morph($digits3, $units[$unit]);
}
$result = implode(' ', $result);
return $result;
}
public function sign($force = null)
public function sign($force = null)
{
if ($force === null) {
$result = $this->_sign_force;
} else {
$result = (bool) $force;
$this->_sign_force = $result;
}
return $result;
}
_ hooks and helpers; not routable as page actionspublic function _default_lang_id()
The site language (conf('language') through LANG_MAP: uk -> UA, en -> EN), else EN.
public function _default_lang_id()
{
$id = $this->LANG_MAP[strtolower((string) conf('language'))] ?? 'EN';
return isset($this->words[$id]) ? $id : (string) current(array_keys($this->words));
}
public function _init()
public function _init()
{
$_lang_id = &$this->_lang_id;
$_currency_id = &$this->_currency_id;
$words = &$this->words;
// the language is resolved on first use (lang_id()), conf('language') may be set after this object exists
$_lang_id = null;
// currency: the first of the default language (UAH in every language)
$_currency_id = current(array_keys($words[$this->_default_lang_id()]['currency']));
}