Files
pterodactyl-panel/app/Http/Requests/Api/Application/Allocations/StoreAllocationRequest.php
T

58 lines
1.7 KiB
PHP
Raw Normal View History

2018-01-27 12:38:56 -06:00
<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
2018-01-27 12:38:56 -06:00
namespace Pterodactyl\Http\Requests\Api\Application\Allocations;
use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Services\Acl\Api\AdminAcl;
use UnexpectedValueException;
2018-01-27 12:38:56 -06:00
class StoreAllocationRequest extends ApplicationApiRequest
{
2022-10-14 10:59:20 -06:00
protected ?string $resource = AdminAcl::RESOURCE_ALLOCATIONS;
2018-01-27 12:38:56 -06:00
2022-10-14 10:59:20 -06:00
protected int $permission = AdminAcl::WRITE;
2018-01-27 12:38:56 -06:00
2026-10-05 18:10:18 -04:00
/**
* @return ValidationRules
*/
2018-01-27 12:38:56 -06:00
public function rules(): array
{
return [
2026-10-05 18:10:18 -04:00
'ip' => ['required', 'string'],
'alias' => ['sometimes', 'nullable', 'string', 'max:191'],
'ports' => ['required', 'array'],
'ports.*' => ['string'],
2018-01-27 12:38:56 -06:00
];
}
2026-10-05 18:10:18 -04:00
/**
* @return AllocationAssignmentData
*/
public function payload(): array
2018-01-27 12:38:56 -06:00
{
$data = parent::validated();
2026-10-05 18:10:18 -04:00
$ip = $data['ip'] ?? null;
$ports = $data['ports'] ?? null;
$alias = $data['alias'] ?? null;
throw_if(! is_string($ip) || ! is_array($ports) || ! array_is_list($ports), UnexpectedValueException::class, 'Validated allocation data has an invalid structure.');
$normalizedPorts = [];
foreach ($ports as $port) {
throw_unless(is_string($port), UnexpectedValueException::class, 'Validated allocation ports must be strings.');
$normalizedPorts[] = $port;
}
throw_if($alias !== null && ! is_string($alias), UnexpectedValueException::class, 'Validated allocation aliases must be strings or null.');
2018-01-27 12:38:56 -06:00
return [
2026-10-05 18:10:18 -04:00
'allocation_ip' => $ip,
'allocation_ports' => $normalizedPorts,
'allocation_alias' => $alias,
2018-01-27 12:38:56 -06:00
];
}
}