@@ -212,24 +212,22 @@ protected function serializeObject($object, int $_depth = 0, array $hashes = [])
212212 /**
213213 * Serializes the given value to a string.
214214 *
215- * @param mixed $value The value to serialize
215+ * @param string $value The value to serialize
216216 */
217- protected function serializeString ($ value ): string
217+ protected function serializeString (string $ value ): string
218218 {
219- $ value = (string ) $ value ;
220-
221219 // we always guarantee this is coerced, even if we can't detect encoding
222220 if ($ currentEncoding = mb_detect_encoding ($ value , $ this ->mbDetectOrder )) {
223- $ value = mb_convert_encoding ($ value , 'UTF-8 ' , $ currentEncoding );
221+ $ encoded = mb_convert_encoding ($ value , 'UTF-8 ' , $ currentEncoding ) ?: ' <encoding error> ' ;
224222 } else {
225- $ value = mb_convert_encoding ($ value , 'UTF-8 ' );
223+ $ encoded = mb_convert_encoding ($ value , 'UTF-8 ' ) ?: ' <encoding error> ' ;
226224 }
227225
228- if (mb_strlen ($ value ) > $ this ->options ->getMaxValueLength ()) {
229- $ value = mb_substr ($ value , 0 , $ this ->options ->getMaxValueLength () - 10 , 'UTF-8 ' ) . ' {clipped} ' ;
226+ if (mb_strlen ($ encoded ) > $ this ->options ->getMaxValueLength ()) {
227+ $ encoded = mb_substr ($ encoded , 0 , $ this ->options ->getMaxValueLength () - 10 , 'UTF-8 ' ) . ' {clipped} ' ;
230228 }
231229
232- return $ value ;
230+ return $ encoded ;
233231 }
234232
235233 /**
@@ -276,7 +274,11 @@ protected function serializeValue($value)
276274 return 'Array of length ' . \count ($ value );
277275 }
278276
279- return $ this ->serializeString ($ value );
277+ if (\is_string ($ value ) || (\is_object ($ value ) && method_exists ($ value , '__toString ' ))) {
278+ return $ this ->serializeString ((string ) $ value );
279+ }
280+
281+ return null ;
280282 }
281283
282284 private function formatDate (\DateTimeInterface $ date ): string
0 commit comments