From 0e6eed56991162aac08578fa1886c594656bccd8 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Sat, 7 Oct 2017 12:44:53 -0400 Subject: [PATCH] Add boolean sanitizer to ValueInfo --- lib/Misc/ValueInfo.php | 12 +++++ lib/REST/AbstractHandler.php | 2 +- tests/Misc/TestValueInfo.php | 93 ++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 1 deletion(-) diff --git a/lib/Misc/ValueInfo.php b/lib/Misc/ValueInfo.php index 040243dc..c382836b 100644 --- a/lib/Misc/ValueInfo.php +++ b/lib/Misc/ValueInfo.php @@ -89,4 +89,16 @@ class ValueInfo { 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; + } } diff --git a/lib/REST/AbstractHandler.php b/lib/REST/AbstractHandler.php index 8c0988df..81c3a68a 100644 --- a/lib/REST/AbstractHandler.php +++ b/lib/REST/AbstractHandler.php @@ -59,7 +59,7 @@ abstract class AbstractHandler implements Handler { } break; case "bool": - $test = filter_var($value, \FILTER_VALIDATE_BOOLEAN, \FILTER_NULL_ON_FAILURE); + $test = ValueInfo::bool($value); if (!is_null($test)) { $out[$key] = $test; } diff --git a/tests/Misc/TestValueInfo.php b/tests/Misc/TestValueInfo.php index 1a75099d..1291b47a 100644 --- a/tests/Misc/TestValueInfo.php +++ b/tests/Misc/TestValueInfo.php @@ -52,6 +52,12 @@ class TestValueInfo extends Test\AbstractTest { ["-000.000", I::VALID | I::ZERO], [false, 0], [true, 0], + ["on", 0], + ["off", 0], + ["yes", 0], + ["no", 0], + ["true", 0], + ["false", 0], [INF, 0], [-INF, 0], [NAN, 0], @@ -116,6 +122,12 @@ class TestValueInfo extends Test\AbstractTest { ["-000.000", I::VALID], [false, 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], [NAN, 0], @@ -181,6 +193,12 @@ class TestValueInfo extends Test\AbstractTest { ["-000.000", false, true], [false, 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], [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)); } } + + 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)); + } + } + } }