<?php
// Copyright 1999-2017. Plesk International GmbH. All rights reserved.

namespace Plesk\MigrationLibV8\Utils;

use Plesk\MigrationLibV8\Plesk\PleskFunctions;

/**
 * Collection of methods useful to work with PHP strings
 */
class StringUtils
{
    /**
     * Normalize domain name for comparison - convert to lowercase punycode form.
     *
     * @param string $domainName original domain name
     * @return string normalized domain name
     */
    public static function normalizeDomainName($domainName)
    {
        return strtolower(PleskFunctions::IDNToASCII($domainName));
    }

    /**
     * Normalize domain names for comparison - convert to lowercase punycode form.
     *
     * @param string[] $domainNames array of original domain names
     * @return array array of normalized domain names
     */
    public static function normalizeDomainNames($domainNames)
    {
        $normalized = [];
        foreach ($domainNames as $domainName) {
            $normalized[] = self::normalizeDomainName($domainName);
        }
        return $normalized;
    }

    /**
     * Normalize domain names for comparison, which are used as array keys - convert to lowercase punycode form.
     *
     * @param string[] $dataArray array, where each key is a domain name
     * @return array array where each key is normalized domain name, value is the same as in original array
     */
    public static function normalizeDomainNameKeys($dataArray)
    {
        $normalized = [];
        foreach ($dataArray as $domainName => $data) {
            $normalized[self::normalizeDomainName($domainName)] = $data;
        }
        return $normalized;
    }
}
