|
| 1 | +# Payload |
| 2 | + |
| 3 | +The **Payload** class is a lightweight data wrapper used to represent a single filter input. |
| 4 | +It normalizes values, provides utility methods, and makes it easier to work with common patterns such as wildcard search, JSON detection, boolean casting, and more. |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +## Overview |
| 9 | + |
| 10 | +When you define filters inside a `Filterable` class, the filter method receives a `Payload` object instead of a raw value. |
| 11 | + |
| 12 | +```php |
| 13 | +class PostFilter extends Filterable |
| 14 | +{ |
| 15 | + protected $filters = ['title']; |
| 16 | + |
| 17 | + protected function title(Payload $payload) |
| 18 | + { |
| 19 | + return $this->builder->where('title', 'like', $payload->like()); |
| 20 | + } |
| 21 | +} |
| 22 | +``` |
| 23 | + |
| 24 | +--- |
| 25 | + |
| 26 | +## Properties |
| 27 | + |
| 28 | +| Property | Type | Description | |
| 29 | +| ----------------- | -------- | -------------------------------------- | |
| 30 | +| `$field` | `string` | The field passed from the request. | |
| 31 | +| `$operator` | `string` | The operator passed from the request. | |
| 32 | +| `$value` | `mixed` | The raw value passed from the request. | |
| 33 | +| `$beforeSanitize` | `mixed` | The original value before sanitizing. | |
| 34 | + |
| 35 | +--- |
| 36 | + |
| 37 | +## Public Methods |
| 38 | + |
| 39 | +### `__toString(): string` |
| 40 | + |
| 41 | +Returns the value as string. |
| 42 | + |
| 43 | +```php |
| 44 | +(string) $payload; // equivalent to $payload->value |
| 45 | +``` |
| 46 | + |
| 47 | +--- |
| 48 | + |
| 49 | +### `length(): int` |
| 50 | + |
| 51 | +Get the length of the value. |
| 52 | +Dealing with `array` or `string` |
| 53 | + |
| 54 | +```php |
| 55 | +if ($payload->length() > 10) { |
| 56 | + // skip filter |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +--- |
| 61 | + |
| 62 | +### `isEmpty(): bool` |
| 63 | + |
| 64 | +Check if the value is empty (`null`, `""`, or whitespace). |
| 65 | + |
| 66 | +```php |
| 67 | +if ($payload->isEmpty()) { |
| 68 | + // skip filter |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +--- |
| 73 | + |
| 74 | +### `isNotEmpty(): bool` |
| 75 | + |
| 76 | +Check if the value is not empty (`filterable`, `['one', 'tow']`, or any data). |
| 77 | + |
| 78 | +```php |
| 79 | +if ($payload->isNotEmpty()) { |
| 80 | + // appliy filter |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +--- |
| 85 | + |
| 86 | +### `isNull(): bool` |
| 87 | + |
| 88 | +Check if the value is null. |
| 89 | + |
| 90 | +```php |
| 91 | +if ($payload->isNull()) { |
| 92 | + // skip filter |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +--- |
| 97 | + |
| 98 | +### `isJson(): bool` |
| 99 | + |
| 100 | +Check if the payload is a valid JSON string. |
| 101 | + |
| 102 | +```php |
| 103 | +if ($payload->isJson()) { |
| 104 | + $data = json_decode($payload->value, true); |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +--- |
| 109 | + |
| 110 | +### `asBoolean(): bool|null` |
| 111 | + |
| 112 | +Convert value to boolean. |
| 113 | +Supports `"true"`, `"false"`, `"1"`, `"0"`, `"yes"`, `"no"`. |
| 114 | + |
| 115 | +```php |
| 116 | +$payload->asBoolean(); // true or false |
| 117 | +``` |
| 118 | + |
| 119 | +--- |
| 120 | + |
| 121 | +### `asLike(string $side = "both"): string` |
| 122 | + |
| 123 | +Wrap the value with `%` for SQL `LIKE` queries. |
| 124 | + |
| 125 | +- `both` → `%value%` |
| 126 | +- `left` → `%value` |
| 127 | +- `right` → `value%` |
| 128 | + |
| 129 | +```php |
| 130 | +$this->builder->where('title', 'like', $payload->asLike()); |
| 131 | +// WHERE title LIKE "%keyword%" |
| 132 | +``` |
| 133 | + |
| 134 | +--- |
| 135 | + |
| 136 | +### `asInt(): int` |
| 137 | + |
| 138 | +Cast value to integer. |
| 139 | + |
| 140 | +```php |
| 141 | +$payload->asInt(); // 42 |
| 142 | +``` |
| 143 | + |
| 144 | +--- |
| 145 | + |
| 146 | +### `raw(): mixed` |
| 147 | + |
| 148 | +Get the original unmodified value. |
| 149 | + |
| 150 | +```php |
| 151 | +$payload->raw(); // equivalent to $payload->beforeSanitize |
| 152 | +``` |
| 153 | + |
| 154 | +--- |
| 155 | + |
| 156 | +### `isNumeric(): bool` |
| 157 | + |
| 158 | +Check if the value is numeric. |
| 159 | + |
| 160 | +```php |
| 161 | +if ($payload->isNumeric()) { |
| 162 | + return $this->builder->where('id', $this->value); |
| 163 | +} |
| 164 | +``` |
| 165 | + |
| 166 | +--- |
| 167 | + |
| 168 | +### `isString(): bool` |
| 169 | + |
| 170 | +Check if the value is string. |
| 171 | + |
| 172 | +```php |
| 173 | +if ($payload->isString()) { |
| 174 | + return $this->builder->where('name', $this->value); |
| 175 | +} |
| 176 | +``` |
| 177 | + |
| 178 | +--- |
| 179 | + |
| 180 | +### `isArray(): bool` |
| 181 | + |
| 182 | +Check if the value is array. |
| 183 | + |
| 184 | +```php |
| 185 | +if ($payload->isArray()) { |
| 186 | + return $this->builder->where('name', 'in', $this->value); |
| 187 | +} |
| 188 | +``` |
| 189 | + |
| 190 | +--- |
| 191 | + |
| 192 | +### `isTrue(): bool` |
| 193 | + |
| 194 | +Check if the value is `true`. |
| 195 | +Supports `"true"`, `"1"`, `"yes"`. |
| 196 | + |
| 197 | +```php |
| 198 | +$payload->isTrue(); // true |
| 199 | +``` |
| 200 | + |
| 201 | +--- |
| 202 | + |
| 203 | +### `isFalse(): bool` |
| 204 | + |
| 205 | +Check if the value is `false`. |
| 206 | +Supports `"false"`, `"0"`, `"no"`, `""`. |
| 207 | + |
| 208 | +```php |
| 209 | +$payload->isFalse(); |
| 210 | +``` |
| 211 | + |
| 212 | +--- |
| 213 | + |
| 214 | +### `asArray(): array` |
| 215 | + |
| 216 | +If the value is a valid JSON string representing an array/object, it will be decoded into an array. |
| 217 | +If the value is already an array, it will be returned directly. Otherwise returns null. |
| 218 | + |
| 219 | +```php |
| 220 | +$payload->asArray(); |
| 221 | +``` |
| 222 | + |
| 223 | +--- |
| 224 | + |
| 225 | +### `toArray(): array` |
| 226 | + |
| 227 | +Get the instance as an array |
| 228 | + |
| 229 | +```php |
| 230 | +$payload->toArray(); |
| 231 | + |
| 232 | +/* |
| 233 | + [ |
| 234 | + "field" => "status", |
| 235 | + "operator" => "=", |
| 236 | + "value" => "filterable" |
| 237 | + ] |
| 238 | +*/ |
| 239 | +``` |
| 240 | + |
| 241 | +--- |
| 242 | + |
| 243 | +### `toJson(): string` |
| 244 | + |
| 245 | +Get the instance as an json string |
| 246 | + |
| 247 | +```php |
| 248 | +$payload->toJson(); |
| 249 | + |
| 250 | +/* |
| 251 | + [ |
| 252 | + "field" => "status", |
| 253 | + "operator" => "=", |
| 254 | + "value" => "filterable" |
| 255 | + ] |
| 256 | +*/ |
| 257 | +``` |
| 258 | + |
| 259 | +## Example Usage |
| 260 | + |
| 261 | +```php |
| 262 | +protected function status(Payload $payload) |
| 263 | +{ |
| 264 | + return $this->builder->where('is_active', $payload->asBoolean()); |
| 265 | +} |
| 266 | + |
| 267 | +protected function category(Payload $payload) |
| 268 | +{ |
| 269 | + return $this->builder->where('category_id', $payload->asInt()); |
| 270 | +} |
| 271 | + |
| 272 | +protected function meta(Payload $payload) |
| 273 | +{ |
| 274 | + if ($payload->isJson()) { |
| 275 | + return $this->builder->whereJsonContains('meta', $payload->raw()); |
| 276 | + } |
| 277 | +} |
| 278 | +``` |
| 279 | + |
| 280 | +--- |
| 281 | + |
| 282 | +## Summary |
| 283 | + |
| 284 | +- `Payload` standardizes how filter values are processed. |
| 285 | +- It provides helper methods (`asLike`, `asBoolean`, `isJson`, etc.). |
| 286 | +- This reduces repetitive code inside filter classes. |
0 commit comments