Allow stringeable objects to be serialized by StringType

Closes #302
This commit is contained in:
Théo FIDRY 2018-07-07 22:55:53 +02:00
parent 50dfd3fac2
commit c258109844
No known key found for this signature in database
GPG Key ID: F8C66BC24296DE7E
2 changed files with 4 additions and 0 deletions

View File

@ -40,6 +40,9 @@ represent free-form human-readable text.';
if ($value === null) { if ($value === null) {
return 'null'; return 'null';
} }
if (is_object($value) && method_exists($value, '__toString')) {
return (string) $value;
}
if (!is_scalar($value)) { if (!is_scalar($value)) {
throw new Error("String cannot represent non scalar value: " . Utils::printSafe($value)); throw new Error("String cannot represent non scalar value: " . Utils::printSafe($value));
} }

View File

@ -150,6 +150,7 @@ class ScalarSerializationTest extends \PHPUnit_Framework_TestCase
$this->assertSame('true', $stringType->serialize(true)); $this->assertSame('true', $stringType->serialize(true));
$this->assertSame('false', $stringType->serialize(false)); $this->assertSame('false', $stringType->serialize(false));
$this->assertSame('null', $stringType->serialize(null)); $this->assertSame('null', $stringType->serialize(null));
$this->assertSame('2', $stringType->serialize(new ObjectIdStub(2)));
} }
public function testSerializesOutputStringsCannotRepresentArray() public function testSerializesOutputStringsCannotRepresentArray()