mirror of
https://code.mensbeam.com/MensBeam/Arsse.git
synced 2024-12-22 13:12:41 +00:00
Treat objects which are convertible to strings the same as actual strings in ValueInfo
This commit is contained in:
parent
d365529493
commit
5ebf6cb689
3 changed files with 42 additions and 4 deletions
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
|
|
15
tests/lib/Misc/StrClass.php
Normal file
15
tests/lib/Misc/StrClass.php
Normal 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;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue