@@ -34,7 +34,7 @@ public function __construct()
3434 * @param mixed $value The value to be stored in the context.
3535 * @return $this
3636 */
37- public function set (string |array $ key , mixed $ value ): self
37+ public function set (string |array $ key , mixed $ value = null ): self
3838 {
3939 if (is_array ($ key )) {
4040 $ this ->data = array_merge ($ this ->data , $ key );
@@ -52,7 +52,7 @@ public function set(string|array $key, mixed $value): self
5252 * @param mixed $value The value to be stored in the context.
5353 * @return $this
5454 */
55- public function setHidden (string |array $ key , mixed $ value ): self
55+ public function setHidden (string |array $ key , mixed $ value = null ): self
5656 {
5757 if (is_array ($ key )) {
5858 $ this ->hiddenData = array_merge ($ this ->hiddenData , $ key );
@@ -75,6 +75,34 @@ public function get(string $key, mixed $default = null): mixed
7575 return $ this ->data [$ key ] ?? $ default ;
7676 }
7777
78+ /**
79+ * Get only the specified keys from the context. If a key does not exist, it will be ignored.
80+ *
81+ * @param string|array<string> $keys An array of keys to retrieve from the context.
82+ * @return array<string, mixed> An array of key-value pairs for the specified keys that exist in the context.
83+ */
84+ public function getOnly (string |array $ keys ): array
85+ {
86+ if (is_string ($ keys )) {
87+ $ keys = [$ keys ];
88+ }
89+ return array_filter ($ this ->data , fn ($ k ) => in_array ($ k , $ keys ), ARRAY_FILTER_USE_KEY );
90+ }
91+
92+ /**
93+ * Get all keys from the context except the specified keys.
94+ *
95+ * @param string|array<string> $keys An array of keys to exclude from the context.
96+ * @return array<string, mixed> An array of key-value pairs for all keys in the context except the specified keys.
97+ */
98+ public function getExcept (string |array $ keys ): array
99+ {
100+ if (is_string ($ keys )) {
101+ $ keys = [$ keys ];
102+ }
103+ return array_filter ($ this ->data , fn ($ k ) => !in_array ($ k , $ keys ), ARRAY_FILTER_USE_KEY );
104+ }
105+
78106 /**
79107 * Get all data from the context
80108 *
@@ -97,6 +125,34 @@ public function getHidden(string $key, mixed $default = null): mixed
97125 return $ this ->hiddenData [$ key ] ?? $ default ;
98126 }
99127
128+ /**
129+ * Get only the specified keys from the hidden context. If a key does not exist, it will be ignored.
130+ *
131+ * @param string|array<string> $keys An array of keys to retrieve from the hidden context.
132+ * @return array<string, mixed> An array of key-value pairs for the specified keys that exist in the hidden context.
133+ */
134+ public function getOnlyHidden (string |array $ keys ): array
135+ {
136+ if (is_string ($ keys )) {
137+ $ keys = [$ keys ];
138+ }
139+ return array_filter ($ this ->hiddenData , fn ($ k ) => in_array ($ k , $ keys ), ARRAY_FILTER_USE_KEY );
140+ }
141+
142+ /**
143+ * Get all keys from the hidden context except the specified keys.
144+ *
145+ * @param string|array<string> $keys An array of keys to exclude from the hidden context.
146+ * @return array<string, mixed> An array of key-value pairs for all keys in the hidden context except the specified keys.
147+ */
148+ public function getExceptHidden (string |array $ keys ): array
149+ {
150+ if (is_string ($ keys )) {
151+ $ keys = [$ keys ];
152+ }
153+ return array_filter ($ this ->hiddenData , fn ($ k ) => !in_array ($ k , $ keys ), ARRAY_FILTER_USE_KEY );
154+ }
155+
100156 /**
101157 * Get all hidden data from the context
102158 *
@@ -107,6 +163,17 @@ public function getAllHidden(): array
107163 return $ this ->hiddenData ;
108164 }
109165
166+ /**
167+ * Check if a key does not exist in the context. Exactly the opposite of `has()`.
168+ *
169+ * @param string $key The key to check for non-existence in the context.
170+ * @return bool True if the key does not exist in the context, false otherwise.
171+ */
172+ public function missing (string $ key ): bool
173+ {
174+ return !$ this ->has ($ key );
175+ }
176+
110177 /**
111178 * Check if a key exists in the context.
112179 *
@@ -118,6 +185,17 @@ public function has(string $key): bool
118185 return array_key_exists ($ key , $ this ->data );
119186 }
120187
188+ /**
189+ * Check if a key does not exist in the hidden context. Exactly the opposite of `hasHidden()`.
190+ *
191+ * @param string $key The key to check for non-existence in the hidden context.
192+ * @return bool True if the key does not exist in the hidden context, false otherwise.
193+ */
194+ public function missingHidden (string $ key ): bool
195+ {
196+ return !$ this ->hasHidden ($ key );
197+ }
198+
121199 /**
122200 * Check if a key exists in the hidden context.
123201 *
@@ -132,23 +210,37 @@ public function hasHidden(string $key): bool
132210 /**
133211 * Remove a key-value pair from the context by its key.
134212 *
135- * @param string $key The key to identify the data to be removed from the context.
213+ * @param string|array<string> $key The key to identify the data to be removed from the context.
136214 * @return $this
137215 */
138- public function remove (string $ key ): self
216+ public function remove (string | array $ key ): self
139217 {
218+ if (is_array ($ key )) {
219+ foreach ($ key as $ k ) {
220+ unset($ this ->data [$ k ]);
221+ }
222+ return $ this ;
223+ }
224+
140225 unset($ this ->data [$ key ]);
141226 return $ this ;
142227 }
143228
144229 /**
145230 * Remove a key-value pair from the hidden context by its key.
146231 *
147- * @param string $key The key to identify the data to be removed from the hidden context.
232+ * @param string|array<string> $key The key to identify the data to be removed from the hidden context.
148233 * @return $this
149234 */
150- public function removeHidden (string $ key ): self
235+ public function removeHidden (string | array $ key ): self
151236 {
237+ if (is_array ($ key )) {
238+ foreach ($ key as $ k ) {
239+ unset($ this ->hiddenData [$ k ]);
240+ }
241+ return $ this ;
242+ }
243+
152244 unset($ this ->hiddenData [$ key ]);
153245 return $ this ;
154246 }
0 commit comments