Skip to content

Commit 1e5ec46

Browse files
author
Jonathan Gaillard
committed
Merge pull request #18 from chadicus/master
Add castInts flag to Float::filter()
2 parents 61aaaf1 + 40e521a commit 1e5ec46

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/DominionEnterprises/Filter/Float.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

tests/DominionEnterprises/Filter/FloatTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,4 +251,35 @@ public function filter_equalToMax()
251251
{
252252
$this->assertSame(0.0, F::filter(0.0, false, null, 0.0));
253253
}
254+
255+
/**
256+
* @test
257+
* @covers \DominionEnterprises\Filter\Float::filter
258+
*/
259+
public function filter_castInts()
260+
{
261+
$this->assertSame(1.0, F::filter(1, false, null, null, true));
262+
}
263+
264+
/**
265+
* @test
266+
* @covers \DominionEnterprises\Filter\Float::filter
267+
* @expectedException \Exception
268+
* @expectedExceptionMessage "1" $value is not a string
269+
*/
270+
public function filter_castIntsIsFalse()
271+
{
272+
F::filter(1, false, null, null, false);
273+
}
274+
275+
/**
276+
* @test
277+
* @covers \DominionEnterprises\Filter\Float::filter
278+
* @expectedException \InvalidArgumentException
279+
* @expectedExceptionMessage "1" $castInts was not a bool
280+
*/
281+
public function filter_castIntsIsNotBool()
282+
{
283+
F::filter('1', false, null, null, 1);
284+
}
254285
}

0 commit comments

Comments
 (0)