Skip to content

Commit

Permalink
Merge pull request #18 from chadicus/master
Browse files Browse the repository at this point in the history
Add castInts flag to Float::filter()
  • Loading branch information
Jonathan Gaillard committed Jan 8, 2014
2 parents 61aaaf1 + 40e521a commit 1e5ec46
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/DominionEnterprises/Filter/Float.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,23 @@ final class Float
* @param bool $allowNull Set to true if NULL values are allowed. The filtered result of a NULL value is NULL
* @param int $minValue The minimum acceptable value
* @param int $maxValue The maximum acceptable value
* @param bool $castInts Flag to cast $value to float if it is an integer
*
* @return float The filtered value
*
* @see is_numeric
* @throws \InvalidArgumentException if $allowNull is not a boolean
* @throws \InvalidArgumentException if $minValue is not null and not a float
* @throws \InvalidArgumentException if $maxValue is not null and not a float
* @throws \InvalidArgumentException if $castInts is not a boolean
* @throws \Exception if $value does not pass is_numeric
* @throws \Exception if $value is hex format
* @throws \Exception if $value is not a string or float
* @throws \Exception if $value overflow or underflows
* @throws \Exception if $value is less than $minValue
* @throws \Exception if $value is greater than $maxValue
*/
public static function filter($value, $allowNull = false, $minValue = null, $maxValue = null)
public static function filter($value, $allowNull = false, $minValue = null, $maxValue = null, $castInts = false)
{
if ($allowNull !== false && $allowNull !== true) {
throw new \InvalidArgumentException('"' . var_export($allowNull, true) . '" $allowNull was not a bool');
Expand All @@ -47,13 +49,19 @@ public static function filter($value, $allowNull = false, $minValue = null, $max
throw new \InvalidArgumentException('"' . var_export($maxValue, true) . '" $maxValue was not a float');
}

if ($castInts !== false && $castInts !== true) {
throw new \InvalidArgumentException('"' . var_export($castInts, true) . '" $castInts was not a bool');
}

if ($allowNull === true && $value === null) {
return null;
}

$valueFloat = null;
if (is_float($value)) {
$valueFloat = $value;
} elseif (is_int($value) && $castInts) {
$valueFloat = (float)$value;
} elseif (is_string($value)) {
$value = trim($value);

Expand Down
31 changes: 31 additions & 0 deletions tests/DominionEnterprises/Filter/FloatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,35 @@ public function filter_equalToMax()
{
$this->assertSame(0.0, F::filter(0.0, false, null, 0.0));
}

/**
* @test
* @covers \DominionEnterprises\Filter\Float::filter
*/
public function filter_castInts()
{
$this->assertSame(1.0, F::filter(1, false, null, null, true));
}

/**
* @test
* @covers \DominionEnterprises\Filter\Float::filter
* @expectedException \Exception
* @expectedExceptionMessage "1" $value is not a string
*/
public function filter_castIntsIsFalse()
{
F::filter(1, false, null, null, false);
}

/**
* @test
* @covers \DominionEnterprises\Filter\Float::filter
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage "1" $castInts was not a bool
*/
public function filter_castIntsIsNotBool()
{
F::filter('1', false, null, null, 1);
}
}

0 comments on commit 1e5ec46

Please sign in to comment.