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