user() !== null; } /** * Get the validation rules that apply to the request. * * @return array|string> */ public function rules(): array { /** @var TemplateWorkbook $templateWorkbook */ $templateWorkbook = app(TemplateWorkbook::class); /** @var StructuredTemplateRegistry $structuredTemplateRegistry */ $structuredTemplateRegistry = app(StructuredTemplateRegistry::class); $departmentKeys = array_map( fn (array $department): string => $department['key'], $templateWorkbook->departments(), ); $reportDepartment = $this->string('department')->toString(); $template = $structuredTemplateRegistry->templateForDepartment($reportDepartment); $rules = [ 'department' => ['required', 'string', Rule::in($departmentKeys)], 'template_key' => ['required', 'string', Rule::in(array_filter([$template['key'] ?? null]))], ]; if (isset($template['sections']) && is_array($template['sections'])) { $rules['sections'] = ['required', 'array']; foreach ($template['sections'] as $section) { $rules['sections.'.$section['key'].'.entries'] = ['nullable', 'array']; foreach ($section['fields'] ?? [] as $field) { $rules['sections.'.$section['key'].'.entries.*.'.$field['key']] = $this->fieldRules($field['type'] ?? 'text'); } } return $rules; } $rules['entries'] = ['required', 'array']; foreach ($template['fields'] ?? [] as $field) { $rules['entries.*.'.$field['key']] = $this->fieldRules($field['type'] ?? 'text'); } return $rules; } /** * @return array */ private function fieldRules(string $fieldType): array { return match ($fieldType) { 'department-select' => [ 'required', 'integer', Rule::exists((new Department)->getTable(), 'id'), ], 'number' => ['required', 'numeric'], default => ['required', 'string', 'max:255'], }; } }