Skip to content

Commit 454eb0d

Browse files
[11.x] Add Context "missing" method (#54499)
* Add missing method * Fix test * Fix * formatting * update facade --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent d80e1ef commit 454eb0d

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

src/Illuminate/Log/Context/Repository.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,17 @@ public function has($key)
6666
return array_key_exists($key, $this->data);
6767
}
6868

69+
/**
70+
* Determine if the given key is missing.
71+
*
72+
* @param string $key
73+
* @return bool
74+
*/
75+
public function missing($key)
76+
{
77+
return ! $this->has($key);
78+
}
79+
6980
/**
7081
* Determine if the given key exists within the hidden context data.
7182
*
@@ -77,6 +88,17 @@ public function hasHidden($key)
7788
return array_key_exists($key, $this->hidden);
7889
}
7990

91+
/**
92+
* Determine if the given key is missing within the hidden context data.
93+
*
94+
* @param string $key
95+
* @return bool
96+
*/
97+
public function missingHidden($key)
98+
{
99+
return ! $this->hasHidden($key);
100+
}
101+
80102
/**
81103
* Retrieve all the context data.
82104
*

src/Illuminate/Support/Facades/Context.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
/**
66
* @method static bool has(string $key)
7+
* @method static bool missing(string $key)
78
* @method static bool hasHidden(string $key)
9+
* @method static bool missingHidden(string $key)
810
* @method static array all()
911
* @method static array allHidden()
1012
* @method static mixed get(string $key, mixed $default = null)

tests/Log/ContextTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,14 @@ public function test_it_can_check_if_context_has_been_set()
263263
$this->assertFalse(Context::has('unset'));
264264
}
265265

266+
public function test_it_can_check_if_context_is_missing()
267+
{
268+
Context::add('foo', 'bar');
269+
270+
$this->assertTrue(Context::missing('lorem'));
271+
$this->assertFalse(Context::missing('foo'));
272+
}
273+
266274
public function test_it_can_check_if_value_is_in_context_stack()
267275
{
268276
Context::push('foo', 'bar', 'lorem');

0 commit comments

Comments
 (0)