name = $contact->name; $this->title = $contact->title ?? ''; $this->phone = $contact->phone ?? ''; $this->email = $contact->email ?? ''; $this->note = $contact->note ?? ''; } public function toData(): ContactData { $this->validate(); return ContactData::fromArray([ 'name' => $this->name, 'title' => $this->title, 'phone' => $this->phone, 'email' => $this->email, 'note' => $this->note, ]); } /** * Shows a broken business rule as an error on the field it concerns. */ public function showDomainError(DomainException $exception): void { [$field, $key] = match ($exception::class) { InvalidContactName::class => ['name', 'customers.errors.invalid_contact_name'], InvalidEmailAddress::class => ['email', 'customers.errors.invalid_email'], default => throw $exception, }; $this->addError($field, __($key)); } /** * @return array> */ protected function rules(): array { return [ 'name' => ['required', 'string', 'max:255'], 'title' => ['nullable', 'string', 'max:255'], 'phone' => ['nullable', 'string', 'max:50'], 'email' => ['nullable', 'string', 'email', 'max:255'], 'note' => ['nullable', 'string', 'max:2000'], ]; } /** * @return array */ protected function messages(): array { return [ 'name.required' => __('customers.errors.contact_name_required'), 'name.max' => __('customers.errors.invalid_contact_name'), 'title.max' => __('customers.errors.too_long', ['max' => 255]), 'phone.max' => __('customers.errors.too_long', ['max' => 50]), 'email.email' => __('customers.errors.invalid_email'), 'email.max' => __('customers.errors.invalid_email'), 'note.max' => __('customers.errors.too_long', ['max' => 2000]), ]; } }