File tree Expand file tree Collapse file tree 2 files changed +25
-1
lines changed Expand file tree Collapse file tree 2 files changed +25
-1
lines changed Original file line number Diff line number Diff line change @@ -41,9 +41,22 @@ public static function asParameter($value)
41
41
return self ::emptySequenceToArray ($ value ) ??
42
42
self ::emptyDictionaryToStdClass ($ value ) ??
43
43
self ::filledIterableToArray ($ value ) ??
44
+ self ::stringableToString ($ value ) ??
44
45
self ::filterInvalidType ($ value );
45
46
}
46
47
48
+ /**
49
+ * @param mixed $value
50
+ */
51
+ private static function stringableToString ($ value ): ?string
52
+ {
53
+ if (is_object ($ value ) && method_exists ($ value , '__toString ' )) {
54
+ return (string ) $ value ;
55
+ }
56
+
57
+ return null ;
58
+ }
59
+
47
60
/**
48
61
* @param mixed $value
49
62
*
@@ -52,7 +65,7 @@ public static function asParameter($value)
52
65
private static function filterInvalidType ($ value )
53
66
{
54
67
if ($ value !== null && !is_scalar ($ value )) {
55
- throw new InvalidArgumentException ('Parameters must be iterable, scalar or null ' );
68
+ throw new InvalidArgumentException ('Parameters must be iterable, scalar, null or stringable ' );
56
69
}
57
70
58
71
return $ value ;
Original file line number Diff line number Diff line change @@ -135,4 +135,15 @@ public function testAsParmeterEmptyArray(): void
135
135
$ result = ParameterHelper::asParameter ([]);
136
136
self ::assertInstanceOf (stdClass::class, $ result );
137
137
}
138
+
139
+ public function testStringable (): void
140
+ {
141
+ $ result = ParameterHelper::asParameter (new class () {
142
+ public function __toString (): string
143
+ {
144
+ return 'abc ' ;
145
+ }
146
+ });
147
+ self ::assertEquals ('abc ' , $ result );
148
+ }
138
149
}
You can’t perform that action at this time.
0 commit comments