private function importTel()

in app/Services/VCard/ImportVCard.php [846:900]


    private function importTel(Contact $contact, VCard $entry): void
    {
        if (is_null($entry->TEL)) {
            return;
        }

        $contactFieldTypeId = $this->getContactFieldTypeId(ContactFieldType::PHONE);
        if (! $contactFieldTypeId) {
            // Case of contact field type phone does not exist
            return;
        }

        $phones = $contact->contactFields()
                            ->phone()
                            ->get()
                            ->sortBy('id');

        $countryISO = VCardHelper::getCountryISOFromSabreVCard($entry);

        foreach ($entry->TEL as $tel) {
            $data = (string) $tel;
            $data = LocaleHelper::formatTelephoneNumberByISO($data, $countryISO, Str::startsWith($data, '+') ? \libphonenumber\PhoneNumberFormat::INTERNATIONAL : \libphonenumber\PhoneNumberFormat::NATIONAL);

            $contactFieldContent = [
                'account_id' => $contact->account_id,
                'contact_id' => $contact->id,
                'contact_field_type_id' => $contactFieldTypeId,
                'data' => $this->formatValue($data),
                'labels' => preg_split('/,/', (string) $tel['TYPE']),
            ];

            // We assume contact fields are in the same order
            $phone = $phones->shift();

            if (is_null($phone)) {
                // Address does not exist
                app(CreateContactField::class)->execute($contactFieldContent);
            } else {
                // Address has to be updated
                app(UpdateContactField::class)->execute([
                    'contact_field_id' => $phone->id,
                ] +
                    $contactFieldContent
                );
            }
        }

        foreach ($phones as $phone) {
            // Remaining phones have to be removed
            app(DestroyContactField::class)->execute([
                'account_id' => $contact->account_id,
                'contact_field_id' => $phone->id,
            ]);
        }
    }