From 5ebf6cb689bad3d9762350b7829b7ba28ca89c8b Mon Sep 17 00:00:00 2001 From: "J. King" Date: Thu, 28 Sep 2017 08:55:47 -0400 Subject: [PATCH] Treat objects which are convertible to strings the same as actual strings in ValueInfo --- lib/Misc/ValueInfo.php | 12 ++++++++---- tests/Misc/TestValueInfo.php | 19 +++++++++++++++++++ tests/lib/Misc/StrClass.php | 15 +++++++++++++++ 3 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 tests/lib/Misc/StrClass.php diff --git a/lib/Misc/ValueInfo.php b/lib/Misc/ValueInfo.php index bc7e9d38..dd784b10 100644 --- a/lib/Misc/ValueInfo.php +++ b/lib/Misc/ValueInfo.php @@ -18,9 +18,10 @@ class ValueInfo { if (is_null($value)) { // check if the input is null return self::NULL; - } elseif (is_string($value)) { + } elseif (is_string($value) || (is_object($value) && method_exists($value, "__toString"))) { + $value = (string) $value; // normalize a string an integer or float if possible - if (!strlen((string) $value)) { + if (!strlen($value)) { // the empty string is equivalent to null when evaluating an integer return self::NULL; } elseif (filter_var($value, \FILTER_VALIDATE_FLOAT) !== false && !fmod((float) $value, 1)) { @@ -55,8 +56,11 @@ class ValueInfo { if (is_null($value)) { $out += self::NULL; } - // if the value is not scalar, is a boolean, or is infinity or NaN, it cannot be valid - if (!is_scalar($value) || is_bool($value) || (is_float($value) && !is_finite($value))) { + if (is_object($value) && method_exists($value, "__toString")) { + // if the value is an object which has a __toString method, this is acceptable + $value = (string) $value; + } elseif (!is_scalar($value) || is_bool($value) || (is_float($value) && !is_finite($value))) { + // otherwise if the value is not scalar, is a boolean, or is infinity or NaN, it cannot be valid return $out; } // mark validity diff --git a/tests/Misc/TestValueInfo.php b/tests/Misc/TestValueInfo.php index 8a590552..6fac75cc 100644 --- a/tests/Misc/TestValueInfo.php +++ b/tests/Misc/TestValueInfo.php @@ -3,6 +3,7 @@ declare(strict_types=1); namespace JKingWeb\Arsse; use JKingWeb\Arsse\Misc\ValueInfo as I; +use JKingWeb\Arsse\Test\Misc\StrClass; /** @covers \JKingWeb\Arsse\Misc\ValueInfo */ class TestValueInfo extends Test\AbstractTest { @@ -58,6 +59,12 @@ class TestValueInfo extends Test\AbstractTest { ["some string", 0], [" ", 0], [new \StdClass, 0], + [new StrClass(""), I::NULL], + [new StrClass("1"), I::VALID], + [new StrClass("0"), I::VALID | I::ZERO], + [new StrClass("-1"), I::VALID | I::NEG], + [new StrClass("Msg"), 0], + [new StrClass(" "), 0], ]; foreach ($tests as $test) { list($value, $exp) = $test; @@ -116,6 +123,12 @@ class TestValueInfo extends Test\AbstractTest { ["some string", I::VALID], [" ", I::VALID | I::WHITE], [new \StdClass, 0], + [new StrClass(""), I::VALID | I::EMPTY], + [new StrClass("1"), I::VALID], + [new StrClass("0"), I::VALID], + [new StrClass("-1"), I::VALID], + [new StrClass("Msg"), I::VALID], + [new StrClass(" "), I::VALID | I::WHITE], ]; foreach ($tests as $test) { list($value, $exp) = $test; @@ -175,6 +188,12 @@ class TestValueInfo extends Test\AbstractTest { ["some string", false, false], [" ", false, false], [new \StdClass, false, false], + [new StrClass(""), false, true], + [new StrClass("1"), true, true], + [new StrClass("0"), false, true], + [new StrClass("-1"), false, false], + [new StrClass("Msg"), false, false], + [new StrClass(" "), false, false], ]; foreach ($tests as $test) { list($value, $exp, $expNull) = $test; diff --git a/tests/lib/Misc/StrClass.php b/tests/lib/Misc/StrClass.php new file mode 100644 index 00000000..d93f2c5d --- /dev/null +++ b/tests/lib/Misc/StrClass.php @@ -0,0 +1,15 @@ +str = (string) $str; + } + + public function __toString() { + return $this->str; + } +} \ No newline at end of file