name = $billingUnit->name; $this->billing_address = $billingUnit->billingAddress ?? ''; $this->e_invoice_address = $billingUnit->eInvoiceAddress ?? ''; $this->e_invoice_operator = $billingUnit->eInvoiceOperator ?? ''; $this->reference = $billingUnit->reference ?? ''; } public function toData(): BillingUnitData { $this->validate(); return BillingUnitData::fromArray([ 'name' => $this->name, 'billing_address' => $this->billing_address, 'e_invoice_address' => $this->e_invoice_address, 'e_invoice_operator' => $this->e_invoice_operator, 'reference' => $this->reference, ]); } /** * Shows a broken business rule as an error on the field it concerns. */ public function showDomainError(DomainException $exception): void { [$field, $key] = match ($exception::class) { InvalidBillingUnitName::class => ['name', 'customers.errors.invalid_billing_unit_name'], InvalidEInvoiceAddress::class => ['e_invoice_address', 'customers.errors.invalid_e_invoice_address'], EInvoiceOperatorRequired::class => ['e_invoice_operator', 'customers.errors.e_invoice_operator_required'], default => throw $exception, }; $this->addError($field, __($key)); } /** * @return array> */ protected function rules(): array { return [ 'name' => ['required', 'string', 'max:255'], 'billing_address' => ['nullable', 'string', 'max:1000'], 'e_invoice_address' => ['nullable', 'string', 'max:50'], 'e_invoice_operator' => ['nullable', 'string', 'max:35'], 'reference' => ['nullable', 'string', 'max:255'], ]; } /** * @return array */ protected function messages(): array { return [ 'name.required' => __('customers.errors.billing_unit_name_required'), 'name.max' => __('customers.errors.invalid_billing_unit_name'), 'billing_address.max' => __('customers.errors.too_long', ['max' => 1000]), 'e_invoice_address.max' => __('customers.errors.invalid_e_invoice_address'), 'e_invoice_operator.max' => __('customers.errors.too_long', ['max' => 35]), 'reference.max' => __('customers.errors.too_long', ['max' => 255]), ]; } }