<?php
/**
 * Created by PhpStorm.
 * User: cpueblo
 * Date: 2016. 8. 6.
 * Time: 오후 5:00
 */

namespace platyFramework;

// require_once(__DIR__ . "/loader.php");


/**
 * @property loader   $loader
 * @property request $request;
 * @property response $response;
 * @property ptyMysql $db;
 * @property ptyMysql $countDb;
 * @property ptyCliLog $cliLog;
 * @property ptyuserManagerModel $userManager
 * @property ptyuserItemModel $signedInItem;
 */
class model
{
    var $_tableName;

	public function __construct()
	{
        $this->_checkTableExistsAndCreate();
	}

    protected function _checkTableExistsAndCreate()
    {

    }

	public function __toString() {
		return "platyFramework\\model";
	}

	public function callAction($func, &$args = null, &$args2 = null, &$args3 = null)
	{
		GLOBAL $platyFramework;
		$platyFramework->callAction($func, $args, $args2, $args3);
	}

    public function getAction($func, &$args = null, &$args2 = null)
    {
        GLOBAL $platyFramework;
        return $platyFramework->getAction($func, $args, $args2);
    }


	public function __get($key)
	{
		GLOBAL $platyFramework;
		if ($platyFramework)
			return $platyFramework->__get($key);
		else {
			debug_print_backtrace();
			die ("platyFramework::model::__get($key) error. this->platyFramework is null");
		}
	}

    /**
     * [cpueblo] 2018-03-14
     * 다음의 데이터를 http_build_query 로 리턴합니다. 리턴한 함수들은 parse_str 로 가공하며, get_defined_vars() 를 통해 확인할 수 있습니다
     * ptyDebug(get_defined_vars());
     *
     * $_POST['data']
     * $this->>request->post
     * function args
     *
     * @return array|string
     */
    public function getParam()
    {
        $p = array();

        // post 에 data 로 넘어온 경우 세팅
        if (isset($_POST['data'])) {
            ptyParseStr($_POST['data'], $p);
            ptyTrim($p);
        }

        // post 에 직접 넘어온 경우
        else $p = $this->request->request;      // post 로 강제할것인가, get & post 를 모두 받을 것인가

        // 함수 arguments 로 넘어온 경우
        $p = self::funcGetNamedParams($p);

        $p = http_build_query($p);
        return $p;
    }

    public static function funcGetNamedParams($params) {
        $func = debug_backtrace()[2]['function'];
        $args = debug_backtrace()[2]['args'];
        $class = debug_backtrace()[2]['class'];

        // $reflector = new \ReflectionClass(__CLASS__);
        $reflector = new \ReflectionClass($class);
        // $params = [];
        foreach($reflector->getMethod($func)->getParameters() as $k => $parameter){
            // $params[$parameter->name] = isset($args[$k]) ? $args[$k] : $parameter->getDefaultValue();
            if (isset($args[$k]))
                $params[$parameter->name] = $args[$k];
        }

        return $params;
    }

    function returnFalseAlert($alertMsg, $code = "", $focusName = "")
    {
        GLOBAL $platyFramework;
        $arr = array();
        $arr['alertMsg'] = $alertMsg;
        $arr['code'] = $code;
        $arr['focusName'] = $focusName;

        $platyFramework->returnFalse = $arr;
        return false;
    }

    function returnTrue()
    {
        GLOBAL $platyFramework;

        $arr = array();
        $platyFramework->returntrue = null;

        return true;
    }

    /**
     * @return static
     */
    public static function build()
    {
        $list = new static ();
        return $list;
    }




//	public function __set($key, $value)
//	{
//		return parent::__set($key, $value);
////		$this->platyFramework->__set($key, $value);
//	}

/*
	public function setToFramework($key, $value)
	{
		$this->platyFramework->__set($key, $value);
	}
*/
}

?>
