value; public string $business_id = ''; /** * A new personal identity code. Never filled from the stored code: empty * keeps the stored one. */ public string $personal_identity_code = ''; public bool $remove_personal_identity_code = false; public function fillFromSummary(CustomerSummary $customer): void { $this->name = $customer->name; $this->type = $customer->type->value; $this->business_id = $customer->businessId ?? ''; } public function toData(): CustomerDetailsData { $this->validate(); return CustomerDetailsData::fromArray([ 'name' => $this->name, 'type' => $this->type, 'business_id' => $this->type === CustomerType::Consumer->value ? null : $this->business_id, 'personal_identity_code' => $this->type === CustomerType::Consumer->value ? $this->personal_identity_code : null, 'remove_personal_identity_code' => $this->remove_personal_identity_code, ]); } /** * Shows a broken business rule as an error on the field it concerns. */ public function showDomainError(DomainException $exception): void { [$field, $key] = match ($exception::class) { InvalidCustomerName::class => ['name', 'customers.errors.invalid_name'], InvalidBusinessId::class => ['business_id', 'customers.errors.invalid_business_id'], ConsumerCannotHaveBusinessId::class => ['business_id', 'customers.errors.consumer_business_id'], BusinessIdAlreadyInUse::class => ['business_id', 'customers.errors.business_id_taken'], InvalidPersonalIdentityCode::class => ['personal_identity_code', 'customers.errors.invalid_personal_identity_code'], CompanyCannotHavePersonalIdentityCode::class => ['personal_identity_code', 'customers.errors.company_personal_identity_code'], InternalCustomerMustBeCompany::class => ['type', 'customers.errors.internal_must_be_company'], PersonalIdentityCodeAlreadyInUse::class => ['personal_identity_code', 'customers.errors.personal_identity_code_taken'], default => throw $exception, }; $this->addError($field, __($key)); } /** * @return array> */ protected function rules(): array { return [ 'name' => ['required', 'string', 'max:255'], 'type' => ['required', Rule::enum(CustomerType::class)], 'business_id' => ['nullable', 'string', 'max:20'], 'personal_identity_code' => ['nullable', 'string', 'max:20'], 'remove_personal_identity_code' => ['boolean'], ]; } /** * @return array */ protected function messages(): array { return [ 'name.required' => __('customers.errors.name_required'), 'name.max' => __('customers.errors.invalid_name'), 'type.required' => __('customers.errors.type_required'), 'type.enum' => __('customers.errors.type_required'), 'business_id.max' => __('customers.errors.invalid_business_id'), 'personal_identity_code.max' => __('customers.errors.invalid_personal_identity_code'), ]; } }