Completed
Push — master ( 13b38a...f07239 )
by Joao
02:35
created

Query::top()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: jg
5
 * Date: 21/06/16
6
 * Time: 12:01
7
 */
8
9
namespace ByJG\MicroOrm;
10
11
12
class Query
13
{
14
    protected $fields = [];
15
    protected $table = "";
16
    protected $where = [];
17
    protected $groupBy = [];
18
    protected $orderBy = [];
19
    protected $join = [];
20
    
21
    protected $forUpdate = false;
22
23
    /**
24
     * Example:
25
     *   $query->fields(['name', 'price']);
26
     * 
27
     * @param array $fields
28
     * @return $this
29
     */
30 4
    public function fields(array $fields)
31
    {
32 4
        $this->fields = array_merge($this->fields, $fields);
33
        
34 4
        return $this;
35
    }
36
37
    /**
38
     * Example
39
     *    $query->table('product');
40
     * 
41
     * @param string $table
42
     * @return $this
43
     */
44 10
    public function table($table) 
45
    {
46 10
        $this->table = $table;
47
48 10
        return $this;
49
    }
50
51
    /**
52
     * Example:
53
     *    $query->join('sales', 'product.id = sales.id');
54
     * 
55
     * @param string $table
56
     * @param string $filter
57
     * @return $this
58
     */
59 1
    public function join($table, $filter)
60
    {
61 1
        $this->join[] = [ 'table'=>$table, 'filter'=>$filter, 'type' => 'INNER'];
62 1
        return $this;
63
    }
64
65
    /**
66
     * Example:
67
     *    $query->join('sales', 'product.id = sales.id');
68
     *
69
     * @param string $table
70
     * @param string $filter
71
     * @return $this
72
     */
73 1
    public function leftJoin($table, $filter)
74
    {
75 1
        $this->join[] = [ 'table'=>$table, 'filter'=>$filter, 'type' => 'LEFT'];
76 1
        return $this;
77
    }
78
79
    /**
80
     * Example:
81
     *    $query->filter('price > [[amount]]', [ 'amount' => 1000] );
82
     * 
83
     * @param string $filter
84
     * @param array $params
85
     * @return $this
86
     */
87 8
    public function where($filter, array $params = [])
88
    {
89 8
        $this->where[] = [ 'filter' => $filter, 'params' => $params  ];
90 8
        return $this;
91
    }
92
93
    /**
94
     * Example:
95
     *    $query->groupBy(['name']);
96
     * 
97
     * @param array $fields
98
     * @return $this
99
     */
100
    public function groupBy(array $fields)
101
    {
102
        $this->groupBy = array_merge($this->groupBy, $fields);
103
    
104
        return $this;
105
    }
106
107
    /**
108
     * Example:
109
     *     $query->orderBy(['price desc']);
110
     * 
111
     * @param array $fields
112
     * @return $this
113
     */
114 3
    public function orderBy(array $fields)
115
    {
116 3
        $this->orderBy = array_merge($this->orderBy, $fields);
117
118 3
        return $this;
119
    }
120
121
    public function forUpdate()
122
    {
123
        $this->forUpdate = true;
124
        
125
        return $this;
126
    }
127
    
128 10
    protected function getFields()
129
    {
130 10
        if (empty($this->fields)) {
131 8
            return ' * ';
132
        }
133
134 2
        return ' ' . implode(', ', $this->fields) . ' ';
135
    }
136
    
137 10
    protected function getJoin()
138
    {
139 10
        $join = $this->table;
140 10
        foreach ($this->join as $item) {
141 2
            $join .= ' ' . $item['type'] . ' JOIN ' . $item['table'] . ' ON ' . $item['filter'];
142 10
        }
143 10
        return $join;
144
    }
145
    
146 10
    protected function getWhere() 
147
    {
148 10
        $where = [];
149 10
        $params = [];
150
151 10
        foreach ($this->where as $item) {
152 8
            $where[] = $item['filter'];
153 8
            $params = array_merge($params, $item['params']);
154 10
        }
155
        
156 10
        if (empty($where)) {
157 2
            return null;
158
        }
159
        
160 8
        return [ implode(' AND ', $where), $params ];
161
    }
162
163
    /**
164
     * @return array
165
     */
166 10
    public function getSelect()
167
    {
168
        $sql = "SELECT " .
169 10
            $this->getFields() . 
170 10
            "FROM " . $this->getJoin();
171
        
172 10
        $where = $this->getWhere();
173 10
        $params = null;
174 10
        if (!is_null($where)) {
175 8
            $sql .= ' WHERE ' . $where[0];
176 8
            $params = $where[1];
177 8
        }
178
        
179 10
        if (!empty($this->groupBy)) {
180
            $sql .= ' GROUP BY ' . implode(', ', $this->groupBy);
181
        }
182
183 10
        if (!empty($this->orderBy)) {
184 3
            $sql .= ' ORDER BY ' . implode(', ', $this->orderBy);
185 3
        }
186
        
187 10
        if ($this->forUpdate) {
188
            $sql .= ' FOR UPDATE ';
189
        }
190
191 10
        return [ 'sql' => $sql, 'params' => $params ];
192
    }
193
194
    /**
195
     * @return string
196
     * @throws \Exception
197
     */
198 1
    public function getInsert()
199
    {
200 1
        if (empty($this->fields)) {
201
            throw new \Exception('You must specifiy the fields for insert');
202
        }
203
        
204
        $sql = 'INSERT INTO '
205 1
            . $this->table
206 1
            . '( ' . implode(', ', $this->fields) . ' ) '
207 1
            . ' values '
208 1
            . '( [[' . implode(']], [[', $this->fields) . ']] ) ';
209
        
210 1
        return $sql;
211
    }
212
213
    /**
214
     * @return array
215
     * @throws \Exception
216
     */
217 1
    public function getUpdate()
218
    {
219 1
        if (empty($this->fields)) {
220
            throw new \Exception('You must specifiy the fields for insert');
221
        }
222
        
223 1
        $fields = [];
224 1
        foreach ($this->fields as $field) {
225 1
            $fields[] = "$field = [[$field]] ";
226 1
        }
227
        
228 1
        $where = $this->getWhere();
229 1
        if (is_null($where)) {
230
            throw new \Exception('You must specifiy a where clause');
231
        }
232
233 1
        $sql = 'UPDATE ' . $this->table . ' SET '
234 1
            . implode(', ', $fields)
235 1
            . ' WHERE ' . $where[0];
236
237 1
        return [ 'sql' => $sql, 'params' => $where[1] ];
238
    }
239
240
    /**
241
     * @return array
242
     * @throws \Exception
243
     */
244 2
    public function getDelete()
245
    {
246 2
        $where = $this->getWhere();
247 2
        if (is_null($where)) {
248
            throw new \Exception('You must specifiy a where clause');
249
        }
250
251 2
        $sql = 'DELETE FROM ' . $this->table
252 2
            . ' WHERE ' . $where[0];
253
254 2
        return [ 'sql' => $sql, 'params' => $where[1] ];
255
    }
256
257
}
258