*/ public function actionInput(): array { return $this->validated(); } public function perPage(int $default = self::DEFAULT_PER_PAGE): int { $perPage = filter_var($this->validated('per_page') ?? $default, FILTER_VALIDATE_INT); throw_if($perPage === false, InvalidArgumentException::class, 'The validated per-page value must be an integer.'); return $perPage; } /** * Returns the named route parameter and asserts that it is a real model that * exists in the database. * * @template T of \Illuminate\Database\Eloquent\Model * * @param class-string $expect * @return T * * @noinspection PhpDocSignatureInspection */ public function parameter(string $key, string $expect): Model { $route = $this->route(); throw_if($route === null, InvalidArgumentException::class, 'Cannot resolve a route parameter without an active route.'); $value = $route->parameter($key); throw_unless($value instanceof $expect, InvalidArgumentException::class, "Route parameter [$key] is not a valid model."); throw_unless($value->exists, InvalidArgumentException::class, "Route parameter [$key] does not exist."); return $value; } /** @return ValidationRules */ protected function validationRules(): array { if (! $this->isMethod('GET')) { return $this->rules(); } return array_merge($this->rules(), [ 'page' => ['sometimes', 'integer', 'min:1'], 'per_page' => ['sometimes', 'integer', 'min:1', 'max:'.self::MAX_PER_PAGE], ]); } }