109 lines
3.6 KiB
PHP
109 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use App\Actions\Permissions\PermissionService;
|
|
use App\Actions\Projects\DuplicateProjectConfiguration;
|
|
use App\Enums\Permission;
|
|
use App\Models\Project;
|
|
use Illuminate\Contracts\Validation\ValidationRule;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class StoreProjectRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return $this->user()?->can('create-project') ?? false;
|
|
}
|
|
|
|
protected function prepareForValidation(): void
|
|
{
|
|
$key = Arr::get($this->all(), 'key');
|
|
$creationMode = Arr::get($this->all(), 'creation_mode', 'scratch');
|
|
$copyAreas = Arr::get($this->all(), 'copy_areas', []);
|
|
|
|
if ($key !== null) {
|
|
$this->merge([
|
|
'key' => strtoupper(trim((string) $key)),
|
|
]);
|
|
}
|
|
|
|
$this->merge([
|
|
'creation_mode' => is_string($creationMode) ? trim($creationMode) : 'scratch',
|
|
'copy_areas' => is_array($copyAreas)
|
|
? array_values($copyAreas)
|
|
: ($copyAreas === null || $copyAreas === '' ? [] : [(string) $copyAreas]),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array<string, ValidationRule|array<mixed>|string>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'creation_mode' => ['required', 'string', Rule::in(['scratch', 'duplicate', 'template'])],
|
|
'key' => [
|
|
'required',
|
|
'string',
|
|
'max:5',
|
|
'regex:/^[A-Z0-9]+$/',
|
|
Rule::unique('projects', 'key'),
|
|
],
|
|
'name' => ['required', 'string', 'max:255'],
|
|
'source_project_id' => [
|
|
Rule::requiredIf(fn (): bool => $this->input('creation_mode') === 'duplicate'),
|
|
'nullable',
|
|
'integer',
|
|
Rule::exists('projects', 'id'),
|
|
function (string $attribute, mixed $value, \Closure $fail): void {
|
|
if ($value === null || $value === '') {
|
|
return;
|
|
}
|
|
|
|
$project = Project::query()->find($value);
|
|
|
|
if ($project === null) {
|
|
return;
|
|
}
|
|
|
|
$permissionService = app(PermissionService::class);
|
|
|
|
if (! $permissionService->allows($this->user(), Permission::ProjectRead, $project)) {
|
|
$fail('Select a project you can access as the duplication source.');
|
|
}
|
|
},
|
|
],
|
|
'source_template_id' => [
|
|
Rule::requiredIf(fn (): bool => $this->input('creation_mode') === 'template'),
|
|
'nullable',
|
|
'integer',
|
|
Rule::exists('project_templates', 'id')->where(fn ($query) => $query->where('is_published', true)),
|
|
],
|
|
'copy_areas' => ['nullable', 'array'],
|
|
'copy_areas.*' => [
|
|
'string',
|
|
Rule::in(DuplicateProjectConfiguration::copyableAreas()),
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array<string, string>
|
|
*/
|
|
public function messages(): array
|
|
{
|
|
return [
|
|
'source_project_id.required' => 'Select a source project to duplicate from.',
|
|
'source_template_id.required' => 'Select a template to create the project from.',
|
|
];
|
|
}
|
|
}
|