8000 [12.x] Add `except` and `exceptHidden` methods to `Context` class by xurshudyan · Pull Request #55692 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

[12.x] Add except and exceptHidden methods to Context class #55692

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
8000 merged 1 commit into from
May 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/Illuminate/Log/Context/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,28 @@ public function onlyHidden($keys)
return array_intersect_key($this->hidden, array_flip($keys));
}

/**
* Retrieve all values except those with the given keys.
*
* @param array<int, string> $keys
* @return array<string, mixed>
*/
public function except($keys)
{
return array_diff_key($this->data, array_flip($keys));
}

/**
* Retrieve all hidden values except those with the given keys.
*
* @param array<int, string> $keys
* @return array<string, mixed>
*/
public function exceptHidden($keys)
{
return array_diff_key($this->hidden, array_flip($keys));
}

/**
* Add a context value.
*
Expand Down
28 changes: 28 additions & 0 deletions tests/Log/ContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,34 @@ public function test_it_can_retrieve_subset_of_context()
]));
}

public function test_it_can_exclude_subset_of_context()
{
Context::add('parent.child.1', 5);
Context::add('parent.child.2', 6);
Context::add('another', 7);

$this->assertSame([
'another' => 7,
], Context::except([
'parent.child.1',
'parent.child.2',
]));
}

public function test_it_can_exclude_subset_of_hidden_context()
{
Context::addHidden('parent.child.1', 5);
Context::addHidden('parent.child.2', 6);
Context::addHidden('another', 7);

$this->assertSame([
'another' => 7,
], Context::exceptHidden([
'parent.child.1',
'parent.child.2',
]));
}

public function test_it_adds_context_to_logging()
{
$path = storage_path('logs/laravel.log');
Expand Down
0