Passed
Branch main (166306)
by Nelson
03:35
created

QueryBuilder::orWhere()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Pin\Libs;
3
4
/**
5
 * QueryBuilder
6
 * Clase encargada de generar consultas usando métodos de apoyo
7
 * @author nelson rojas
8
 */
9
class QueryBuilder {
10
11
    protected $_table = '';
12
    protected $_columns = '';
13
    protected $_conditions = '';
14
    protected $_joins = '';
15
    protected $_limit = '';
16
    protected $_group = '';
17
    protected $_having = '';
18
    protected $_orderBy = '';
19
20
    /**
21
     * constructor
22
     * @params table
23
     */
24
    public function __construct($tableName) {
25
        $this->_table = $tableName;
26
        return $this;
27
    }
28
29
    /**
30
     * columns
31
     * @params columns
32
     */
33
    public function columns($columns) {
34
        $this->_columns = $columns;
35
        return $this;
36
    }
37
38
    /**
39
     * where
40
     * @params condition
41
     * @params operator
42
     */
43
    public function where($condition) {
44
        $operator = 'AND';
45
        if (empty($this->_conditions)) {
46
            $this->_conditions = $condition;
47
        } else {
48
            $this->_conditions .= $operator . ' ' . $condition;
49
        }
50
51
        $this->_conditions .= ' '; // extra space after condition
52
        return $this;
53
    }
54
55
    /**
56
     * orWhere
57
     * @params condition
58
     * @params operator
59
     */
60
    public function orWhere($condition) {
61
        $operator = 'OR';
62
        if (empty($this->_conditions)) {
63
            $this->_conditions = $condition;
64
        } else {
65
            $this->_conditions .= $operator . ' ' . $condition;
66
        }
67
68
        $this->_conditions .= ' '; // extra space after condition
69
        return $this;
70
    }
71
72
    /**
73
     * join
74
     * @params join
75
     */
76
    public function join($join) {
77
        $this->_joins .= $join . ' ';
78
        return $this;
79
    }
80
81
    /**
82
     * limit
83
     * @params limit
84
     */
85
    public function limit($limit) {
86
        $this->_limit = 'LIMIT ' . $limit;
87
        return $this;
88
    }
89
90
    /**
91
     * group
92
     * @params group
93
     */
94
    public function group($group) {
95
        $this->_group = ' GROUP BY ' . $group;
96
        return $this;
97
    }
98
99
    /**
100
     * having
101
     * @params having
102
     */
103
    public function having($having) {
104
        $this->_having = ' HAVING ' . $having;
105
        return $this;
106
    }
107
108
    /**
109
     * orderBy
110
     * @params orderBy
111
     */
112
    public function orderBy($orderBy) {
113
        $this->_orderBy = ' ORDER BY ' . $orderBy;
114
        return $this;
115
    }
116
117
    /**
118
     * generar string con la consulta a partir de los métodos 
119
     * cargados
120
     */
121
    public function __toString() {
122
        $sql = 'SELECT ' . (empty($this->_columns) ? '*' : $this->_columns) . 
123
        	   ' ' . 'FROM ' . $this->_table;
124
125
        $check_for = [
126
            '_joins',
127
            '_conditions',
128
            '_group',
129
            '_having',
130
            '_limit',
131
            '_orderBy'
132
        ];
133
134
        if (!empty($this->_conditions)) {
135
            $this->_conditions = 'WHERE ' . $this->_conditions;
136
        }
137
138
        foreach ($check_for as $element) :
139
            if (!empty(trim($this->$element))) :
140
                $sql .= ' ' . $this->$element . ' ';
141
            endif;
142
143
        endforeach;
144
145
        return $sql;
146
    }        
147
148
    /**
149
     * clear
150
     */
151
    private function clear() {
152
        $clear_in = [
153
            '_table',
154
            '_joins',
155
            '_conditions',
156
            '_group',
157
            '_having',
158
            '_limit',
159
            '_orderBy'
160
        ];
161
        foreach ($clear_in as $elem) :
162
            $this->$elem = '';
163
        endforeach
164
        ;        
165
    }
166
167
    /**
168
     * destruct
169
     */
170
    public function __destruct() {
171
        $this->clear();
172
    }
173
174
}
175