parse( $normalized, $region_for_parsing ); if ( ! $util->isValidNumber( $parsed_number ) ) { return ValidationResult::failure( ValidationResult::ERROR_INVALID_NUMBER, __( 'The phone number is not valid.', 'shift64-phone-validation' ) ); } return ValidationResult::success( $parsed_number ); } catch ( NumberParseException $e ) { return self::handle_parse_exception( $e ); } } /** * Check if a phone number is international (starts with '+'). * * @param string $phone_number The normalized phone number. * @return bool True if the number starts with '+'. */ public static function is_international_number( string $phone_number ): bool { return 0 === strpos( $phone_number, '+' ); } /** * Handle NumberParseException and return appropriate error message. * * @param NumberParseException $exception The parse exception. * @return ValidationResult The failure result with appropriate message. */ private static function handle_parse_exception( NumberParseException $exception ): ValidationResult { $error_type = $exception->getErrorType(); switch ( $error_type ) { case NumberParseException::INVALID_COUNTRY_CODE: $code = ValidationResult::ERROR_INVALID_COUNTRY_CODE; $message = __( 'Invalid country code.', 'shift64-phone-validation' ); break; case NumberParseException::NOT_A_NUMBER: $code = ValidationResult::ERROR_NOT_A_NUMBER; $message = __( 'The input does not appear to be a phone number.', 'shift64-phone-validation' ); break; case NumberParseException::TOO_SHORT_AFTER_IDD: $code = ValidationResult::ERROR_TOO_SHORT; $message = __( 'Phone number is too short after country code.', 'shift64-phone-validation' ); break; case NumberParseException::TOO_SHORT_NSN: $code = ValidationResult::ERROR_TOO_SHORT; $message = __( 'Phone number is too short.', 'shift64-phone-validation' ); break; case NumberParseException::TOO_LONG: $code = ValidationResult::ERROR_TOO_LONG; $message = __( 'Phone number is too long.', 'shift64-phone-validation' ); break; default: $code = ValidationResult::ERROR_PARSE_ERROR; $message = __( 'Unable to parse phone number.', 'shift64-phone-validation' ); break; } return ValidationResult::failure( $code, $message ); } }