1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-02 20:57:26 +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. * Clean and Encode Ampersands '&' for output to browser.
* @param string $text * @param string $text

View File

@@ -1792,6 +1792,7 @@ class e_model extends e_object
* @param string $value * @param string $value
* @return integer|float * @return integer|float
*/ */
// moved to e_parse
// public function toNumber($value) // public function toNumber($value)
// { // {
// $larr = localeconv(); // $larr = localeconv();
@@ -1807,23 +1808,6 @@ class e_model extends e_object
// return str_replace($search, $replace, $value); // 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 * Convert object data to a string
@@ -2721,7 +2705,8 @@ class e_front_model extends e_model
{ {
case 'int': case 'int':
case 'integer': case 'integer':
return intval($this->toNumber($value)); //return intval($this->toNumber($value));
return intval($tp->toNumber($value));
break; break;
case 'safestr': case 'safestr':
@@ -2748,7 +2733,8 @@ class e_front_model extends e_model
break; break;
case 'float': case 'float':
return $this->toNumber($value); // return $this->toNumber($value);
return $tp->toNumber($value);
break; break;
case 'bool': case 'bool':

View File

@@ -1307,7 +1307,7 @@ class e_db_mysql
// return str_replace($search, $replace, floatval($fieldValue)); // return str_replace($search, $replace, floatval($fieldValue));
return $this->_toNumber($fieldValue); return e107::getParser()->toNumber($fieldValue);
break; break;
case 'null': case 'null':
@@ -1367,7 +1367,7 @@ class e_db_mysql
// $replace = array('.', '.', '', '', '', ''); // $replace = array('.', '.', '', '', '', '');
// return str_replace($search, $replace, floatval($fieldValue)); // return str_replace($search, $replace, floatval($fieldValue));
return $this->_toNumber($fieldValue); return e107::getParser()->toNumber($fieldValue);
break; break;
case 'null': 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 * @DEPRECATED
Similar to db_Update(), but splits the variables and the 'WHERE' clause. Similar to db_Update(), but splits the variables and the 'WHERE' clause.

View File

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