Changeset View
Changeset View
Standalone View
Standalone View
branches/5.2.x/core/kernel/utility/formatters/formatter.php
Show First 20 Lines • Show All 57 Lines • ▼ Show 20 Line(s) | |||||
* | * | ||||
* @param mixed $value | * @param mixed $value | ||||
* @param Array $options | * @param Array $options | ||||
* @return mixed | * @return mixed | ||||
* @access protected | * @access protected | ||||
*/ | */ | ||||
function TypeCast($value, $options) | function TypeCast($value, $options) | ||||
{ | { | ||||
$ret = true; | |||||
if ( isset($options['type']) ) { | if ( isset($options['type']) ) { | ||||
$field_type = $options['type']; | $field_type = $options['type']; | ||||
if ($field_type == 'numeric') { | if ($field_type == 'numeric') { | ||||
trigger_error('Invalid field type <strong>' . $field_type . '</strong> (in TypeCast method), please use <strong>float</strong> instead', E_USER_NOTICE); | trigger_error('Invalid field type <strong>' . $field_type . '</strong> (in TypeCast method), please use <strong>float</strong> instead', E_USER_NOTICE); | ||||
$field_type = 'float'; | $field_type = 'float'; | ||||
} | } | ||||
elseif ( $field_type == 'string' ) { | elseif ( $field_type == 'string' ) { | ||||
if ( isset($options['allow_html']) && $options['allow_html'] ) { | if ( isset($options['allow_html']) && $options['allow_html'] ) { | ||||
$value = $this->Application->unescapeRequestVariable($value); | $value = $this->Application->unescapeRequestVariable($value); | ||||
} | } | ||||
return $value; | return $value; | ||||
} | } | ||||
$value = $this->formatNumber($value); | $value = $this->formatNumber($value); | ||||
$type_ok = preg_match('#int|integer|double|float|real|numeric|string#', $field_type); | $is_numeric_type = preg_match('#int|integer|double|float|real#', $field_type); | ||||
if ( $value != '' && $type_ok ) { | if ( $value != '' && $is_numeric_type ) { | ||||
$ret = is_numeric($value); | $tc_value = $value; | ||||
settype($tc_value, $field_type); | |||||
if ($ret) { | // Type casing is considered a success only, when type casted value visually looks the same. | ||||
$f = 'is_' . $field_type; | return (string)$value === (string)$tc_value ? $tc_value : false; | ||||
settype($value, $field_type); | |||||
$ret = $f($value); | |||||
} | |||||
} | } | ||||
} | } | ||||
return $ret ? $value : false; | return $value; | ||||
} | } | ||||
/** | /** | ||||
* Formats number, according to regional settings | * Formats number, according to regional settings | ||||
* | * | ||||
* @param string $number | * @param string $number | ||||
* @return float | * @return float | ||||
*/ | */ | ||||
▲ Show 20 Lines • Show All 204 Lines • Show Last 20 Lines |