Skip to content
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
53 changes: 53 additions & 0 deletions src/Illuminate/Log/Context/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Log\Context;

use __PHP_Incomplete_Class;
use Closure;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Log\Context\Events\ContextDehydrating as Dehydrating;
Expand Down Expand Up @@ -311,6 +312,58 @@ public function pushHidden($key, ...$values)
return $this;
}

/**
* Determine if the given value is in the given stack.
*
* @param string $key
* @param mixed $value
* @return bool
*
* @throws \RuntimeException
*/
public function stackContains(string $key, mixed $value): bool
{
if (! $this->isStackable($key)) {
throw new RuntimeException("Given key [{$key}] is not a stack.");
}

if (! array_key_exists($key, $this->data)) {
return false;
}

if ($value instanceof Closure) {
return $value($this->data[$key]);
}

return in_array($value, $this->data[$key]);
}

/**
* Determine if the given value is in the given hidden stack.
*
* @param string $key
* @param mixed $value
* @return bool
*
* @throws \RuntimeException
*/
public function hiddenStackContains(string $key, mixed $value): bool
{
if (! $this->isHiddenStackable($key)) {
throw new RuntimeException("Given key [{$key}] is not a stack.");
}

if (! array_key_exists($key, $this->hidden)) {
return false;
}

if ($value instanceof Closure) {
return $value($this->data[$key]);
}

return in_array($value, $this->hidden[$key]);
}

/**
* Determine if a given key can be used as a stack.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Support/Facades/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
* @method static \Illuminate\Log\Context\Repository addHiddenIf(string $key, mixed $value)
* @method static \Illuminate\Log\Context\Repository push(string $key, mixed ...$values)
* @method static \Illuminate\Log\Context\Repository pushHidden(string $key, mixed ...$values)
* @method static bool stackContains(string $key, mixed $value)
* @method static bool hiddenStackContains(string $key, mixed $value)
* @method static bool isEmpty()
* @method static \Illuminate\Log\Context\Repository dehydrating(callable $callback)
* @method static \Illuminate\Log\Context\Repository hydrated(callable $callback)
Expand Down
33 changes: 32 additions & 1 deletion tests/Log/ContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ public function test_it_can_serialize_values()
'data' => [
'string' => 's:6:"string";',
'bool' => 'b:0;',
'bool' => 'b:0;',
'int' => 'i:5;',
'float' => 'd:5.5;',
'null' => 'N;',
Expand Down Expand Up @@ -206,6 +205,38 @@ public function test_it_can_check_if_context_has_been_set()
$this->assertFalse(Context::has('unset'));
}

public function test_it_can_check_if_value_is_in_context_stack()
{
Context::push('foo', 'bar', 'lorem');

$this->assertTrue(Context::stackContains('foo', 'bar'));
$this->assertTrue(Context::stackContains('foo', 'lorem'));
$this->assertFalse(Context::stackContains('foo', 'doesNotExist'));
}

public function test_it_can_check_if_value_is_in_context_stack_with_closures()
{
Context::push('foo', 'bar', ['lorem'], 123);

$this->assertTrue(Context::stackContains('foo', fn ($stack) => in_array('bar', $stack, true)));
}

public function test_it_can_check_if_value_is_in_hidden_context_stack()
{
Context::pushHidden('foo', 'bar', 'lorem');

$this->assertTrue(Context::hiddenStackContains('foo', 'bar'));
$this->assertTrue(Context::hiddenStackContains('foo', 'lorem'));
$this->assertFalse(Context::hiddenStackContains('foo', 'doesNotExist'));
}

public function test_it_cannot_check_if_hidden_value_is_in_non_hidden_context_stack()
{
Context::pushHidden('foo', 'bar', 'lorem');

$this->assertFalse(Context::stackContains('foo', 'bar'));
}

public function test_it_can_get_all_values()
{
Context::add('foo', 'bar');
Expand Down