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

Treat objects which are convertible to strings the same as actual strings in ValueInfo

This commit is contained in:
J. King 2017-09-28 08:55:47 -04:00
parent d365529493
commit 5ebf6cb689
3 changed files with 42 additions and 4 deletions

View file

@ -18,9 +18,10 @@ class ValueInfo {
if (is_null($value)) { if (is_null($value)) {
// check if the input is null // check if the input is null
return self::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 // 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 // the empty string is equivalent to null when evaluating an integer
return self::NULL; return self::NULL;
} elseif (filter_var($value, \FILTER_VALIDATE_FLOAT) !== false && !fmod((float) $value, 1)) { } elseif (filter_var($value, \FILTER_VALIDATE_FLOAT) !== false && !fmod((float) $value, 1)) {
@ -55,8 +56,11 @@ class ValueInfo {
if (is_null($value)) { if (is_null($value)) {
$out += self::NULL; $out += self::NULL;
} }
// if the value is not scalar, is a boolean, or is infinity or NaN, it cannot be valid if (is_object($value) && method_exists($value, "__toString")) {
if (!is_scalar($value) || is_bool($value) || (is_float($value) && !is_finite($value))) { // 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; return $out;
} }
// mark validity // mark validity

View file

@ -3,6 +3,7 @@ declare(strict_types=1);
namespace JKingWeb\Arsse; namespace JKingWeb\Arsse;
use JKingWeb\Arsse\Misc\ValueInfo as I; use JKingWeb\Arsse\Misc\ValueInfo as I;
use JKingWeb\Arsse\Test\Misc\StrClass;
/** @covers \JKingWeb\Arsse\Misc\ValueInfo */ /** @covers \JKingWeb\Arsse\Misc\ValueInfo */
class TestValueInfo extends Test\AbstractTest { class TestValueInfo extends Test\AbstractTest {
@ -58,6 +59,12 @@ class TestValueInfo extends Test\AbstractTest {
["some string", 0], ["some string", 0],
[" ", 0], [" ", 0],
[new \StdClass, 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) { foreach ($tests as $test) {
list($value, $exp) = $test; list($value, $exp) = $test;
@ -116,6 +123,12 @@ class TestValueInfo extends Test\AbstractTest {
["some string", I::VALID], ["some string", I::VALID],
[" ", I::VALID | I::WHITE], [" ", I::VALID | I::WHITE],
[new \StdClass, 0], [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) { foreach ($tests as $test) {
list($value, $exp) = $test; list($value, $exp) = $test;
@ -175,6 +188,12 @@ class TestValueInfo extends Test\AbstractTest {
["some string", false, false], ["some string", false, false],
[" ", false, false], [" ", false, false],
[new \StdClass, 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) { foreach ($tests as $test) {
list($value, $exp, $expNull) = $test; list($value, $exp, $expNull) = $test;

View file

@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace JKingWeb\Arsse\Test\Misc;
class StrClass {
public $str = "";
public function __construct($str) {
$this->str = (string) $str;
}
public function __toString() {
return $this->str;
}
}