value; public string $business_id = ''; 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, ]); } /** * 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'], 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'], ]; } /** * @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'), ]; } }