Skip to content

Commit f636177

Browse files
committed
feat: add in() and notIn() helpers for value membership checking
1 parent 36547ae commit f636177

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

docs/api/payload.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,44 @@ if ($payload->isNull()) {
105105

106106
---
107107

108+
### `in(...$haystack): bool`
109+
110+
Check if the payload value exists inside the given list.
111+
Supports both a flat list of values or a single array.
112+
113+
```php
114+
if ($payload->in('active', 'pending', 'archived')) {
115+
// apply filter
116+
}
117+
```
118+
119+
---
120+
121+
### `notIn(...$haystack): bool`
122+
123+
Check if the payload value does _not_ exist in the given list.
124+
125+
```php
126+
if ($payload->notIn('banned', 'deleted')) {
127+
// only include safe records
128+
}
129+
```
130+
131+
---
132+
133+
### `isBoolean(): bool`
134+
135+
Check if the value can be interpreted as boolean.
136+
Supports `"true"`, `"false"`, `"1"`, `"0"`, `"yes"`, `"no"`.
137+
138+
```php
139+
if ($payload->isBoolean()) {
140+
$this->builder->where('is_active', $payload->asBoolean());
141+
}
142+
```
143+
144+
---
145+
108146
### `isJson(): bool`
109147

110148
Check if the payload is a valid JSON string.

src/Support/Payload.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,32 @@ public function asBoolean(): ?bool
216216
return $this->isBoolean() ? filter_var($this->value, FILTER_VALIDATE_BOOLEAN) : null;
217217
}
218218

219+
/**
220+
* Check if the payload value is in the given haystack.
221+
*
222+
* @param mixed ...$haystack
223+
* @return bool
224+
*/
225+
public function in(...$haystack): bool
226+
{
227+
if (count($haystack) === 1 && is_array($haystack[0])) {
228+
$haystack = $haystack[0];
229+
}
230+
231+
return in_array($this->value, (array) $haystack, true);
232+
}
233+
234+
/**
235+
* Check if the payload value is not in the given haystack.
236+
*
237+
* @param mixed ...$haystack
238+
* @return bool
239+
*/
240+
public function notIn(...$haystack): bool
241+
{
242+
return !$this->in(...$haystack);
243+
}
244+
219245
/**
220246
* Return a new Payload instance with the given value.
221247
*

0 commit comments

Comments
 (0)