<?php
namespace BexioConnectorByHelvy;

defined( 'ABSPATH' ) || exit;

class Utils {
    /**
     * Extract the domain or hostname from a URL.
     *
     * @param string $url The URL to extract the domain from.
     * @return string|null The extracted domain or hostname, or null if invalid URL.
     */
    public static function extract_domain($url) {
        $parsed_url = wp_parse_url($url);
        
        // Check if the URL parsing was successful and contains a host component
        if ($parsed_url && isset($parsed_url['host'])) {
            if (isset($parsed_url['port']) && $parsed_url['port'] != 80 && $parsed_url['port'] != 443) {
                return $parsed_url['host'] . ':' . $parsed_url['port'];
            } else {
                return $parsed_url['host'];
            }
        }
        
        // Return null if the URL is invalid or doesn't contain a host component
        return null;
    }
}