File tree Expand file tree Collapse file tree 2 files changed +32
-4
lines changed
Expand file tree Collapse file tree 2 files changed +32
-4
lines changed Original file line number Diff line number Diff line change @@ -77,14 +77,14 @@ private static function serializeBareItem($value): string
7777 return self ::serializeInteger ($ value );
7878 } elseif (is_float ($ value )) {
7979 return self ::serializeDecimal ($ value );
80- } elseif (is_string ($ value )) {
81- return self ::serializeString ($ value );
82- } elseif ($ value instanceof Token) {
83- return self ::serializeToken ($ value );
8480 } elseif (is_bool ($ value )) {
8581 return self ::serializeBoolean ($ value );
82+ } elseif ($ value instanceof Token) {
83+ return self ::serializeToken ($ value );
8684 } elseif ($ value instanceof Bytes) {
8785 return self ::serializeByteSequence ($ value );
86+ } elseif (is_string ($ value ) || (is_object ($ value ) && method_exists ($ value , '__toString ' ))) {
87+ return self ::serializeString ($ value );
8888 }
8989
9090 throw new SerializeException ("Unrecognized type " );
Original file line number Diff line number Diff line change @@ -15,7 +15,35 @@ class SerializeStringTest extends TestCase
1515 public function testInvalidCharacter ()
1616 {
1717 $ this ->expectException (SerializeException::class);
18+ $ this ->expectExceptionMessage ("Invalid characters in string " );
1819
1920 Serializer::serializeItem ("🙁 " );
2021 }
22+
23+ public function testStringableObject ()
24+ {
25+ $ stringable = new class {
26+ public function __toString (): string
27+ {
28+ return "Don't Panic " ;
29+ }
30+ };
31+
32+ $ this ->assertEquals ('"Don \'t Panic" ' , Serializer::serializeItem ($ stringable ));
33+ }
34+
35+ public function testInvalidStringableObject ()
36+ {
37+ $ this ->expectException (SerializeException::class);
38+ $ this ->expectExceptionMessage ("Invalid characters in string " );
39+
40+ $ stringable = new class {
41+ public function __toString (): string
42+ {
43+ return "🙁 " ;
44+ }
45+ };
46+
47+ Serializer::serializeItem ($ stringable );
48+ }
2149}
You can’t perform that action at this time.
0 commit comments