@@ -32,25 +32,68 @@ public static function asMap(iterable $iterable): Map
32
32
}
33
33
34
34
/**
35
- * @param iterable|scalar|null $value
35
+ * @param mixed $value
36
36
*
37
37
* @return iterable|scalar|stdClass|null
38
38
*/
39
39
public static function asParameter ($ value )
40
+ {
41
+ return self ::emptySequenceToArray ($ value ) ??
42
+ self ::emptyDictionaryToStdClass ($ value ) ??
43
+ self ::filledIterableToArray ($ value ) ??
44
+ self ::filterInvalidType ($ value );
45
+ }
46
+
47
+ /**
48
+ * @param mixed $value
49
+ *
50
+ * @return scalar|null
51
+ */
52
+ private static function filterInvalidType ($ value )
53
+ {
54
+ if ($ value !== null && !is_scalar ($ value )) {
55
+ throw new InvalidArgumentException ('Parameters must be iterable, scalar or null ' );
56
+ }
57
+
58
+ return $ value ;
59
+ }
60
+
61
+ /**
62
+ * @param mixed $value
63
+ */
64
+ private static function emptySequenceToArray ($ value ): ?array
40
65
{
41
66
if ($ value instanceof Sequence && $ value ->count () === 0 ) {
42
67
return [];
43
68
}
69
+
70
+ return null ;
71
+ }
72
+
73
+ /**
74
+ * @param mixed $value
75
+ */
76
+ private static function emptyDictionaryToStdClass ($ value ): ?stdClass
77
+ {
44
78
if (($ value instanceof Map && $ value ->count () === 0 ) ||
45
79
(is_array ($ value ) && count ($ value ) === 0 )
46
80
) {
47
81
return new stdClass ();
48
82
}
83
+
84
+ return null ;
85
+ }
86
+
87
+ /**
88
+ * @param mixed $value
89
+ */
90
+ private static function filledIterableToArray ($ value ): ?array
91
+ {
49
92
if (is_iterable ($ value )) {
50
93
return self ::iterableToArray ($ value );
51
94
}
52
95
53
- return $ value ;
96
+ return null ;
54
97
}
55
98
56
99
/**
@@ -82,7 +125,6 @@ private static function iterableToArray(iterable $value): array
82
125
*/
83
126
foreach ($ value as $ key => $ val ) {
84
127
if (is_int ($ key ) || is_string ($ key )) {
85
- /** @psalm-suppress MixedAssignment */
86
128
$ tbr [$ key ] = self ::asParameter ($ val );
87
129
} else {
88
130
$ msg = 'Iterable parameters must have an integer or string as key values, ' .gettype ($ key ).' received. ' ;
0 commit comments