      1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202:

    <?php

    namespace WPGMZA;

    class Query
    {
        private $_type;
        private $_table;

        private $_fields;

        private $_join;
        private $_where;
        private $_groupBy;
        private $_orderBy;
        private $_having;

        private $_limit;

        private $_params;

        public function __construct($parts=null)
        {
            $this->_type = null;
            $this->_table = null;

            $this->_fields  = new QueryFragment();

            $this->_join    = new QueryFragment();
            $this->_where   = new QueryFragment();
            $this->_groupBy = new QueryFragment();
            $this->_orderBy = new QueryFragment();
            $this->_having  = new QueryFragment();

            $this->_placeholders = array();
            $this->_params  = new QueryFields();
        }

        public function __get($name)
        {
            if(isset($this->{"_$name"}))
            {
                if($this->{"_$name"} instanceof QueryFragment)
                    return $this->{"_$name"};

                return $this->{"_$name"};
            }

            return $this->{$name};
        }

        public function __set($name, $value)
        {
            if(isset($this->{"_$name"}) && is_array($this->{"_$name"}) && $name != 'fields' && $name != 'limit')
                throw new \Exception('Property is read only');

            switch($name)
            {
                case 'type':
                    $value = strtoupper($value);
                    $this->{"_$name"} = $value;

                    $this->assertTypeValid();

                    break;

                case 'table':
                    $this->{"_$name"} = $value;

                    $this->assertTableValid();

                    break;

                case 'fields':
                    if(!is_array($value))
                        throw new \Exception('Fields must be an array');

                    foreach($value as $k => $v)
                        $this->_fields->{$k} = $v;

                    break;

                case 'limit':
                    if(!preg_match('/\d+\s*,\s*\d+/', $value))
                        throw new \Exception('Invalid SQL limit');

                    $this->_limit = $value;

                    break;

                default:
                    $this->{$name} = $value;
                    break;
            }

        }

        private function assertTypeValid()
        {
            switch($this->_type)
            {
                case 'INSERT':
                case 'SELECT':
                case 'UPDATE':
                case 'DELETE':
                    break;

                default:
                    throw new \Exception('Invalid query type');
                    break;
            }
        }

        private function assertTableValid()
        {
            if(!is_string($this->_table) || empty($this->_table))
                throw new \Exception('Invalid table');
        }

        public function in($field, $set, $placeholder='%d')
        {
            if(!is_string($field))
                throw new \Exception('Invalid field name');

            if(!is_array($set))
                throw new \Exception('Set must be an array');

            if(empty($set))
                return;

            $count = count($set);

            $placeholders = implode(', ', array_fill(0, $count, $placeholder));

            $this->_where[] = "$field IN ($placeholders)";

            foreach($set as $item)
                $this->_params[] = $item;

            return $this;
        }

        public function build()
        {
            global $wpdb;

            $this->assertTypeValid();
            $this->assertTableValid();

            $qstr = $this->_type;

            switch($this->_type)
            {
                case 'SELECT':
                    if(empty($this->_fields))
                        throw new \Exception('You must specify fields to select');

                    $arr = $this->_fields->toArray();

                    if(!empty($arr))
                        $str = implode(', ', $arr);
                    else
                        $str = '*';

                    $qstr .= " $str FROM";
                    break;

                case 'INSERT':
                    $qstr .= " INTO";
                    break;

                case 'DELETE':
                    $qstr .= " FROM";
                    break;
            }

            $qstr .= " " . $this->_table;

            if(!empty($this->_join))
            {
                $qstr .= ' ';

                foreach($this->_join as $join)
                    $qstr .= 'JOIN ' . $join;
            }

            $where = $this->_where->toArray();
            if(!empty($where))
                $qstr .= " WHERE " . implode(' AND ', $where);

            if(!empty($this->_limit))
                $qstr .= " LIMIT {$this->_limit}";

            $params = $this->_params->toArray();

            if(empty($params))
                return $qstr;

            return $wpdb->prepare($qstr, $params);
        }
    }