78 lines
2.5 KiB
PHP
78 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Back\Sites;
|
|
|
|
use App\Models\Site;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Facades\Gate;
|
|
|
|
/**
|
|
* Handles authorization and validation for creating a new Site.
|
|
*/
|
|
class StoreSiteRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return Gate::check('create', Site::class);
|
|
}
|
|
|
|
/**
|
|
* Prepare the data for validation.
|
|
*
|
|
* @return void
|
|
*/
|
|
#[\Override]
|
|
protected function prepareForValidation(): void
|
|
{
|
|
$this->merge([
|
|
'project_manager_id' => is_array($this->project_manager_id)
|
|
? intval($this->project_manager_id[0])
|
|
: $this->project_manager_id,
|
|
'gitlab_project_id' => is_array($this->gitlab_project_id)
|
|
? intval($this->gitlab_project_id[0])
|
|
: $this->gitlab_project_id,
|
|
'users' => is_array($this->users) ? array_map(intval(...), $this->users) : $this->users,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'name' => 'required|string|between:3,255|unique:sites,name',
|
|
'url_re7' => 'nullable|string|url:http,https|unique:sites,url_re7',
|
|
'url_prod' => 'nullable|string|url:http,https|unique:sites,url_prod',
|
|
'project_manager_id' => 'required|integer|exists:users,id',
|
|
'gitlab_project_id' => 'required|integer|exists:gitlab_projects,id|unique:sites,gitlab_project_id',
|
|
'users' => 'nullable|array',
|
|
'users.*' => 'integer|exists:users,id|distinct',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get custom attributes for validator errors.
|
|
*
|
|
* @return array<string, string>
|
|
*/
|
|
#[\Override]
|
|
public function attributes(): array
|
|
{
|
|
return [
|
|
'name' => trans('validation.custom.name'),
|
|
'url_re7' => trans('validation.custom.url_re7'),
|
|
'url_prod' => trans('validation.custom.url_prod'),
|
|
'project_manager_id' => trans('validation.custom.project_manager_id'),
|
|
'gitlab_project_id' => trans_choice('models.classes.gitlab-projects', 1),
|
|
'users' => trans_choice('uwil-backend::models.classes.users', \INF),
|
|
];
|
|
}
|
|
}
|