@@ -19,21 +19,23 @@ final class Float
1919 * @param bool $allowNull Set to true if NULL values are allowed. The filtered result of a NULL value is NULL
2020 * @param int $minValue The minimum acceptable value
2121 * @param int $maxValue The maximum acceptable value
22+ * @param bool $castInts Flag to cast $value to float if it is an integer
2223 *
2324 * @return float The filtered value
2425 *
2526 * @see is_numeric
2627 * @throws \InvalidArgumentException if $allowNull is not a boolean
2728 * @throws \InvalidArgumentException if $minValue is not null and not a float
2829 * @throws \InvalidArgumentException if $maxValue is not null and not a float
30+ * @throws \InvalidArgumentException if $castInts is not a boolean
2931 * @throws \Exception if $value does not pass is_numeric
3032 * @throws \Exception if $value is hex format
3133 * @throws \Exception if $value is not a string or float
3234 * @throws \Exception if $value overflow or underflows
3335 * @throws \Exception if $value is less than $minValue
3436 * @throws \Exception if $value is greater than $maxValue
3537 */
36- public static function filter ($ value , $ allowNull = false , $ minValue = null , $ maxValue = null )
38+ public static function filter ($ value , $ allowNull = false , $ minValue = null , $ maxValue = null , $ castInts = false )
3739 {
3840 if ($ allowNull !== false && $ allowNull !== true ) {
3941 throw new \InvalidArgumentException ('" ' . var_export ($ allowNull , true ) . '" $allowNull was not a bool ' );
@@ -47,13 +49,19 @@ public static function filter($value, $allowNull = false, $minValue = null, $max
4749 throw new \InvalidArgumentException ('" ' . var_export ($ maxValue , true ) . '" $maxValue was not a float ' );
4850 }
4951
52+ if ($ castInts !== false && $ castInts !== true ) {
53+ throw new \InvalidArgumentException ('" ' . var_export ($ castInts , true ) . '" $castInts was not a bool ' );
54+ }
55+
5056 if ($ allowNull === true && $ value === null ) {
5157 return null ;
5258 }
5359
5460 $ valueFloat = null ;
5561 if (is_float ($ value )) {
5662 $ valueFloat = $ value ;
63+ } elseif (is_int ($ value ) && $ castInts ) {
64+ $ valueFloat = (float )$ value ;
5765 } elseif (is_string ($ value )) {
5866 $ value = trim ($ value );
5967
0 commit comments