parse( $raw, $region ); } catch ( \Exception $e ) { return null; } if ( ! $util->isValidNumber( $proto ) ) { return ''; } $type = $util->getNumberType( $proto ); $mobile = \libphonenumber\PhoneNumberType::MOBILE; $either = \libphonenumber\PhoneNumberType::FIXED_LINE_OR_MOBILE; if ( $type !== $mobile && $type !== $either ) { return ''; } $e164 = $util->format( $proto, \libphonenumber\PhoneNumberFormat::E164 ); return ltrim( $e164, '+' ); } /** * Lightweight fallback normaliser. * * @param string $raw Raw input. * @param string $region Resolved ISO alpha-2 region. * @param string $default_country Fallback ISO when region has no calling code. * @return string|null */ private static function normalize_fallback( $raw, $region, $default_country ) { $has_plus = ( 0 === strpos( $raw, '+' ) ); $digits = preg_replace( '/\D/', '', $raw ); if ( '' === $digits ) { return null; } $international = false; if ( $has_plus ) { $international = true; } elseif ( 0 === strpos( $digits, '00' ) ) { $international = true; $digits = substr( $digits, 2 ); } if ( $international ) { $d = $digits; } else { $national = preg_replace( '/^0+/', '', $digits ); $cc = CountryCodes::calling_code( $region ); if ( null === $cc ) { $cc = CountryCodes::calling_code( $default_country ); } if ( null === $cc ) { $cc = '48'; } // Treat as already-international only when it starts with the country // code AND the remainder is a plausible national-number length (>= 8). // This avoids double-prepending for e.g. a US "1XXXXXXXXXX" while not // mistaking a national number that merely begins with the CC digits. $cc_len = strlen( $cc ); if ( 0 === strpos( $national, $cc ) && strlen( $national ) >= $cc_len + 8 && strlen( $national ) <= $cc_len + 11 ) { $d = $national; } else { $d = $cc . $national; } } $len = strlen( $d ); if ( $len < 8 || $len > 15 ) { return null; } return $d; } }