CertificationRequestValidator.php
1 <?php 2 3 namespace App\Http\Services; 4 5 use App\Enums\SectorialDimensionsEnum; 6 use App\Enums\TechnologicalDimensionsEnum; 7 use App\Enums\UsageDimensionsEnum; 8 use App\Rules\DossierTypeIsValid; 9 use App\Rules\NormVersionExists; 10 use App\Rules\ValidateAdditionalComponents; 11 use App\Rules\ValidateDimensions; 12 use App\Rules\ValidateDossierCodeExists; 13 use App\Rules\ValidateNormScope; 14 use Illuminate\Support\Facades\Validator; 15 16 class CertificationRequestValidator 17 { 18 public function __construct(public array $data) 19 { 20 // 21 } 22 23 public function rules(): array 24 { 25 return [ 26 'laboratory_name' => 'required|string|max:255', 27 'laboratory_trade_name' => 'required|string|max:255', 28 'laboratory_nif' => ['nullable', 'string', 'max:255'], 29 'laboratory_corporate_identity' => ['nullable', 'string', 'max:255'], 30 'laboratory_city_name' => ['nullable', 'string', 'max:255'], 31 'laboratory_zip_code' => ['nullable', 'string', 'max:255'], 32 'laboratory_address' => 'nullable|string|max:255', 33 'laboratory_phone' => 'nullable|string|max:255', 34 'laboratory_email' => ['nullable', 'string', 'max:255'], 35 'laboratory_notification_number' => ['nullable', 'string', 'max:255'], 36 'laboratory_authorization_number' => ['nullable', 'string', 'max:255'], 37 38 'applicant_name' => 'required|string|max:255', 39 'applicant_trade_name' => 'required|string|max:255', 40 'applicant_nif' => 'required|string|max:255', 41 'applicant_corporate_identity' => 'required|string|max:255', 42 'applicant_city_name' => ['nullable', 'string', 'max:255'], 43 'applicant_zip_code' => ['nullable', 'string', 'regex:/^\d{5}(-\d{4})?$/'], 44 'applicant_address' => 'required|string|max:255', 45 'applicant_phone' => 'required|string|max:255', 46 'applicant_email' => 'required|string|max:255', 47 48 'manufacturer_name' => ['required', 'string', 'max:255'], 49 'manufacturer_city_name' => ['nullable', 'string', 'max:255'], 50 'manufacturer_zip_code' => ['nullable', 'string', 'regex:/^\d{5}(-\d{4})?$/'], 51 'manufacturer_address' => ['nullable', 'string', 'max:255'], 52 'manufacturer_phone' => ['nullable', 'string', 'max:255'], 53 54 'toe_name' => 'required|string|max:255', 55 'toe_alias' => 'required|string|max:255', 56 'toe_commercial_name' => ['nullable', 'string', 'max:255'], // TODO: add to the form 57 'toe_version' => ['nullable', 'string', 'max:255'], 58 'toe_support_period' => ['nullable', 'integer', 'min:0'], 59 'toe_description' => ['nullable', 'string'], 60 'toe_sectorial_dimensions' => ['nullable', new ValidateDimensions(SectorialDimensionsEnum::class, 'Sectorial Dimension')], 61 'toe_usage_dimensions' => ['required', new ValidateDimensions(UsageDimensionsEnum::class, 'Usage Dimension')], 62 'toe_technological_dimensions' => ['nullable', new ValidateDimensions(TechnologicalDimensionsEnum::class, 'Technological Dimension')], 63 'toe_security_info_link' => ['nullable', 'url'], 64 'toe_vulnerability_contact_link' => ['nullable', 'url'], 65 'toe_public_vulnerabilities_link' => ['nullable', 'url'], 66 'toe_vulnerability_notifications_via' => ['nullable', 'string', 'max:255'], 67 'toe_usage_guide_link' => ['nullable', 'url'], // or "usague" 68 69 'dossier_norm_version' => ['required', new NormVersionExists], 70 'ref_previous_dossier' => ['nullable', 'string', new ValidateDossierCodeExists], 71 'dossier_evaluation_level' => ['nullable', 'string'], 72 'dossier_additional_components' => ['string', new ValidateAdditionalComponents(CertificationRequestService::getNorm($this->data['dossier_norm_version']), ', ')], 73 'dossier_type' => ['required', new DossierTypeIsValid], 74 'dossier_modo' => ['required', new DossierTypeIsValid($this->data['dossier_type'])], 75 'dossier_subtype' => ['required', new DossierTypeIsValid($this->data['dossier_type'], $this->data['dossier_modo'])], 76 'ref_st' => 'nullable|string|max:255', 77 'ref_strr' => 'nullable|string|max:255', 78 ]; 79 } 80 81 public function validate(): array 82 { 83 $validator = validator::make($this->data, $this->rules()); 84 if (!$validator->fails()) { 85 return []; 86 } 87 88 return $validator->errors()->toArray(); 89 } 90 }