CoreFiles by mySites.guru
AbstractIdnaConvert.php41 lines, 1,080 bytes, from Joomla_5.3.0-rc1-Release_Candidate-Full_Package.zip
Raw History Compare with 5.2.4-rc1
md5
a3a3556221b50123e587421fb3d1c0f8
sha1
70e5e2eda08d29be1fbbd06999a33b5f1d7508e8
sha256
9a9edb7a50f2509cbb1ecae45950ef6ffe5cd928f3e450a5abed29d112e44e37
<?php declare(strict_types=1);

namespace Algo26\IdnaConvert;

use InvalidArgumentException;

abstract class AbstractIdnaConvert
{
    abstract public function convert(string $host): string;

    public function convertEmailAddress(string $emailAddress): string
    {
        if ( ! str_contains($emailAddress, '@')) {
            throw new InvalidArgumentException('The given string does not look like an email address', 206);
        }

        $parts = explode('@', $emailAddress);

        return sprintf(
            '%s@%s',
            $parts[0],
            $this->convert($parts[1])
        );
    }

    public function convertUrl(string $url): string
    {
        $parsed = parse_url($url);
        if ($parsed === false) {
            throw new InvalidArgumentException('The given string does not look like a URL', 206);
        }

        // Nothing to do
        if (!isset($parsed['host']) || $parsed['host'] === null) {
            return $url;
        }
        $parsed['host'] = $this->convert($parsed['host']);

        return http_build_url($parsed);
    }
}

The same content is in 4 releases, Joomla 5.3.0-rc1 to 6.0.0-alpha3. See them all, or every version of this file.