Files
econom-calculator/app/Http/Requests/UpdateMedicalReportStructuredTemplateRequest.php
brusnitsyn 3edc8e667e
Some checks failed
tests / ci (8.5) (push) Has been cancelled
linter / quality (push) Has been cancelled
tests / ci (8.3) (push) Has been cancelled
tests / ci (8.4) (push) Has been cancelled
first commit
2026-04-03 17:20:05 +09:00

84 lines
2.7 KiB
PHP

<?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'],
};
}
}