1
0
mirror of https://github.com/e107inc/e107.git synced 2025-04-20 04:32:01 +02:00

refactoring toNumber() into e_parse class

This commit is contained in:
Achim Ennenbach 2018-05-04 19:27:00 +02:00
parent 2a1a6d4118
commit d01a97816f
4 changed files with 50 additions and 62 deletions

View File

@ -2385,6 +2385,32 @@ class e_parse extends e_parser
}
/**
* Convert a string to a number (int/float)
*
* @param string $value
* @return int|float
*/
function toNumber($value)
{
// adapted from: https://secure.php.net/manual/en/function.floatval.php#114486
$dotPos = strrpos($value, '.');
$commaPos = strrpos($value, ',');
$sep = (($dotPos > $commaPos) && $dotPos) ? $dotPos :
((($commaPos > $dotPos) && $commaPos) ? $commaPos : false);
if (!$sep) {
return preg_replace("/[^-0-9]/", "", $value);
}
return (
preg_replace("/[^-0-9]/", "", substr($value, 0, $sep)) . '.' .
preg_replace("/[^0-9]/", "", substr($value, $sep+1, strlen($value)))
);
}
/**
* Clean and Encode Ampersands '&' for output to browser.
* @param string $text

View File

@ -1792,6 +1792,7 @@ class e_model extends e_object
* @param string $value
* @return integer|float
*/
// moved to e_parse
// public function toNumber($value)
// {
// $larr = localeconv();
@ -1807,23 +1808,6 @@ class e_model extends e_object
// return str_replace($search, $replace, $value);
// }
public function toNumber($value)
{
// adapted from: https://secure.php.net/manual/en/function.floatval.php#114486
$dotPos = strrpos($value, '.');
$commaPos = strrpos($value, ',');
$sep = (($dotPos > $commaPos) && $dotPos) ? $dotPos :
((($commaPos > $dotPos) && $commaPos) ? $commaPos : false);
if (!$sep) {
return preg_replace("/[^-0-9]/", "", $value);
}
return (
preg_replace("/[^-0-9]/", "", substr($value, 0, $sep)) . '.' .
preg_replace("/[^0-9]/", "", substr($value, $sep+1, strlen($value)))
);
}
/**
* Convert object data to a string
@ -2721,7 +2705,8 @@ class e_front_model extends e_model
{
case 'int':
case 'integer':
return intval($this->toNumber($value));
//return intval($this->toNumber($value));
return intval($tp->toNumber($value));
break;
case 'safestr':
@ -2748,7 +2733,8 @@ class e_front_model extends e_model
break;
case 'float':
return $this->toNumber($value);
// return $this->toNumber($value);
return $tp->toNumber($value);
break;
case 'bool':

View File

@ -1307,7 +1307,7 @@ class e_db_mysql
// return str_replace($search, $replace, floatval($fieldValue));
return $this->_toNumber($fieldValue);
return e107::getParser()->toNumber($fieldValue);
break;
case 'null':
@ -1367,7 +1367,7 @@ class e_db_mysql
// $replace = array('.', '.', '', '', '', '');
// return str_replace($search, $replace, floatval($fieldValue));
return $this->_toNumber($fieldValue);
return e107::getParser()->toNumber($fieldValue);
break;
case 'null':
@ -1439,31 +1439,6 @@ class e_db_mysql
}
/**
* Convert a string to a number (int/float)
*
* @param string $value
* @return int|float
*/
function _toNumber($value)
{
// adapted from: https://secure.php.net/manual/en/function.floatval.php#114486
$dotPos = strrpos($value, '.');
$commaPos = strrpos($value, ',');
$sep = (($dotPos > $commaPos) && $dotPos) ? $dotPos :
((($commaPos > $dotPos) && $commaPos) ? $commaPos : false);
if (!$sep) {
return preg_replace("/[^-0-9]/", "", $value);
}
return (
preg_replace("/[^-0-9]/", "", substr($value, 0, $sep)) . '.' .
preg_replace("/[^0-9]/", "", substr($value, $sep+1, strlen($value)))
);
}
/**
* @DEPRECATED
Similar to db_Update(), but splits the variables and the 'WHERE' clause.

View File

@ -577,7 +577,7 @@ class e_validator
break;
case 'float':
$value = $this->toNumber($value);
$value = e107::getParser()->toNumber($value);
if(!is_numeric($value))
{
$this->addValidateResult($name, self::ERR_FLOAT_EXPECTED);
@ -728,22 +728,23 @@ class e_validator
break;
}
}
public function toNumber($value)
{
$larr = localeconv();
$search = array(
$larr['decimal_point'],
$larr['mon_decimal_point'],
$larr['thousands_sep'],
$larr['mon_thousands_sep'],
$larr['currency_symbol'],
$larr['int_curr_symbol']
);
$replace = array('.', '.', '', '', '', '');
// moved to e_parse
// public function toNumber($value)
// {
// $larr = localeconv();
// $search = array(
// $larr['decimal_point'],
// $larr['mon_decimal_point'],
// $larr['thousands_sep'],
// $larr['mon_thousands_sep'],
// $larr['currency_symbol'],
// $larr['int_curr_symbol']
// );
// $replace = array('.', '.', '', '', '', '');
return str_replace($search, $replace, $value);
}
// return str_replace($search, $replace, $value);
// }
protected function parseMinMax($string)
{