<?php
/**
 * 简易数据库操作类
 *
 * @author Jinhui Zhu<jinhui.zhu@live.cn>2015-12-10 14:35
 *
 * usage:
 *
 *
 */
namespace Qii\Driver;

use Qii\Exceptions\InvalidParams;

/**
 * 用法 ：
 * 首先需要创建 _ormRules 文件 rules/user.php
    use Qii\Base\Rules;
    class user extends Rules {
        public $table = 'user';
        pubic function fields(){
           return ['uid', 'nickname', 'password'];
        }
        public function constants() {
            $this->addRequiredFields(
                ['id', '用户ID不能为空'],
                ['nickname', '昵称不能为空'],
                ['password', '密码不能为空'],
            );

            $this->addNumberFields(
                ['uid', '用户ID必须是数字']
            );
            $this->addSafeFields(
                ['nickname', '昵称格式不正确，不能包含怪字符']
            );
            $this->addPasswordFields(
                ['password', '密码格式不正确']
            );
        }
    }
    use Qii\Driver\EasyDrive;

    class user extends EasyDrive {

        public $_ormRules = 'rules\user';
        public function __construct()
        {
            parent::__construct();
        }
    }

    $user = new user();
    $user->uid = 1;
    $user->nickname = 'admin';
    $user->password = md5('asdfadsfasdadfadf');
    $response = $user->save();

    $user->setPrivatekey(['uid']);
    $response = $user->exist();

    if($response->isError()) {
        return ['code' => $response->getCode(), 'msg' => $response->getMessage()]
   }
   if($response->count() > 0) {
        return $response->getResult();
   }
 *
 * @method EasyORM setQueryFields(array $fields)
 * @method void cleanQueryFields()
 * @method void cleanValues()
 * @method EasyORM setLimit(int $start, int $end)
 * @method EasyORM cleanLimit()
 * @method EasyORM  cleanCondition()
 * @method EasyORM cleanFieldsVal()
 */
class EasyDrive {
    /**
     * 数据表验证规则
     * @var string
     */
    public $_ormRules = '';
    /**
     * 本方法
     * @var EasyORM
     */
    protected $_easyORM;
    /**
     * 数据库操作类
     * @var mixed
     */
    protected $db;
    /**
     * 验证器
     * @var mixed
     */
    public $_validator;
    private $_message = [];
    /**
     * @param string $rules 规则类名
     * @throws InvalidParams
     */
    public function __construct($rules = ''){
        if ($this->_ormRules == '') {
            throw new InvalidParams('_ormRules is null');
        }

        if($rules != '') {
            $this->_ormRules = $rules;
        }

        $rules = new $this->_ormRules();
        $this->_easyORM = new EasyORM($rules);

        $this->db = $this->getDB();
        $this->_validator = _library("Validate");
    }

    /**
     * 返回消息提示
     *
     * @return void
     */
    public function getMessage() {
        return $this->_message;
    }
    /**
     * 验证字段
     *
     * @return bool
     */
    public function verify() {
        return true;
    }
    /**
     * 清除数据
     *
     * @return EasyORM
     */
    final public function clean() {
        return $this->_easyORM->clean();
    }

    /**
     * 获取DB属性
     * @return mixed
     */
    public function getDB() {
        return $this->_easyORM->getDB();
    }

    /**
     * 获取规则
     *
     * @return mixed
     */
    public function getRules() {
        return $this->_easyORM->getRules();
    }

    /**
     * has field
     * @param string $key
     * @return mixed
     */
    public function hasField($key) {
        return $this->_easyORM->fields->hasField($key);
    }
    /**
     * 获取所有的字段
     *
     * @return mixed
     */
    public function getFields() {
        return $this->_easyORM->getFields();
    }

    /**
     * 获取指定字段的值
     *
     * @param string $field
     * @return mixed
     */
    public function getField($field) {
        return $this->_easyORM->getFieldValue($field);
    }

    /**
     * 设置表名，将rules暴露到外边
     *
     * @param string $table 表名
     * @return mixed
     */
    public function setTable($table) {
        $this->_easyORM->setTable($table);
        return $this;
    }

    /**
     * 返回表名
     *
     * @return mixed
     */
    public function getTable() {
        return $this->_easyORM->getTable();
    }

    /**
     * 获取字段及值 stdClass
     *
     * @return mixed
     */
    public function getValues() {
        return $this->_easyORM->getValues();
    }

