Skip to content

Commit c258109

Browse files
committed
Allow stringeable objects to be serialized by StringType
Closes webonyx#302
1 parent 50dfd3f commit c258109

File tree

2 files changed

+4
-0
lines changed

2 files changed

+4
-0
lines changed

src/Type/Definition/StringType.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public function serialize($value)
4040
if ($value === null) {
4141
return 'null';
4242
}
43+
if (is_object($value) && method_exists($value, '__toString')) {
44+
return (string) $value;
45+
}
4346
if (!is_scalar($value)) {
4447
throw new Error("String cannot represent non scalar value: " . Utils::printSafe($value));
4548
}

tests/Type/ScalarSerializationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ public function testSerializesOutputStrings()
150150
$this->assertSame('true', $stringType->serialize(true));
151151
$this->assertSame('false', $stringType->serialize(false));
152152
$this->assertSame('null', $stringType->serialize(null));
153+
$this->assertSame('2', $stringType->serialize(new ObjectIdStub(2)));
153154
}
154155

155156
public function testSerializesOutputStringsCannotRepresentArray()

0 commit comments

Comments
 (0)