1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-22 21:22:40 +00:00

Add boolean sanitizer to ValueInfo

This commit is contained in:
J. King 2017-10-07 12:44:53 -04:00
parent 7a2de95c70
commit 0e6eed5699
3 changed files with 106 additions and 1 deletions

View file

@ -89,4 +89,16 @@ class ValueInfo {
return true; return true;
} }
} }
public static function bool($value, bool $default = null) {
if (is_null($value) || ValueInfo::str($value) & ValueInfo::WHITE) {
return $default;
}
$out = filter_var($value, \FILTER_VALIDATE_BOOLEAN, \FILTER_NULL_ON_FAILURE);
if (is_null($out) && (ValueInfo::int($value) & ValueInfo::VALID)) {
$out = abs((int) filter_var($value, \FILTER_VALIDATE_FLOAT));
return ($out < 2) ? (bool) $out : $default;
}
return !is_null($out) ? $out : $default;
}
} }

View file

@ -59,7 +59,7 @@ abstract class AbstractHandler implements Handler {
} }
break; break;
case "bool": case "bool":
$test = filter_var($value, \FILTER_VALIDATE_BOOLEAN, \FILTER_NULL_ON_FAILURE); $test = ValueInfo::bool($value);
if (!is_null($test)) { if (!is_null($test)) {
$out[$key] = $test; $out[$key] = $test;
} }

View file

@ -52,6 +52,12 @@ class TestValueInfo extends Test\AbstractTest {
["-000.000", I::VALID | I::ZERO], ["-000.000", I::VALID | I::ZERO],
[false, 0], [false, 0],
[true, 0], [true, 0],
["on", 0],
["off", 0],
["yes", 0],
["no", 0],
["true", 0],
["false", 0],
[INF, 0], [INF, 0],
[-INF, 0], [-INF, 0],
[NAN, 0], [NAN, 0],
@ -116,6 +122,12 @@ class TestValueInfo extends Test\AbstractTest {
["-000.000", I::VALID], ["-000.000", I::VALID],
[false, 0], [false, 0],
[true, 0], [true, 0],
["on", I::VALID],
["off", I::VALID],
["yes", I::VALID],
["no", I::VALID],
["true", I::VALID],
["false", I::VALID],
[INF, 0], [INF, 0],
[-INF, 0], [-INF, 0],
[NAN, 0], [NAN, 0],
@ -181,6 +193,12 @@ class TestValueInfo extends Test\AbstractTest {
["-000.000", false, true], ["-000.000", false, true],
[false, false, false], [false, false, false],
[true, false, false], [true, false, false],
["on", false, false],
["off", false, false],
["yes", false, false],
["no", false, false],
["true", false, false],
["false", false, false],
[INF, false, false], [INF, false, false],
[-INF, false, false], [-INF, false, false],
[NAN, false, false], [NAN, false, false],
@ -201,4 +219,79 @@ class TestValueInfo extends Test\AbstractTest {
$this->assertSame($expNull, I::id($value, true), "Null test failed for value: ".var_export($value, true)); $this->assertSame($expNull, I::id($value, true), "Null test failed for value: ".var_export($value, true));
} }
} }
public function testValidateBoolean() {
$tests = [
[null, null],
["", false],
[1, true],
[PHP_INT_MAX, null],
[1.0, true],
["1.0", true],
["001.0", true],
["1.0e2", null],
["1", true],
["001", true],
["1e2", null],
["+1.0", true],
["+001.0", true],
["+1.0e2", null],
["+1", true],
["+001", true],
["+1e2", null],
[0, false],
["0", false],
["000", false],
[0.0, false],
["0.0", false],
["000.000", false],
["+0", false],
["+000", false],
["+0.0", false],
["+000.000", false],
[-1, true],
[-1.0, true],
["-1.0", true],
["-001.0", true],
["-1.0e2", null],
["-1", true],
["-001", true],
["-1e2", null],
[-0, false],
["-0", false],
["-000", false],
[-0.0, false],
["-0.0", false],
["-000.000", false],
[false, false],
[true, true],
["on", true],
["off", false],
["yes", true],
["no", false],
["true", true],
["false", false],
[INF, null],
[-INF, null],
[NAN, null],
[[], null],
["some string", null],
[" ", null],
[new \StdClass, null],
[new StrClass(""), false],
[new StrClass("1"), true],
[new StrClass("0"), false],
[new StrClass("-1"), true],
[new StrClass("Msg"), null],
[new StrClass(" "), null],
];
foreach ($tests as $test) {
list($value, $exp) = $test;
$this->assertSame($exp, I::bool($value), "Null Test failed for value: ".var_export($value, true));
if (is_null($exp)) {
$this->assertTrue(I::bool($value, true), "True Test failed for value: ".var_export($value, true));
$this->assertFalse(I::bool($value, false), "False Test failed for value: ".var_export($value, true));
}
}
}
} }