<?php

namespace Lib\Imap;

/**
 * @author:dc
 * @time 2024/10/21 9:47
 * Class Fun
 * @package Lib\Imap
 */
class Fun {

    /**
     * 编码
     * @param $str
     * @param $to
     * @param null $from
     * @return string
     * @author:dc
     * @time 2024/10/21 9:43
     */
    public static function mb_convert_encoding($str, $to, $from = null){

        $from = self::getEncodingAliases($from);


        try {
            return mb_convert_encoding($str,$to,$from);
        }catch (\Throwable $e){}

        return mb_convert_encoding($str,$to);
    }


    /**
     * 只有发现一个处理一个
     * 获取编码
     * @author:dc
     * @time 2024/10/25 17:24
     */
    public static function getEncodingAliases($coding){
        $alias = [
            "iso-8859-8-i"             => "ISO-8859-8",
            "iso-8859-8-e"             => "ISO-8859-8",
            "iso-8859-6-i"             => "ISO-8859-6",
            "iso-8859-6-e"             => "ISO-8859-6",
        ];

        $coding = strtolower($coding);
        if(isset($alias[$coding])){
            return $alias[$coding];
        }

        return $coding;

    }


}