Skip to content

Commit 6faf8de

Browse files
xurshudyanXurshudyan
andauthored
Add except and exceptHidden methods to Context class (#55692)
Co-authored-by: Xurshudyan <[email protected]>
1 parent f033c4a commit 6faf8de

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/Illuminate/Log/Context/Repository.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,28 @@ public function onlyHidden($keys)
193193
return array_intersect_key($this->hidden, array_flip($keys));
194194
}
195195

196+
/**
197+
* Retrieve all values except those with the given keys.
198+
*
199+
* @param array<int, string> $keys
200+
* @return array<string, mixed>
201+
*/
202+
public function except($keys)
203+
{
204+
return array_diff_key($this->data, array_flip($keys));
205+
}
206+
207+
/**
208+
* Retrieve all hidden values except those with the given keys.
209+
*
210+
* @param array<int, string> $keys
211+
* @return array<string, mixed>
212+
*/
213+
public function exceptHidden($keys)
214+
{
215+
return array_diff_key($this->hidden, array_flip($keys));
216+
}
217+
196218
/**
197219
* Add a context value.
198220
*

tests/Log/ContextTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,34 @@ public function test_it_can_retrieve_subset_of_context()
364364
]));
365365
}
366366

367+
public function test_it_can_exclude_subset_of_context()
368+
{
369+
Context::add('parent.child.1', 5);
370+
Context::add('parent.child.2', 6);
371+
Context::add('another', 7);
372+
373+
$this->assertSame([
374+
'another' => 7,
375+
], Context::except([
376+
'parent.child.1',
377+
'parent.child.2',
378+
]));
379+
}
380+
381+
public function test_it_can_exclude_subset_of_hidden_context()
382+
{
383+
Context::addHidden('parent.child.1', 5);
384+
Context::addHidden('parent.child.2', 6);
385+
Context::addHidden('another', 7);
386+
387+
$this->assertSame([
388+
'another' => 7,
389+
], Context::exceptHidden([
390+
'parent.child.1',
391+
'parent.child.2',
392+
]));
393+
}
394+
367395
public function test_it_adds_context_to_logging()
368396
{
369397
$path = storage_path('logs/laravel.log');

0 commit comments

Comments
 (0)