@@ -53,7 +53,9 @@ class Cache implements CacheInterface
53
53
private array $ items = [];
54
54
private static ?self $ instance = null ;
55
55
56
- private function __construct () {}
56
+ private function __construct ()
57
+ {
58
+ }
57
59
58
60
public static function getInstance (): self
59
61
{
@@ -68,11 +70,11 @@ public static function getInstance(): self
68
70
* @template U
69
71
*
70
72
* @param string $key
71
- * @param U $default
73
+ * @param U $default
72
74
*
73
75
* @return T|U
74
76
*/
75
- public function get ($ key , $ default = null )
77
+ public function get (string $ key , mixed $ default = null ): mixed
76
78
{
77
79
$ this ->assertValidKey ($ key );
78
80
@@ -98,7 +100,7 @@ public function clear(): bool
98
100
*
99
101
* @throws InvalidCacheArgumentException
100
102
*/
101
- public function deleteMultiple ($ keys ): bool
103
+ public function deleteMultiple (iterable $ keys ): bool
102
104
{
103
105
$ this ->assertIterable ($ keys );
104
106
@@ -110,11 +112,9 @@ public function deleteMultiple($keys): bool
110
112
}
111
113
112
114
/**
113
- * @param string $key
114
- * @param T $value
115
- * @param int|DateInterval|null $ttl
115
+ * @param T $value
116
116
*/
117
- public function set ($ key , $ value , $ ttl = null ): bool
117
+ public function set (string $ key , mixed $ value , int | DateInterval | null $ ttl = null ): bool
118
118
{
119
119
$ this ->assertValidKey ($ key );
120
120
@@ -150,7 +150,7 @@ public function delete($key): bool
150
150
* @template U
151
151
*
152
152
* @param iterable<string> $keys
153
- * @param U $default
153
+ * @param U $default
154
154
*
155
155
* @return Generator<string, T|U>
156
156
*/
@@ -173,18 +173,18 @@ public function getMultiple($keys, $default = null): Generator
173
173
}
174
174
175
175
/**
176
- * @param iterable<string, T> $values
176
+ * @param iterable<string, T> $values
177
177
* @param int|DateInterval|null $ttl
178
178
*
179
179
* @throws InvalidCacheArgumentException
180
180
*/
181
- public function setMultiple ($ values , $ ttl = null ): bool
181
+ public function setMultiple (iterable $ values , null | int | DateInterval $ ttl = null ): bool
182
182
{
183
183
$ this ->assertIterable ($ values );
184
184
185
185
foreach ($ values as $ key => $ value ) {
186
186
if (is_int ($ key )) {
187
- $ key = (string ) $ key ;
187
+ $ key = (string )$ key ;
188
188
}
189
189
$ this ->set ($ key , $ value , $ ttl );
190
190
}
@@ -193,38 +193,20 @@ public function setMultiple($values, $ttl = null): bool
193
193
}
194
194
195
195
/**
196
- * @param-out string $key
197
- *
198
- * @throws InvalidCacheArgumentException
199
- */
200
- private function assertValidKey (mixed $ key ): void
201
- {
202
- if (is_string ($ key )) {
203
- if ($ key === '' ||
204
- str_contains ($ key , '{ ' ) ||
205
- str_contains ($ key , '} ' ) ||
206
- str_contains ($ key , '( ' ) ||
207
- str_contains ($ key , ') ' ) ||
208
- str_contains ($ key , '/ ' ) ||
209
- str_contains ($ key , '\\' ) ||
210
- str_contains ($ key , '@ ' ) ||
211
- str_contains ($ key , ': ' )
212
- ) {
213
- throw new InvalidCacheArgumentException ();
214
- }
215
- } else {
216
- throw new InvalidCacheArgumentException ();
217
- }
218
- }
219
-
220
- /**
221
- * @param-out iterable<string> $keys
222
- *
223
196
* @throws InvalidCacheArgumentException
224
197
*/
225
- private function assertIterable ( mixed $ keys ): void
198
+ private function assertValidKey ( string $ key ): void
226
199
{
227
- if (!is_iterable ($ keys )) {
200
+ if ($ key === '' ||
201
+ str_contains ($ key , '{ ' ) ||
202
+ str_contains ($ key , '} ' ) ||
203
+ str_contains ($ key , '( ' ) ||
204
+ str_contains ($ key , ') ' ) ||
205
+ str_contains ($ key , '/ ' ) ||
206
+ str_contains ($ key , '\\' ) ||
207
+ str_contains ($ key , '@ ' ) ||
208
+ str_contains ($ key , ': ' )
209
+ ) {
228
210
throw new InvalidCacheArgumentException ();
229
211
}
230
212
}
0 commit comments