Skip to content

Commit 4980576

Browse files
[11.x] Add in() and inHidden() functions to Context Stacks (#52346)
* Add tests for in & inHidden functions * Implement logic for new functions * Add to Facade PHPDoc * Style fix * Add optional passing of strictness * formatting --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 630df64 commit 4980576

File tree

3 files changed

+87
-1
lines changed

3 files changed

+87
-1
lines changed

src/Illuminate/Log/Context/Repository.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Log\Context;
44

55
use __PHP_Incomplete_Class;
6+
use Closure;
67
use Illuminate\Contracts\Events\Dispatcher;
78
use Illuminate\Database\Eloquent\ModelNotFoundException;
89
use Illuminate\Log\Context\Events\ContextDehydrating as Dehydrating;
@@ -311,6 +312,58 @@ public function pushHidden($key, ...$values)
311312
return $this;
312313
}
313314

315+
/**
316+
* Determine if the given value is in the given stack.
317+
*
318+
* @param string $key
319+
* @param mixed $value
320+
* @return bool
321+
*
322+
* @throws \RuntimeException
323+
*/
324+
public function stackContains(string $key, mixed $value): bool
325+
{
326+
if (! $this->isStackable($key)) {
327+
throw new RuntimeException("Given key [{$key}] is not a stack.");
328+
}
329+
330+
if (! array_key_exists($key, $this->data)) {
331+
return false;
332+
}
333+
334+
if ($value instanceof Closure) {
335+
return $value($this->data[$key]);
336+
}
337+
338+
return in_array($value, $this->data[$key]);
339+
}
340+
341+
/**
342+
* Determine if the given value is in the given hidden stack.
343+
*
344+
* @param string $key
345+
* @param mixed $value
346+
* @return bool
347+
*
348+
* @throws \RuntimeException
349+
*/
350+
public function hiddenStackContains(string $key, mixed $value): bool
351+
{
352+
if (! $this->isHiddenStackable($key)) {
353+
throw new RuntimeException("Given key [{$key}] is not a stack.");
354+
}
355+
356+
if (! array_key_exists($key, $this->hidden)) {
357+
return false;
358+
}
359+
360+
if ($value instanceof Closure) {
361+
return $value($this->data[$key]);
362+
}
363+
364+
return in_array($value, $this->hidden[$key]);
365+
}
366+
314367
/**
315368
* Determine if a given key can be used as a stack.
316369
*

src/Illuminate/Support/Facades/Context.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
* @method static \Illuminate\Log\Context\Repository addHiddenIf(string $key, mixed $value)
2222
* @method static \Illuminate\Log\Context\Repository push(string $key, mixed ...$values)
2323
* @method static \Illuminate\Log\Context\Repository pushHidden(string $key, mixed ...$values)
24+
* @method static bool stackContains(string $key, mixed $value)
25+
* @method static bool hiddenStackContains(string $key, mixed $value)
2426
* @method static bool isEmpty()
2527
* @method static \Illuminate\Log\Context\Repository dehydrating(callable $callback)
2628
* @method static \Illuminate\Log\Context\Repository hydrated(callable $callback)

tests/Log/ContextTest.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ public function test_it_can_serialize_values()
136136
'data' => [
137137
'string' => 's:6:"string";',
138138
'bool' => 'b:0;',
139-
'bool' => 'b:0;',
140139
'int' => 'i:5;',
141140
'float' => 'd:5.5;',
142141
'null' => 'N;',
@@ -206,6 +205,38 @@ public function test_it_can_check_if_context_has_been_set()
206205
$this->assertFalse(Context::has('unset'));
207206
}
208207

208+
public function test_it_can_check_if_value_is_in_context_stack()
209+
{
210+
Context::push('foo', 'bar', 'lorem');
211+
212+
$this->assertTrue(Context::stackContains('foo', 'bar'));
213+
$this->assertTrue(Context::stackContains('foo', 'lorem'));
214+
$this->assertFalse(Context::stackContains('foo', 'doesNotExist'));
215+
}
216+
217+
public function test_it_can_check_if_value_is_in_context_stack_with_closures()
218+
{
219+
Context::push('foo', 'bar', ['lorem'], 123);
220+
221+
$this->assertTrue(Context::stackContains('foo', fn ($stack) => in_array('bar', $stack, true)));
222+
}
223+
224+
public function test_it_can_check_if_value_is_in_hidden_context_stack()
225+
{
226+
Context::pushHidden('foo', 'bar', 'lorem');
227+
228+
$this->assertTrue(Context::hiddenStackContains('foo', 'bar'));
229+
$this->assertTrue(Context::hiddenStackContains('foo', 'lorem'));
230+
$this->assertFalse(Context::hiddenStackContains('foo', 'doesNotExist'));
231+
}
232+
233+
public function test_it_cannot_check_if_hidden_value_is_in_non_hidden_context_stack()
234+
{
235+
Context::pushHidden('foo', 'bar', 'lorem');
236+
237+
$this->assertFalse(Context::stackContains('foo', 'bar'));
238+
}
239+
209240
public function test_it_can_get_all_values()
210241
{
211242
Context::add('foo', 'bar');

0 commit comments

Comments
 (0)