Client
1 week ago
Concerns
1 month ago
JsonResponse.php
1 month ago
RedirectResponse.php
1 week ago
Request.php
1 week ago
Response.php
1 month ago
Request.php
1033 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Handles REST API request data abstraction for operations. |
| 5 | * |
| 6 | * @package Framework |
| 7 | * @subpackage Http |
| 8 | * @since 1.0.0 |
| 9 | */ |
| 10 | namespace Kirki\Framework\Http; |
| 11 | |
| 12 | \defined('ABSPATH') || exit; |
| 13 | use BadMethodCallException; |
| 14 | use Kirki\Framework\Contracts\Request as RequestContract; |
| 15 | use Kirki\Framework\Contracts\Support\Arrayable; |
| 16 | use Kirki\Framework\Sanitizer; |
| 17 | use Kirki\Framework\Exceptions\AuthorizationException; |
| 18 | use Kirki\Framework\Exceptions\ValidationException; |
| 19 | use Kirki\Framework\Http\Concerns\InteractsWithFiles; |
| 20 | use Kirki\Framework\Supports\Arr; |
| 21 | use Kirki\Framework\Validation\Validator; |
| 22 | use WP_REST_Request; |
| 23 | use Kirki\Framework\Supports\Str; |
| 24 | use InvalidArgumentException; |
| 25 | use function Kirki\Framework\message; |
| 26 | use function Kirki\Framework\user; |
| 27 | use function Kirki\Framework\value; |
| 28 | /** |
| 29 | * The Request class for handling HTTP requests. |
| 30 | * |
| 31 | * @method mixed|null string(string $key, $default = null) |
| 32 | * @method mixed|null date(string $key, $default = null) |
| 33 | * @method mixed|null datetime(string $key, $default = null) |
| 34 | * @method mixed|null text(string $key, $default = null) |
| 35 | * @method mixed|null html(string $key, $default = null) |
| 36 | * @method mixed|null email(string $key, $default = null) |
| 37 | * @method mixed|null url(string $key, $default = null) |
| 38 | * @method mixed|null key(string $key, $default = null) |
| 39 | * @method mixed|null title(string $key, $default = null) |
| 40 | * @method mixed|null file_name(string $key, $default = null) |
| 41 | * @method mixed|null mime_type(string $key, $default = null) |
| 42 | * @method mixed|null int(string $key, $default = null) |
| 43 | * @method mixed|null bool(string $key, $default = null) |
| 44 | * @method mixed|null float(string $key, $default = null) |
| 45 | * @method mixed|null array(string $key, $default = null) |
| 46 | * @method mixed|null whitelisted(string $key, $default = null, array $whitelist = []) |
| 47 | */ |
| 48 | class Request implements RequestContract, Arrayable |
| 49 | { |
| 50 | use InteractsWithFiles; |
| 51 | /** |
| 52 | * The request's input attributes. |
| 53 | * |
| 54 | * @var array |
| 55 | * |
| 56 | * @since 1.0.0 |
| 57 | */ |
| 58 | protected $attributes = []; |
| 59 | /** |
| 60 | * The HTTP method used for the request (e.g. GET, POST). |
| 61 | * |
| 62 | * @var string |
| 63 | * |
| 64 | * @since 1.0.0 |
| 65 | */ |
| 66 | protected $method; |
| 67 | /** |
| 68 | * The route URI for the request. |
| 69 | * |
| 70 | * @var string |
| 71 | * |
| 72 | * @since 1.0.0 |
| 73 | */ |
| 74 | protected $route; |
| 75 | /** |
| 76 | * The headers associated with the request. |
| 77 | * |
| 78 | * @var array |
| 79 | * |
| 80 | * @since 1.0.0 |
| 81 | */ |
| 82 | protected $headers; |
| 83 | /** |
| 84 | * The sanitized data. |
| 85 | * |
| 86 | * @var array |
| 87 | * |
| 88 | * @since 1.0.0 |
| 89 | */ |
| 90 | protected array $sanitized = []; |
| 91 | /** |
| 92 | * The validated data. |
| 93 | * |
| 94 | * @var array |
| 95 | * |
| 96 | * @since 1.0.0 |
| 97 | */ |
| 98 | protected array $validated = []; |
| 99 | /** |
| 100 | * Whether the validation has been resolved. |
| 101 | * |
| 102 | * @var bool |
| 103 | * |
| 104 | * @since 1.0.0 |
| 105 | */ |
| 106 | protected bool $validation_resolved = \false; |
| 107 | /** |
| 108 | * The route parameters. |
| 109 | * |
| 110 | * @var array |
| 111 | * |
| 112 | * @since 1.0.0 |
| 113 | */ |
| 114 | protected array $route_params = []; |
| 115 | /** |
| 116 | * Registered sanitizer method suffixes for typed request accessors. |
| 117 | * |
| 118 | * @var array<int, string> |
| 119 | * |
| 120 | * @since 1.0.0 |
| 121 | */ |
| 122 | protected static array $types = ['string', 'date', 'datetime', 'text', 'html', 'email', 'url', 'key', 'title', 'file_name', 'mime_type', 'int', 'bool', 'float', 'array', 'whitelisted']; |
| 123 | /** |
| 124 | * Magic getter to retrieve request attributes. |
| 125 | * |
| 126 | * @param string $name The name of the attribute. |
| 127 | * |
| 128 | * @return mixed|null The attribute value or null if not set. |
| 129 | * |
| 130 | * @since 1.0.0 |
| 131 | */ |
| 132 | public function __get(string $name) |
| 133 | { |
| 134 | return Arr::get($this->all(), $name, null); |
| 135 | } |
| 136 | /** |
| 137 | * Magic isset to check if an attribute exists. |
| 138 | * |
| 139 | * @param string $name The name of the attribute. |
| 140 | * |
| 141 | * @return bool |
| 142 | * |
| 143 | * @since 1.0.0 |
| 144 | */ |
| 145 | public function __isset(string $name) |
| 146 | { |
| 147 | return !\is_null($this->__get($name)); |
| 148 | } |
| 149 | /** |
| 150 | * Magic setter to set request attributes. |
| 151 | * |
| 152 | * @param string $name The name of the attribute. |
| 153 | * @param mixed $value The value to assign. |
| 154 | * |
| 155 | * @return void |
| 156 | * |
| 157 | * @since 1.0.0 |
| 158 | */ |
| 159 | public function __set(string $name, $value) |
| 160 | { |
| 161 | $this->attributes[$name] = $value; |
| 162 | } |
| 163 | /** |
| 164 | * Dynamically handle calls to the class. |
| 165 | * |
| 166 | * @param string $name The name of the method. |
| 167 | * @param array $arguments The arguments of the method. |
| 168 | * |
| 169 | * @return mixed |
| 170 | * |
| 171 | * @since 1.0.0 |
| 172 | */ |
| 173 | public function __call(string $name, array $arguments) |
| 174 | { |
| 175 | $name = \strtolower($name); |
| 176 | if (!\in_array($name, static::$types, \true)) { |
| 177 | throw new BadMethodCallException(\sprintf('Method %s::%s does not exist.', static::class, $name)); |
| 178 | } |
| 179 | $method_name = 'get_' . $name; |
| 180 | return $this->{$method_name}(...$arguments); |
| 181 | } |
| 182 | /** |
| 183 | * Create a new Request instance from a WP_REST_Request. |
| 184 | * |
| 185 | * @param WP_REST_Request $request The WordPress REST request object. |
| 186 | * |
| 187 | * @return self |
| 188 | * |
| 189 | * @since 1.0.0 |
| 190 | */ |
| 191 | public static function from_wp_rest_request(WP_REST_Request $request) |
| 192 | { |
| 193 | return (new static())->make_request($request); |
| 194 | } |
| 195 | /** |
| 196 | * Make a new request instance from a WP_REST_Request. |
| 197 | * |
| 198 | * @param WP_REST_Request $request The WordPress REST request object. |
| 199 | * |
| 200 | * @return self |
| 201 | * |
| 202 | * @since 1.0.0 |
| 203 | */ |
| 204 | public function make_request(WP_REST_Request $request) |
| 205 | { |
| 206 | $this->attributes = \array_merge($this->attributes, $request->get_params(), $request->get_file_params()); |
| 207 | $this->method = $request->get_method(); |
| 208 | $this->route = $request->get_route(); |
| 209 | $this->headers = $request->get_headers(); |
| 210 | $this->route_params = $request->get_url_params(); |
| 211 | return $this; |
| 212 | } |
| 213 | /** |
| 214 | * Capture the current HTTP request from PHP superglobals. |
| 215 | * |
| 216 | * @return self |
| 217 | * |
| 218 | * @since 1.0.0 |
| 219 | */ |
| 220 | public static function capture() |
| 221 | { |
| 222 | // phpcs:ignore Framework.NamingConventions.SnakeCaseVariable.NotSnakeCase |
| 223 | return (new static())->make_from_http($_GET, $_POST, $_FILES, $_SERVER); |
| 224 | } |
| 225 | /** |
| 226 | * Make a request instance from raw HTTP input arrays. |
| 227 | * |
| 228 | * @param array $query Query string parameters. |
| 229 | * @param array $body Request body parameters. |
| 230 | * @param array $files Uploaded files. |
| 231 | * @param array $server Server parameters. |
| 232 | * @param array $route_params Matched route parameters. |
| 233 | * |
| 234 | * @return self |
| 235 | * |
| 236 | * @since 1.0.0 |
| 237 | */ |
| 238 | public function make_from_http(array $query = [], array $body = [], array $files = [], array $server = [], array $route_params = []) |
| 239 | { |
| 240 | $query = $this->unslash_array($query); |
| 241 | $body = $this->unslash_array($body); |
| 242 | $this->attributes = \array_merge($query, $body, $route_params); |
| 243 | $this->method = \strtoupper($server['REQUEST_METHOD'] ?? 'GET'); |
| 244 | $this->route = $this->resolve_request_path($server); |
| 245 | $this->headers = $this->extract_headers($server); |
| 246 | $this->route_params = $route_params; |
| 247 | $this->files = []; |
| 248 | if (!empty($files)) { |
| 249 | $this->load_files_from_array($files); |
| 250 | } |
| 251 | return $this; |
| 252 | } |
| 253 | /** |
| 254 | * Set the matched route parameters and merge them into attributes. |
| 255 | * |
| 256 | * @param array $params The route parameters. |
| 257 | * |
| 258 | * @return self |
| 259 | * |
| 260 | * @since 1.0.0 |
| 261 | */ |
| 262 | public function set_route_params(array $params) |
| 263 | { |
| 264 | $this->route_params = $params; |
| 265 | $this->attributes = \array_merge($this->attributes, $params); |
| 266 | return $this; |
| 267 | } |
| 268 | /** |
| 269 | * Get all route parameters. |
| 270 | * |
| 271 | * @return array |
| 272 | * |
| 273 | * @since 1.0.0 |
| 274 | */ |
| 275 | public function route_params() |
| 276 | { |
| 277 | return $this->route_params; |
| 278 | } |
| 279 | /** |
| 280 | * Unslash an array of input values. |
| 281 | * |
| 282 | * @param array $values The values to unslash. |
| 283 | * |
| 284 | * @return array |
| 285 | * |
| 286 | * @since 1.0.0 |
| 287 | */ |
| 288 | protected function unslash_array(array $values) |
| 289 | { |
| 290 | if (!\function_exists('wp_unslash')) { |
| 291 | return $values; |
| 292 | } |
| 293 | return \array_map(function ($value) { |
| 294 | if (\is_array($value)) { |
| 295 | return $this->unslash_array($value); |
| 296 | } |
| 297 | return \is_string($value) ? wp_unslash($value) : $value; |
| 298 | }, $values); |
| 299 | } |
| 300 | /** |
| 301 | * Resolve the request path from server parameters. |
| 302 | * |
| 303 | * @param array $server Server parameters. |
| 304 | * |
| 305 | * @return string |
| 306 | * |
| 307 | * @since 1.0.0 |
| 308 | */ |
| 309 | protected function resolve_request_path(array $server) |
| 310 | { |
| 311 | $request_uri = isset($server['REQUEST_URI']) ? (string) $server['REQUEST_URI'] : ''; |
| 312 | $path = (string) \parse_url($request_uri, \PHP_URL_PATH); |
| 313 | if (\function_exists('home_url')) { |
| 314 | $home_path = (string) \parse_url(home_url(), \PHP_URL_PATH); |
| 315 | if ($home_path !== '' && $home_path !== '/' && \strpos($path, $home_path) === 0) { |
| 316 | $path = \substr($path, \strlen($home_path)); |
| 317 | } |
| 318 | } |
| 319 | return \trim($path, '/'); |
| 320 | } |
| 321 | /** |
| 322 | * Extract HTTP headers from server parameters. |
| 323 | * |
| 324 | * @param array $server Server parameters. |
| 325 | * |
| 326 | * @return array |
| 327 | * |
| 328 | * @since 1.0.0 |
| 329 | */ |
| 330 | protected function extract_headers(array $server) |
| 331 | { |
| 332 | $headers = []; |
| 333 | foreach ($server as $key => $value) { |
| 334 | if (\strpos($key, 'HTTP_') === 0) { |
| 335 | $name = \str_replace(' ', '-', \ucwords(\strtolower(\str_replace('_', ' ', \substr($key, 5))))); |
| 336 | $headers[$name] = [$value]; |
| 337 | continue; |
| 338 | } |
| 339 | if (\in_array($key, ['CONTENT_TYPE', 'CONTENT_LENGTH'], \true)) { |
| 340 | $name = \str_replace(' ', '-', \ucwords(\strtolower(\str_replace('_', ' ', $key)))); |
| 341 | $headers[$name] = [$value]; |
| 342 | } |
| 343 | } |
| 344 | return $headers; |
| 345 | } |
| 346 | /** |
| 347 | * Load uploaded files from a provided files array. |
| 348 | * |
| 349 | * @param array $files The files array. |
| 350 | * |
| 351 | * @return void |
| 352 | * |
| 353 | * @since 1.0.0 |
| 354 | */ |
| 355 | protected function load_files_from_array(array $files) |
| 356 | { |
| 357 | $converted = \array_map(function ($file) { |
| 358 | if (isset($file['name']) && \is_array($file['name'])) { |
| 359 | return $this->convert_uploaded_files($file); |
| 360 | } |
| 361 | return \Kirki\Framework\Filesystem\UploadedFile::create_from_base($file); |
| 362 | }, $files); |
| 363 | $keys = \array_keys($files); |
| 364 | $this->files = \array_combine($keys, \array_values($converted)) ?: []; |
| 365 | } |
| 366 | /** |
| 367 | * Get the validation rules for the request. |
| 368 | * |
| 369 | * @return array |
| 370 | * |
| 371 | * @since 1.0.0 |
| 372 | */ |
| 373 | protected function rules() |
| 374 | { |
| 375 | return []; |
| 376 | } |
| 377 | /** |
| 378 | * Run the validation on the request data. |
| 379 | * |
| 380 | * @param array $data The data to validate. |
| 381 | * @param array $rules The rules. |
| 382 | * |
| 383 | * @return array |
| 384 | * |
| 385 | * @throws ValidationException if fails to validate. |
| 386 | * |
| 387 | * @since 1.0.0 |
| 388 | */ |
| 389 | protected function run_validation(array $data, array $rules) |
| 390 | { |
| 391 | $validator = Validator::make($data, $rules, $this->messages()); |
| 392 | return $validator->validated(); |
| 393 | } |
| 394 | /** |
| 395 | * Define the sanitization filters for the request. |
| 396 | * This will be defined into the extended request class. |
| 397 | * |
| 398 | * @return array<string,string|callable(mixed):mixed|array> |
| 399 | * |
| 400 | * @since 1.0.0 |
| 401 | */ |
| 402 | protected function filters() |
| 403 | { |
| 404 | return []; |
| 405 | } |
| 406 | /** |
| 407 | * Define the validation messages for the request. |
| 408 | * This will be defined into the extended request class. |
| 409 | * |
| 410 | * @return array<string,string|callable(mixed):mixed|array> |
| 411 | * |
| 412 | * @since 1.0.0 |
| 413 | */ |
| 414 | protected function messages() |
| 415 | { |
| 416 | return []; |
| 417 | } |
| 418 | /** |
| 419 | * Run the sanitization on the data. |
| 420 | * |
| 421 | * @param array $data The data to sanitize. |
| 422 | * @param array $filters The filters to apply. |
| 423 | * |
| 424 | * @return array |
| 425 | * |
| 426 | * @since 1.0.0 |
| 427 | */ |
| 428 | protected function run_sanitization(array $data, array $filters) |
| 429 | { |
| 430 | return Sanitizer::make($data, $filters)->get_sanitized_data(); |
| 431 | } |
| 432 | /** |
| 433 | * Get the current user instance from the request. |
| 434 | * |
| 435 | * @return \Framework\Wordpress\User |
| 436 | * |
| 437 | * @since 1.0.0 |
| 438 | */ |
| 439 | public function user() |
| 440 | { |
| 441 | return user(); |
| 442 | } |
| 443 | /** |
| 444 | * Get the HTTP method used in the request. |
| 445 | * |
| 446 | * @return string |
| 447 | * |
| 448 | * @since 1.0.0 |
| 449 | */ |
| 450 | public function get_method() |
| 451 | { |
| 452 | return $this->method; |
| 453 | } |
| 454 | /** |
| 455 | * Alias of `get_method()`. |
| 456 | * |
| 457 | * @return string |
| 458 | * |
| 459 | * @since 1.0.0 |
| 460 | */ |
| 461 | public function method() |
| 462 | { |
| 463 | return $this->get_method(); |
| 464 | } |
| 465 | /** |
| 466 | * Get the route URI of the request. |
| 467 | * |
| 468 | * @return string |
| 469 | * |
| 470 | * @since 1.0.0 |
| 471 | */ |
| 472 | public function get_route() |
| 473 | { |
| 474 | return $this->route; |
| 475 | } |
| 476 | /** |
| 477 | * Get the headers associated with the request. |
| 478 | * |
| 479 | * @return array |
| 480 | * |
| 481 | * @since 1.0.0 |
| 482 | */ |
| 483 | public function get_headers() |
| 484 | { |
| 485 | return $this->headers; |
| 486 | } |
| 487 | /** |
| 488 | * Get a single HTTP header from the request. |
| 489 | * |
| 490 | * @param string $name The name. |
| 491 | * @param mixed $default The default. |
| 492 | * |
| 493 | * @return mixed |
| 494 | * |
| 495 | * @since 1.0.0 |
| 496 | */ |
| 497 | public function get_header(string $name, $default = null) |
| 498 | { |
| 499 | $value = $this->headers[$name] ?? $default; |
| 500 | if (\is_array($value)) { |
| 501 | return $value[0] ?? $default; |
| 502 | } |
| 503 | return $value; |
| 504 | } |
| 505 | /** |
| 506 | * Alias of `get_header()`. |
| 507 | * |
| 508 | * @param string $name The name. |
| 509 | * @param mixed $default The default. |
| 510 | * |
| 511 | * @return mixed |
| 512 | * |
| 513 | * @since 1.0.0 |
| 514 | */ |
| 515 | public function header(string $name, $default = null) |
| 516 | { |
| 517 | return $this->get_header($name, $default); |
| 518 | } |
| 519 | /** |
| 520 | * Get all input attributes. |
| 521 | * |
| 522 | * @param array|null $keys The keys to get. |
| 523 | * |
| 524 | * @return array |
| 525 | * |
| 526 | * @since 1.0.0 |
| 527 | */ |
| 528 | public function all($keys = null) |
| 529 | { |
| 530 | $input = \array_merge($this->attributes, $this->all_files()); |
| 531 | if (!$keys) { |
| 532 | return $input; |
| 533 | } |
| 534 | $results = []; |
| 535 | foreach (\is_array($keys) ? $keys : \func_get_args() as $key) { |
| 536 | Arr::set($results, $key, Arr::get($input, $key)); |
| 537 | } |
| 538 | return $results; |
| 539 | } |
| 540 | /** |
| 541 | * Get the route parameters. |
| 542 | * |
| 543 | * @param string $key The key of the route parameter. |
| 544 | * @param mixed $default The default value. |
| 545 | * |
| 546 | * @return array|mixed |
| 547 | * |
| 548 | * @since 1.0.0 |
| 549 | */ |
| 550 | public function route($key, $default = null) |
| 551 | { |
| 552 | if (empty($key)) { |
| 553 | throw new InvalidArgumentException('The route key is required.'); |
| 554 | } |
| 555 | return Arr::get($this->route_params, $key, $default); |
| 556 | } |
| 557 | /** |
| 558 | * Get the sanitized data. |
| 559 | * |
| 560 | * @return array |
| 561 | * |
| 562 | * @since 1.0.0 |
| 563 | */ |
| 564 | public function sanitized() |
| 565 | { |
| 566 | return $this->sanitized; |
| 567 | } |
| 568 | /** |
| 569 | * Validate the request data. |
| 570 | * |
| 571 | * @param array $rules The rules to validate. |
| 572 | * |
| 573 | * @return array |
| 574 | * |
| 575 | * @throws ValidationException if fails to validate. |
| 576 | * |
| 577 | * @since 1.0.0 |
| 578 | */ |
| 579 | public function validate(array $rules) |
| 580 | { |
| 581 | return $this->run_validation($this->attributes(), $rules); |
| 582 | } |
| 583 | /** |
| 584 | * Get the validated data. |
| 585 | * |
| 586 | * @return array |
| 587 | * |
| 588 | * @since 1.0.0 |
| 589 | */ |
| 590 | public function validated() |
| 591 | { |
| 592 | return $this->validated; |
| 593 | } |
| 594 | /** |
| 595 | * Get all input attributes. |
| 596 | * |
| 597 | * @return array |
| 598 | * |
| 599 | * @since 1.0.0 |
| 600 | */ |
| 601 | public function attributes() |
| 602 | { |
| 603 | if ($this->has('_method')) { |
| 604 | $this->remove('_method'); |
| 605 | } |
| 606 | return $this->attributes; |
| 607 | } |
| 608 | /** |
| 609 | * Authorize the request. |
| 610 | * |
| 611 | * @return static |
| 612 | * |
| 613 | * @throws AuthorizationException |
| 614 | * |
| 615 | * @since 1.0.0 |
| 616 | */ |
| 617 | public function authorize_request() |
| 618 | { |
| 619 | if (!$this->authorize()) { |
| 620 | throw new AuthorizationException(message('auth.unauthorized_request')); |
| 621 | } |
| 622 | return $this; |
| 623 | } |
| 624 | /** |
| 625 | * Validate the request data and sanitize the data. |
| 626 | * |
| 627 | * @return static |
| 628 | * |
| 629 | * @since 1.0.0 |
| 630 | */ |
| 631 | public function validate_request() |
| 632 | { |
| 633 | if ($this->validation_resolved) { |
| 634 | return $this; |
| 635 | } |
| 636 | $this->resolve_validation_and_sanitization(); |
| 637 | $this->validation_resolved = \true; |
| 638 | return $this; |
| 639 | } |
| 640 | /** |
| 641 | * Resolve the validation and sanitization. |
| 642 | * |
| 643 | * @return void |
| 644 | * |
| 645 | * @throws AuthorizationException |
| 646 | * |
| 647 | * @since 1.0.0 |
| 648 | */ |
| 649 | protected function resolve_validation_and_sanitization() |
| 650 | { |
| 651 | $this->prepare_for_validation(); |
| 652 | $validated = $this->run_validation($this->attributes(), $this->rules()); |
| 653 | $this->validated = $validated; |
| 654 | $sanitized = $this->run_sanitization($this->attributes(), $this->filters()); |
| 655 | $this->sanitized = $sanitized; |
| 656 | $this->merge($sanitized); |
| 657 | $this->passed_validation(); |
| 658 | } |
| 659 | /** |
| 660 | * Prepare the request data for validation. |
| 661 | * |
| 662 | * @return void |
| 663 | * |
| 664 | * @since 1.0.0 |
| 665 | */ |
| 666 | protected function prepare_for_validation() |
| 667 | { |
| 668 | // Override this method to prepare the request data for validation. |
| 669 | } |
| 670 | /** |
| 671 | * Handle the passed validation. |
| 672 | * |
| 673 | * @return void |
| 674 | * |
| 675 | * @since 1.0.0 |
| 676 | */ |
| 677 | protected function passed_validation() |
| 678 | { |
| 679 | // Override this method to handle the passed validation. |
| 680 | } |
| 681 | /** |
| 682 | * Determine if the user is authorized to make this request. |
| 683 | * |
| 684 | * @return bool |
| 685 | * |
| 686 | * @since 1.0.0 |
| 687 | */ |
| 688 | protected function authorize() |
| 689 | { |
| 690 | return \true; |
| 691 | } |
| 692 | /** |
| 693 | * Merge the given input with the existing attributes. |
| 694 | * |
| 695 | * @param array $input The input to merge. |
| 696 | * |
| 697 | * @return static |
| 698 | * |
| 699 | * @since 1.0.0 |
| 700 | */ |
| 701 | public function merge(array $input) |
| 702 | { |
| 703 | $this->attributes = \array_merge($this->attributes, $input); |
| 704 | return $this; |
| 705 | } |
| 706 | /** |
| 707 | * Check if an attribute exists. |
| 708 | * |
| 709 | * @param string $key The key of the attribute. |
| 710 | * |
| 711 | * @return bool |
| 712 | * |
| 713 | * @since 1.0.0 |
| 714 | */ |
| 715 | public function has(string $key) |
| 716 | { |
| 717 | return isset($this->attributes[$key]); |
| 718 | } |
| 719 | /** |
| 720 | * Remove an attribute. |
| 721 | * |
| 722 | * @param string $key The key of the attribute. |
| 723 | * |
| 724 | * @return void |
| 725 | * |
| 726 | * @since 1.0.0 |
| 727 | */ |
| 728 | public function remove(string $key) |
| 729 | { |
| 730 | unset($this->attributes[$key]); |
| 731 | } |
| 732 | /** |
| 733 | * Get all input attributes except the specified keys. |
| 734 | * |
| 735 | * @param array $attributes The attribute keys to exclude. |
| 736 | * |
| 737 | * @return array |
| 738 | * |
| 739 | * @since 1.0.0 |
| 740 | */ |
| 741 | public function except(array $attributes) |
| 742 | { |
| 743 | return \array_diff_key($this->attributes, \array_flip($attributes)); |
| 744 | } |
| 745 | /** |
| 746 | * Get a single input attribute by key. |
| 747 | * |
| 748 | * @param string $key The key of the attribute. |
| 749 | * |
| 750 | * @return mixed|null |
| 751 | * |
| 752 | * @since 1.0.0 |
| 753 | */ |
| 754 | public function only(string $key) |
| 755 | { |
| 756 | return $this->attributes[$key] ?? null; |
| 757 | } |
| 758 | /** |
| 759 | * Alias for the `only()` method. |
| 760 | * |
| 761 | * @param string $key The key of the attribute. |
| 762 | * |
| 763 | * @return mixed|null |
| 764 | * |
| 765 | * @since 1.0.0 |
| 766 | */ |
| 767 | public function input(string $key) |
| 768 | { |
| 769 | return $this->only($key); |
| 770 | } |
| 771 | /** |
| 772 | * Get a value from the request with optional default and type casting. |
| 773 | * |
| 774 | * @param string $key The key to retrieve. |
| 775 | * @param mixed $default Default value if the key doesn't exist. |
| 776 | * @param string|null $type Optional type to cast the result to: |
| 777 | * int, float, bool, string, array with proper sanitization. |
| 778 | * |
| 779 | * @return mixed|null |
| 780 | * |
| 781 | * @since 1.0.0 |
| 782 | */ |
| 783 | public function get(string $key, $default = null, $type = null) |
| 784 | { |
| 785 | $value = $this->attributes[$key] ?? null; |
| 786 | if ($value === null) { |
| 787 | return value($default); |
| 788 | } |
| 789 | $value = Sanitizer::apply_rule($value, $type); |
| 790 | return $value ?? value($default); |
| 791 | } |
| 792 | /** |
| 793 | * Get a value from the request with optional default and type casting. |
| 794 | * |
| 795 | * @param string $key The key to retrieve. |
| 796 | * @param mixed $default Default value if the key doesn't exist. |
| 797 | * @param array $whitelist Optional whitelist of allowed values. |
| 798 | * |
| 799 | * @return mixed |
| 800 | * |
| 801 | * @since 1.0.0 |
| 802 | */ |
| 803 | public function get_whitelisted(string $key, $default = null, array $whitelist = []) |
| 804 | { |
| 805 | $value = $this->get($key); |
| 806 | if (!\in_array($value, $whitelist, \true)) { |
| 807 | return $default; |
| 808 | } |
| 809 | return $value; |
| 810 | } |
| 811 | /** |
| 812 | * Get a string value with sanitization applied. |
| 813 | * |
| 814 | * @param string $key The key to retrieve. |
| 815 | * @param string|null $default Default value if the key doesn't exist. |
| 816 | * |
| 817 | * @return string|null |
| 818 | * |
| 819 | * @since 1.0.0 |
| 820 | */ |
| 821 | public function get_string(string $key, $default = null) |
| 822 | { |
| 823 | return $this->get($key, $default, Sanitizer::TEXT); |
| 824 | } |
| 825 | /** |
| 826 | * Get a date value. |
| 827 | * |
| 828 | * @param string $key The key to retrieve. |
| 829 | * @param string|null $default Default value if the key doesn't exist. |
| 830 | * |
| 831 | * @return string |
| 832 | * |
| 833 | * @since 1.0.0 |
| 834 | */ |
| 835 | public function get_date(string $key, $default = null) |
| 836 | { |
| 837 | return $this->get($key, $default, Sanitizer::DATE); |
| 838 | } |
| 839 | /** |
| 840 | * Get a datetime value. |
| 841 | * |
| 842 | * @param string $key The key to retrieve. |
| 843 | * @param string|null $default Default value if the key doesn't exist. |
| 844 | * |
| 845 | * @return string |
| 846 | * |
| 847 | * @since 1.0.0 |
| 848 | */ |
| 849 | public function get_datetime(string $key, $default = null) |
| 850 | { |
| 851 | return $this->get($key, $default, Sanitizer::DATETIME); |
| 852 | } |
| 853 | /** |
| 854 | * Get a text with sanitization applied. |
| 855 | * |
| 856 | * @param string $key The key to retrieve. |
| 857 | * @param string|null $default Default value if the key doesn't exist. |
| 858 | * |
| 859 | * @return string|null |
| 860 | * |
| 861 | * @since 1.0.0 |
| 862 | */ |
| 863 | public function get_text(string $key, $default = null) |
| 864 | { |
| 865 | return $this->get_string($key, $default); |
| 866 | } |
| 867 | /** |
| 868 | * Get a html supported content with sanitization applied. |
| 869 | * |
| 870 | * @param string $key The key to retrieve. |
| 871 | * @param string|null $default Default value if the key doesn't exist. |
| 872 | * |
| 873 | * @return string|null |
| 874 | * |
| 875 | * @since 1.0.0 |
| 876 | */ |
| 877 | public function get_html(string $key, $default = null) |
| 878 | { |
| 879 | return $this->get($key, $default, Sanitizer::RICH_TEXT); |
| 880 | } |
| 881 | /** |
| 882 | * Get a email with sanitization applied. |
| 883 | * |
| 884 | * @param string $key The key to retrieve. |
| 885 | * @param string|null $default Default value if the key doesn't exist. |
| 886 | * |
| 887 | * @return string|null |
| 888 | * |
| 889 | * @since 1.0.0 |
| 890 | */ |
| 891 | public function get_email(string $key, $default = null) |
| 892 | { |
| 893 | return $this->get($key, $default, Sanitizer::EMAIL); |
| 894 | } |
| 895 | /** |
| 896 | * Get a url with sanitization applied. |
| 897 | * |
| 898 | * @param string $key The key to retrieve. |
| 899 | * @param string|null $default Default value if the key doesn't exist. |
| 900 | * |
| 901 | * @return string|null |
| 902 | * |
| 903 | * @since 1.0.0 |
| 904 | */ |
| 905 | public function get_url(string $key, $default = null) |
| 906 | { |
| 907 | return $this->get($key, $default, Sanitizer::URL); |
| 908 | } |
| 909 | /** |
| 910 | * Get a key value with sanitization applied. |
| 911 | * |
| 912 | * @param string $key The key to retrieve. |
| 913 | * @param string|null $default Default value if the key doesn't exist. |
| 914 | * |
| 915 | * @return string|null |
| 916 | * |
| 917 | * @since 1.0.0 |
| 918 | */ |
| 919 | public function get_key(string $key, $default = null) |
| 920 | { |
| 921 | return $this->get($key, $default, Sanitizer::KEY); |
| 922 | } |
| 923 | /** |
| 924 | * Get a title value with sanitization applied. |
| 925 | * |
| 926 | * @param string $key The key to retrieve. |
| 927 | * @param string|null $default Default value if the key doesn't exist. |
| 928 | * |
| 929 | * @return string|null |
| 930 | * |
| 931 | * @since 1.0.0 |
| 932 | */ |
| 933 | public function get_title(string $key, $default = null) |
| 934 | { |
| 935 | return $this->get($key, $default, Sanitizer::TITLE); |
| 936 | } |
| 937 | /** |
| 938 | * Get a file name with sanitization applied. |
| 939 | * |
| 940 | * @param string $key The key to retrieve. |
| 941 | * @param string|null $default Default value if the key doesn't exist. |
| 942 | * |
| 943 | * @return string|null |
| 944 | * |
| 945 | * @since 1.0.0 |
| 946 | */ |
| 947 | public function get_file_name(string $key, $default = null) |
| 948 | { |
| 949 | return $this->get($key, $default, Sanitizer::FILE_NAME); |
| 950 | } |
| 951 | /** |
| 952 | * Get mime type with sanitization applied. |
| 953 | * |
| 954 | * @param string $key The key to retrieve. |
| 955 | * @param string|null $default Default value if the key doesn't exist. |
| 956 | * |
| 957 | * @return string|null |
| 958 | * |
| 959 | * @since 1.0.0 |
| 960 | */ |
| 961 | public function get_mime_type(string $key, $default = null) |
| 962 | { |
| 963 | return $this->get($key, $default, Sanitizer::MIME_TYPE); |
| 964 | } |
| 965 | /** |
| 966 | * Get an integer value. |
| 967 | * |
| 968 | * @param string $key The key to retrieve. |
| 969 | * @param int|null $default Default value if the key doesn't exist. |
| 970 | * |
| 971 | * @return int|null |
| 972 | * |
| 973 | * @since 1.0.0 |
| 974 | */ |
| 975 | public function get_int(string $key, $default = null) |
| 976 | { |
| 977 | return $this->get($key, $default, Sanitizer::INT); |
| 978 | } |
| 979 | /** |
| 980 | * Get a boolean value. |
| 981 | * |
| 982 | * @param string $key The key to retrieve. |
| 983 | * @param bool $default Default value if the key doesn't exist. |
| 984 | * |
| 985 | * @return bool |
| 986 | * |
| 987 | * @since 1.0.0 |
| 988 | */ |
| 989 | public function get_bool(string $key, bool $default = \false) |
| 990 | { |
| 991 | return $this->get($key, $default, Sanitizer::BOOL); |
| 992 | } |
| 993 | /** |
| 994 | * Get a float value. |
| 995 | * |
| 996 | * @param string $key The key to retrieve. |
| 997 | * @param float|null $default Default value if the key doesn't exist. |
| 998 | * |
| 999 | * @return float|null |
| 1000 | * |
| 1001 | * @since 1.0.0 |
| 1002 | */ |
| 1003 | public function get_float(string $key, $default = null) |
| 1004 | { |
| 1005 | return $this->get($key, $default, Sanitizer::FLOAT); |
| 1006 | } |
| 1007 | /** |
| 1008 | * Get an array value. |
| 1009 | * |
| 1010 | * @param string $key The key to retrieve. |
| 1011 | * @param array|null $default Default value if the key doesn't exist. |
| 1012 | * |
| 1013 | * @return array|null |
| 1014 | * |
| 1015 | * @since 1.0.0 |
| 1016 | */ |
| 1017 | public function get_array(string $key, $default = null) |
| 1018 | { |
| 1019 | return $this->get($key, $default, Sanitizer::ARRAY); |
| 1020 | } |
| 1021 | /** |
| 1022 | * Convert the request to an array. |
| 1023 | * |
| 1024 | * @return array |
| 1025 | * |
| 1026 | * @since 1.0.0 |
| 1027 | */ |
| 1028 | public function to_array() |
| 1029 | { |
| 1030 | return $this->all(); |
| 1031 | } |
| 1032 | } |
| 1033 |