<?php
/**
 * @link      http://www.u-bo.com
 * @copyright 南京友博网络科技有限公司 
 * @license   http://www.u-bo.com/license/
 */

namespace common\components;

use Yii;

/**
 * 常用工具函数
 * 
 * @author legendjw http://blog.u-bo.com
 * @since 0.1
 */
class Tool 
{
	//简短dump
	public static function dump($var) {
		return \yii\helpers\VarDumper::dump($var, 10, true);
	}

	/**
	 * 将返回的数据集转换成树
	 * @param  array   $list  数据集
	 * @param  string  $pk    主键
	 * @param  string  $pid   父节点名称
	 * @param  string  $child 子节点名称
	 * @param  integer $root  根节点ID
	 * @return array          转换后的树
	 */
	public static function list_to_tree($list, $pk = 'id', $pid = 'pid', $child = '_child', $root = 0) {
		$tree = array(); // 创建Tree
		if (is_array($list)) {
			// 创建基于主键的数组引用
			$refer = array();
			foreach ($list as $key => $data) {
				$refer[$data[$pk]] = & $list[$key];
			}

			foreach ($list as $key => $data) {
				// 判断是否存在parent
				$parentId = $data[$pid];
				if ($root == $parentId) {
					$tree[$data[$pk]] = & $list[$key];
				} else {
					if (isset($refer[$parentId])) {
						$parent = & $refer[$parentId];
						$parent[$child][] = & $list[$key];
					}
				}
			}
		}
		return $tree;
	}

	/**
	 * 直接在trace信息里打印字段
	 */
	public static function fb($what) {
		echo Yii::trace(\yii\helpers\VarDumper::dumpAsString($what), 'vardump');
	}

	/**
	 * 字符串截取
	 * @param char $string    截取对象
	 * @param int $length     截取长度
	 * @param char $etc       超出长度结束符
	 * @param boolean $title  是否有title效果
	 * @return string
	 */
	public static function truncate_utf8_string($string, $length, $etc = '...', $title = false) {
		$result = '';
		$string = html_entity_decode(trim(strip_tags($string)), ENT_QUOTES, 'UTF-8');
		$strlen = strlen($string);
		for ($i = 0; (($i < $strlen) && ($length > 0)); $i++) {
			if ($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, '0', STR_PAD_LEFT), '0')) {
				if ($length < 1.0) {
					break;
				}
				$result .= substr($string, $i, $number);
				$length -= 1.0;
				$i += $number - 1;
			} else {
				$result .= substr($string, $i, 1);
				$length -= 0.5;
			}
		}
		$result = htmlspecialchars($result, ENT_QUOTES, 'UTF-8');
		if ($i < $strlen) {
			$result .= $etc;
		}
		if ($title)
            $result = "<span title='" . $string . "'>" . $result . "</span>";
        return $result;
    }

    /**
     * 判断是否是手机请求
     */
    public static function is_mobile_request()  
    {  
        $_SERVER['ALL_HTTP'] = isset($_SERVER['ALL_HTTP']) ? $_SERVER['ALL_HTTP'] : '';  
        $mobile_browser = '0';  
        if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|iphone|ipad|ipod|android|xoom)/i', strtolower($_SERVER['HTTP_USER_AGENT'])))  
            $mobile_browser++;  
        if((isset($_SERVER['HTTP_ACCEPT'])) and (strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') !== false))  
            $mobile_browser++;  
        if(isset($_SERVER['HTTP_X_WAP_PROFILE']))  
            $mobile_browser++;  
        if(isset($_SERVER['HTTP_PROFILE']))  
            $mobile_browser++;  
        $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4));  
        $mobile_agents = array(  
            'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',  
            'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',  
            'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',  
            'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',  
            'newt','noki','oper','palm','pana','pant','phil','play','port','prox',  
            'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',  
            'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',  
            'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',  
            'wapr','webc','winw','winw','xda','xda-'
        );  
        if(in_array($mobile_ua, $mobile_agents))  
            $mobile_browser++;  
        if(strpos(strtolower($_SERVER['ALL_HTTP']), 'operamini') !== false)  
            $mobile_browser++;  
        // Pre-final check to reset everything if the user is on Windows  
        if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows') !== false)  
            $mobile_browser=0;  
        // But WP7 is also Windows, with a slightly different characteristic  
        if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows phone') !== false)  
            $mobile_browser++;  
        if($mobile_browser>0)  
            return true;  
        else
            return false;
    }


}
