mirror of
https://gh.wpcy.net/https://github.com/fairpm/aspirecloud.git
synced 2026-06-20 02:22:28 +08:00
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: Joost de Valk <joost@altha.nl>
57 lines
1.7 KiB
PHP
57 lines
1.7 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Values\Packages;
|
|
|
|
use App\Values\DTO;
|
|
use Bag\Attributes\Laravel\FromRouteParameter;
|
|
use Bag\Attributes\StripExtraParameters;
|
|
use Bag\Attributes\Transforms;
|
|
use Illuminate\Http\Request;
|
|
|
|
/**
|
|
* Validated search request for the GET /packages/{type} endpoint.
|
|
*
|
|
* Constructed automatically from the HTTP request via Bag's service provider.
|
|
* The `type` route parameter is resolved via #[FromRouteParameter].
|
|
*/
|
|
#[StripExtraParameters]
|
|
readonly class PackageSearchRequest extends DTO
|
|
{
|
|
/** @param array<string, string>|null $requires */
|
|
public function __construct(
|
|
#[FromRouteParameter]
|
|
public string $type,
|
|
public ?string $q = null,
|
|
public ?array $requires = null,
|
|
public int $page = 1,
|
|
public int $per_page = 24,
|
|
) {}
|
|
|
|
/**
|
|
* Transform an incoming HTTP request into constructor arguments.
|
|
*
|
|
* Validates query parameters and extracts the package type from the route.
|
|
*
|
|
* @return array<string, mixed>
|
|
*/
|
|
#[Transforms(Request::class)]
|
|
public static function fromRequest(Request $request): array
|
|
{
|
|
$validated = $request->validate([
|
|
'q' => ['nullable', 'string', 'max:200'],
|
|
'requires' => ['nullable', 'array'],
|
|
'requires.*' => ['string', 'max:20'],
|
|
'page' => ['nullable', 'integer', 'min:1'],
|
|
'per_page' => ['nullable', 'integer', 'min:1', 'max:100'],
|
|
]);
|
|
|
|
return [
|
|
'type' => $request->route('type'),
|
|
'q' => $validated['q'] ?? null,
|
|
'requires' => $validated['requires'] ?? null,
|
|
'page' => (int) ($validated['page'] ?? 1),
|
|
'per_page' => (int) ($validated['per_page'] ?? 24),
|
|
];
|
|
}
|
|
}
|