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