<?php

namespace Atom\runtime_7456b800;

/**
 * dom attrs 相关的工具函数
 *
 * @package Atom\runtime_7456b800
 * @auther lvsheng
 */
class Atom_Util_Element {

    /**
     * @param $tag string
     * @return bool
     */
    public static function getTagNamespace($tag) {
        return false;
    }

    /**
     * @param $tag string
     * @return bool
     */
    public static function isReservedTag($tag) {
        return false;
    }

    /**
     * @param $tag string
     * @return string
     */
    public static function parsePlatformTagName($tag) {
        return $tag;
    }

    /**
     * @param $tag string
     * @return bool
     */
    public static function isUnaryTag($tag) {
        static $ALL_UNARG_TAGS = array(
            'area' => 1,
            'base' => 1,
            'br' => 1,
            'col' => 1,
            'embed' => 1,
            'frame' => 1,
            'hr' => 1,
            'img' => 1,
            'input' => 1,
            'isindex' => 1,
            'keygen' => 1,
            'link' => 1,
            'meta' => 1,
            'param' => 1,
            'source' => 1,
            'track' => 1,
            'wbr' => 1
        );
        return isset($ALL_UNARG_TAGS[strtolower($tag)]);
    }

    /**
     * @param $style array|string
     * @return $array
     */
    public static function parseStyleText($style) {

        if (is_string($style)) {

            if (empty($style)) {
                return array();
            }

            $res = array();
            $listDelimiter = "/;(?![^(]*\))/";
            // $propertyDelimiter = "/:.+/";
            $list = preg_split($listDelimiter, $style);
            foreach ($list as $item) {
                if ($item) {
                    $tmp = explode(":", $item);
                    if (count($tmp) > 1) {
                        $res[trim($tmp[0])] = trim($tmp[1]);
                    }
                }
            }
            return $res;
        }
        return $style;
    }

    /**
     * 处理 class 的数据结构
     * @param $arr
     * @return array
     */
    public static function handleClass($arr) {
        $ret = array();
        if (is_array($arr)) {
            if (Atom_Util_Shared::isPlainArray($arr)) {
                foreach ($arr as $k => $v) {
                    if (is_string($v) && $v !== '') {
                        array_push($ret, $v);
                    }
                    else {
                        $ret2 = self::handleClass($v);
                        $ret = array_merge($ret, $ret2);
                    }
                }
            }
            else {
                foreach ($arr as $k => $v) {
                    if ($v) {
                        array_push($ret, $k);
                    }
                }
            }
        }
        elseif (is_string($arr) && $arr !== '') {
            array_push($ret, $arr);
        }

        return $ret;
    }

    /**
     * 把数组的key处理成连字符形式
     * {fontSize: 1px} => {'font-size': 1px}
     * @param $arr
     * @return array
     */
    private static function handleArrayKey($arr) {
        $ret = array();
        if (is_array($arr)) {
            foreach ($arr as $k => $v) {
                $k = Atom_Util_Shared::inCamelize($k);
                $ret[$k] = $v;
            }
        }

        return $ret;
    }

    /**
     * 处理style的数据结构
     * @param $arr
     * @return array
     */
    public static function handleStyle($arr) {
        $ret = array();
        if (is_string($arr)) {
            if ($arr === '') {
                return $ret;
            }
            $arr = Atom_Util_Element::parseStyleText($arr);
        }
        if (is_array($arr) && !empty($arr)) {
            if (Atom_Util_Shared::isPlainArray($arr)) {
                foreach ($arr as $v) {
                    if (is_array($v)) {
                        $v = self::handleArrayKey($v);
                    }
                    $ret = array_merge($ret, $v);
                }
            }
            else {
                $ret = self::handleArrayKey($arr);
            }
        }
        return $ret;
    }

    static public $propsToAttrMap = array(
        'acceptCharset' => 'accept-charset',
        'className' => 'class',
        'htmlFor' => 'for',
        'httpEquiv' => 'http-equiv'
    );

}
