11<?php
22
3+ /**
4+ * This file is part of CodeIgniter 4 framework.
5+ *
6+ * (c) CodeIgniter Foundation <[email protected] > 7+ *
8+ * For the full copyright and license information, please view
9+ * the LICENSE file that was distributed with this source code.
10+ */
11+
312namespace CodeIgniter \Context ;
413
514class Context
@@ -23,51 +32,58 @@ class Context
2332 */
2433 public function __construct ()
2534 {
26- $ this ->data = [];
35+ $ this ->data = [];
2736 $ this ->hiddenData = [];
2837 }
2938
3039 /**
3140 * Set a key-value pair to the context.
3241 *
33- * @param string|array<string, mixed> $key The key to identify the data. Can be a string or an array of key-value pairs.
34- * @param mixed $value The value to be stored in the context.
42+ * @param array<string, mixed>|string $key The key to identify the data. Can be a string or an array of key-value pairs.
43+ * @param mixed $value The value to be stored in the context.
44+ *
3545 * @return $this
3646 */
37- public function set (string | array $ key , mixed $ value = null ): self
47+ public function set (array | string $ key , mixed $ value = null ): self
3848 {
3949 if (is_array ($ key )) {
4050 $ this ->data = array_merge ($ this ->data , $ key );
51+
4152 return $ this ;
4253 }
4354
4455 $ this ->data [$ key ] = $ value ;
56+
4557 return $ this ;
4658 }
4759
4860 /**
4961 * Set a hidden key-value pair to the context. This data will not be included in logs.
5062 *
51- * @param string|array<string, mixed> $key The key to identify the data. Can be a string or an array of key-value pairs.
52- * @param mixed $value The value to be stored in the context.
63+ * @param array<string, mixed>|string $key The key to identify the data. Can be a string or an array of key-value pairs.
64+ * @param mixed $value The value to be stored in the context.
65+ *
5366 * @return $this
5467 */
55- public function setHidden (string | array $ key , mixed $ value = null ): self
68+ public function setHidden (array | string $ key , mixed $ value = null ): self
5669 {
5770 if (is_array ($ key )) {
5871 $ this ->hiddenData = array_merge ($ this ->hiddenData , $ key );
72+
5973 return $ this ;
6074 }
6175
6276 $ this ->hiddenData [$ key ] = $ value ;
77+
6378 return $ this ;
6479 }
6580
6681 /**
6782 * Get a value from the context by its key, or return a default value if the key does not exist.
6883 *
69- * @param string $key The key to identify the data.
84+ * @param string $key The key to identify the data.
7085 * @param mixed|null $default The default value to return if the key does not exist in the context.
86+ *
7187 * @return mixed The value associated with the key, or the default value if the key does not exist.
7288 */
7389 public function get (string $ key , mixed $ default = null ): mixed
@@ -78,29 +94,33 @@ public function get(string $key, mixed $default = null): mixed
7894 /**
7995 * Get only the specified keys from the context. If a key does not exist, it will be ignored.
8096 *
81- * @param string|array<string> $keys An array of keys to retrieve from the context.
97+ * @param list<string>|string $keys An array of keys to retrieve from the context.
98+ *
8299 * @return array<string, mixed> An array of key-value pairs for the specified keys that exist in the context.
83100 */
84- public function getOnly (string | array $ keys ): array
101+ public function getOnly (array | string $ keys ): array
85102 {
86103 if (is_string ($ keys )) {
87104 $ keys = [$ keys ];
88105 }
89- return array_filter ($ this ->data , fn ($ k ) => in_array ($ k , $ keys ), ARRAY_FILTER_USE_KEY );
106+
107+ return array_filter ($ this ->data , static fn ($ k ) => in_array ($ k , $ keys , true ), ARRAY_FILTER_USE_KEY );
90108 }
91109
92110 /**
93111 * Get all keys from the context except the specified keys.
94112 *
95- * @param string|array<string> $keys An array of keys to exclude from the context.
113+ * @param list<string>|string $keys An array of keys to exclude from the context.
114+ *
96115 * @return array<string, mixed> An array of key-value pairs for all keys in the context except the specified keys.
97116 */
98- public function getExcept (string | array $ keys ): array
117+ public function getExcept (array | string $ keys ): array
99118 {
100119 if (is_string ($ keys )) {
101120 $ keys = [$ keys ];
102121 }
103- return array_filter ($ this ->data , fn ($ k ) => !in_array ($ k , $ keys ), ARRAY_FILTER_USE_KEY );
122+
123+ return array_filter ($ this ->data , static fn ($ k ) => ! in_array ($ k , $ keys , true ), ARRAY_FILTER_USE_KEY );
104124 }
105125
106126 /**
@@ -116,8 +136,9 @@ public function getAll(): array
116136 /**
117137 * Get a hidden value from the context by its key, or return a default value if the key does not exist.
118138 *
119- * @param string $key The key to identify the data.
139+ * @param string $key The key to identify the data.
120140 * @param mixed|null $default The default value to return if the key does not exist in the context.
141+ *
121142 * @return mixed The value associated with the key, or the default value if the key does not exist.
122143 */
123144 public function getHidden (string $ key , mixed $ default = null ): mixed
@@ -128,29 +149,33 @@ public function getHidden(string $key, mixed $default = null): mixed
128149 /**
129150 * Get only the specified keys from the hidden context. If a key does not exist, it will be ignored.
130151 *
131- * @param string|array<string> $keys An array of keys to retrieve from the hidden context.
152+ * @param list<string>|string $keys An array of keys to retrieve from the hidden context.
153+ *
132154 * @return array<string, mixed> An array of key-value pairs for the specified keys that exist in the hidden context.
133155 */
134- public function getOnlyHidden (string | array $ keys ): array
156+ public function getOnlyHidden (array | string $ keys ): array
135157 {
136158 if (is_string ($ keys )) {
137159 $ keys = [$ keys ];
138160 }
139- return array_filter ($ this ->hiddenData , fn ($ k ) => in_array ($ k , $ keys ), ARRAY_FILTER_USE_KEY );
161+
162+ return array_filter ($ this ->hiddenData , static fn ($ k ) => in_array ($ k , $ keys , true ), ARRAY_FILTER_USE_KEY );
140163 }
141164
142165 /**
143166 * Get all keys from the hidden context except the specified keys.
144167 *
145- * @param string|array<string> $keys An array of keys to exclude from the hidden context.
168+ * @param list<string>|string $keys An array of keys to exclude from the hidden context.
169+ *
146170 * @return array<string, mixed> An array of key-value pairs for all keys in the hidden context except the specified keys.
147171 */
148- public function getExceptHidden (string | array $ keys ): array
172+ public function getExceptHidden (array | string $ keys ): array
149173 {
150174 if (is_string ($ keys )) {
151175 $ keys = [$ keys ];
152176 }
153- return array_filter ($ this ->hiddenData , fn ($ k ) => !in_array ($ k , $ keys ), ARRAY_FILTER_USE_KEY );
177+
178+ return array_filter ($ this ->hiddenData , static fn ($ k ) => ! in_array ($ k , $ keys , true ), ARRAY_FILTER_USE_KEY );
154179 }
155180
156181 /**
@@ -167,17 +192,19 @@ public function getAllHidden(): array
167192 * Check if a key does not exist in the context. Exactly the opposite of `has()`.
168193 *
169194 * @param string $key The key to check for non-existence in the context.
195+ *
170196 * @return bool True if the key does not exist in the context, false otherwise.
171197 */
172198 public function missing (string $ key ): bool
173199 {
174- return !$ this ->has ($ key );
200+ return ! $ this ->has ($ key );
175201 }
176202
177203 /**
178204 * Check if a key exists in the context.
179205 *
180206 * @param string $key The key to check for existence in the context.
207+ *
181208 * @return bool True if the key exists in the context, false otherwise.
182209 */
183210 public function has (string $ key ): bool
@@ -189,17 +216,19 @@ public function has(string $key): bool
189216 * Check if a key does not exist in the hidden context. Exactly the opposite of `hasHidden()`.
190217 *
191218 * @param string $key The key to check for non-existence in the hidden context.
219+ *
192220 * @return bool True if the key does not exist in the hidden context, false otherwise.
193221 */
194222 public function missingHidden (string $ key ): bool
195223 {
196- return !$ this ->hasHidden ($ key );
224+ return ! $ this ->hasHidden ($ key );
197225 }
198226
199227 /**
200228 * Check if a key exists in the hidden context.
201229 *
202230 * @param string $key The key to check for existence in the hidden context.
231+ *
203232 * @return bool True if the key exists in the hidden context, false otherwise.
204233 */
205234 public function hasHidden (string $ key ): bool
@@ -210,38 +239,44 @@ public function hasHidden(string $key): bool
210239 /**
211240 * Remove a key-value pair from the context by its key.
212241 *
213- * @param string|array<string> $key The key to identify the data to be removed from the context.
242+ * @param list<string>|string $key The key to identify the data to be removed from the context.
243+ *
214244 * @return $this
215245 */
216- public function remove (string | array $ key ): self
246+ public function remove (array | string $ key ): self
217247 {
218248 if (is_array ($ key )) {
219249 foreach ($ key as $ k ) {
220250 unset($ this ->data [$ k ]);
221251 }
252+
222253 return $ this ;
223254 }
224255
225256 unset($ this ->data [$ key ]);
257+
226258 return $ this ;
227259 }
228260
229261 /**
230262 * Remove a key-value pair from the hidden context by its key.
231263 *
232- * @param string|array<string> $key The key to identify the data to be removed from the hidden context.
264+ * @param list<string>|string $key The key to identify the data to be removed from the hidden context.
265+ *
233266 * @return $this
234267 */
235- public function removeHidden (string | array $ key ): self
268+ public function removeHidden (array | string $ key ): self
236269 {
237270 if (is_array ($ key )) {
238271 foreach ($ key as $ k ) {
239272 unset($ this ->hiddenData [$ k ]);
240273 }
274+
241275 return $ this ;
242276 }
243277
244278 unset($ this ->hiddenData [$ key ]);
279+
245280 return $ this ;
246281 }
247282
@@ -254,6 +289,7 @@ public function clearAll(): self
254289 {
255290 $ this ->clear ();
256291 $ this ->clearHidden ();
292+
257293 return $ this ;
258294 }
259295
@@ -265,6 +301,7 @@ public function clearAll(): self
265301 public function clear (): self
266302 {
267303 $ this ->data = [];
304+
268305 return $ this ;
269306 }
270307
@@ -276,6 +313,7 @@ public function clear(): self
276313 public function clearHidden (): self
277314 {
278315 $ this ->hiddenData = [];
316+
279317 return $ this ;
280318 }
281319}
0 commit comments