first commit
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Models\Department;
|
||||
use App\Support\MedicalReport\StructuredTemplateRegistry;
|
||||
use App\Support\MedicalReport\TemplateWorkbook;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class UpdateMedicalReportStructuredTemplateRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return $this->user() !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|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<int, ValidationRule|string>
|
||||
*/
|
||||
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'],
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user