    /**
     * 获取字段及值 array
     * @return mixed
     */
    public function getValuesAsArray() {
        return $this->_easyORM->getValueAsArray();
    }

    /**
     * 验证数组中的数据
     *
     * @param array $values 需要验证的数组
     * @return mixed
     */
    public function verifyFields($values) {
        return $this->_easyORM->getRules()->verifyFields($values);
    }

    public function __set($name, $value) {
        $this->_easyORM->fields->$name = $value;
    }

    public function __isset($name){
        return isset($this->_easyORM->fields->$name);
    }

    public function __unset($name) {
        if(isset($this->_easyORM->fields->$name)) unset($this->_easyORM->fields->$name);
    }
    /**
     * getter
     * @param string $name
     * @return void
     */
    public function __get($name) {
        return $this->_easyORM->getFieldValue($name);
    }
    /**
     * 设置主键
     *
     * @param array $array 数组形式
     * @return EasyORM
     */
    final public function setPrivateKey($array) {
        $this->_easyORM->setPrivateKey($array);
        return $this;
    }

    /**
     * 设置where条件
     *
     * @param array $args
     * @return $this
     */
    final public function where($args) {
        $this->_easyORM->where($args);
        return $this;
    }

    /**
     * 清除 where 条件
     * @return $this
     */
    final public function cleanWhere() {
        $this->_easyORM->cleanWhere();
        return $this;
    }
    /**
     * 清除主键
     *
     * @return $this
     */
    final public function cleanPrivateKey() {
        $this->_easyORM->cleanPrivateKey();
        return $this;
    }

    /**
     * 设置排除的内容
     *
     * @param array $array
     * @return $this
     */
    final public function setExclude($array) {
        $this->_easyORM->setExcluded($array);
        return $this;
    }

    /**
     * 清除排除选项
     *
     * @return $this
     */
    final public function cleanExclude() {
        $this->_easyORM->cleanExcluded();
        return $this;
    }
    /**
     * like([['key' => 'value'], ...]->count() like([['key' => 'value'], ...]->lists()
     * @param $values
     * @return Qii\Driver\Response
     */
    final public function like($values = array()) {
        return $this->_easyORM->like($values);
    }
    /**
     * 获取指定条件返回数据的行数
     *
     * @return Qii\Driver\Response
     */
    final public function count() {
        return $this->_easyORM->count();
    }
    /**
     * update
     * @return Qii\Driver\Response
     */
    final public function update(){
        return $this->_easyORM->update();
    }
    /**
     * 删除指定数据
     *
     * @return Response
     */
    final public function remove() {
        return $this->_easyORM->remove();
    }

    /**
     * 更新字段，与update的区别是只验更新字段的值，不验证update设定的字段
     *
     * @return mixed|Response
     */
    final public function updateFields(){
        return $this->_easyORM->updateFields();
    }

    /**
     * lists
     *
     * @return mixed
     */
    final public function lists() {
        return $this->_easyORM->lists();
    }
    /**
     * 设置order by
     * @param $orderBy
     * @return $this
     */
    final public function setOrderBy($orderBy) {
        return $this->_easyORM->setOrderBy($orderBy);
    }

    /**
     * 分组
     * @param array $groupBy 分组字段数组
     * @return EasyORM
     */
    final public function setGroupBy($groupBy) {
        return $this->_easyORM->setGroupBy($groupBy);
    }

    /**
     * 清除 group by
     * @return EasyORM
     */
    final public function cleanGroupBy() {
        return $this->_easyORM->cleanGroupBy();
    }
    /**
     * @return Qii\Driver\Response|mixed
     */
    final public function save() {
        return $this->_easyORM->save();
    }

    /**
     * incr 某一个字段
     * @return mixed|Qii\Driver\Response
     */
    final public function incr() {
        return $this->_easyORM->incr();
    }
    /**
     * 检查数据是否已经存在,并返回一行，只能根据主键查询
     *
     * @return Response
     */
    final public function exist() {
        return $this->_easyORM->exist();
    }
    /**
     * __call 自动调用_easyORM中的方法
     *
     * @param string $method
     * @param mixed $args
     * @return mixed
     */
    public function __call($method, $args) {
        return call_user_func_array(array($this->_easyORM, $method), $args);
    }
}