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> */ 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 */ 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.', ]; } }