Select::setParam()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace BWC\Share\Data;
4
5
use Doctrine\DBAL\Connection;
6
7
class Select
8
{
9
10
    /** @var string */
11
    private $_tableName;
12
13
    /** @var Connection */
14
    private $_con;
15
16
    /** @var array */
17
    private $_select;
18
19
    /** @var string|null */
20
    private $_alias;
21
22
    /** @var array */
23
    private $_join = array();
24
25
    /** @var array */
26
    private $_where = array();
27
28
    private $_sort = array();
29
30
    private $_pageNum = null;
31
    private $_pageSize = null;
32
33
    private $_params = array();
34
35
36
    function __construct($tableName, Connection $connection, $select = null, $alias = null) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
37
        $this->_tableName = $tableName;
38
        $this->_con = $connection;
39
        $this->_select = $select ? (is_array($select) ? $select : array($select)) : array('*');
40
        $this->_alias = $alias;
41
    }
42
43
44
    /**
45
     * @return null|string
46
     */
47
    function getAlias() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
48
        return $this->_alias;
49
    }
50
51
52
    /**
53
     * @param array $select
54
     * @param string $prefix
55
     * @return Select
56
     */
57
    function addSelect(array $select, $prefix = '') {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
58
        foreach ($select as $s) {
59
            $this->_select[] = "{$prefix}.{$s} as {$prefix}_{$s}";
60
        }
61
        return $this;
62
    }
63
64
65
    function reset() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
66
        $this->_join = array();
67
        $this->_where = array();
68
        $this->_sort = array();
69
        $this->_pageNum = null;
70
        $this->_pageSize = null;
71
        $this->_params = array();
72
    }
73
74
    /**
75
     * @param $table
76
     * @param $on
77
     * @param $alias
78
     * @return Select
79
     */
80
    function join($table, $on, $alias) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
81
        $this->_join[] = new JoinChunk('', $table, $on, $alias);
82
        return $this;
83
    }
84
85
    /**
86
     * @param $table
87
     * @param $on
88
     * @param $alias
89
     * @return Select
90
     */
91
    function leftJoin($table, $on, $alias) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
92
        $this->_join[] = new JoinChunk('LEFT', $table, $on, $alias);
93
        return $this;
94
    }
95
96
    /**
97
     * @param array|string $key
98
     * @param null|mixed $value
99
     * @param null|string $operator
100
     * @return Select
101
     */
102
    function where($key, $value = null, $operator = null) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
103
        if (is_array($key)) {
104
            foreach ($key as $k=>$v) {
105
                if ($this->_alias && strpos($k, '.')===false) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->_alias of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
106
                    $k = $this->_alias.'.'.$k;
107
                }
108
                $this->_where[] = new WhereChunk($k, $v, $operator);
109
            }
110
        } else {
111
            $this->_where[] = new WhereChunk($key, $value, $operator);
112
        }
113
        return $this;
114
    }
115
116
117
    /**
118
     * @param $string
119
     * @return Select
120
     */
121
    function whereString($string) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
122
        $this->_where[] = new WhereStringChunk($string);
123
        return $this;
124
    }
125
126
    /**
127
     * @param array|string $column
128
     * @param null|string $dir
129
     * @return Select
130
     */
131
    function sort($column, $dir = null) {
0 ignored issues
show
Unused Code introduced by
The parameter $dir is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
132
        if (is_array($column)) {
133
            foreach ($column as $k=>$v) {
134
                $this->_sort[$k] = $this->getSortDir($v);
135
            }
136
        } else {
137
            $this->_sort[$column] = '';
138
        }
139
        return $this;
140
    }
141
142
    private function getSortDir($dir) {
143
        if (is_string($dir)) {
144
            $dir = strtoupper($dir);
145
            if ($dir != 'ASC' && $dir != 'DESC') {
146
                $dir = $dir ? 'ASC' : 'DESC';
147
            }
148
        } else {
149
            $dir = $dir ? 'ASC' : 'DESC';
150
        }
151
        return $dir;
152
    }
153
154
    /**
155
     * @param $pageNum int
156
     * @param $pageSize int
157
     * @return Select
158
     */
159
    function paging($pageNum, $pageSize) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
160
        $this->_pageNum = intval($pageNum);
161
        $this->_pageSize = intval($pageSize);
162
        return $this;
163
    }
164
165
166
    /**
167
     * @return string|int
168
     */
169
    function getOne() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
170
        list($sql, $params) = $this->build();
171
        $result = $this->_con->fetchColumn($sql, $params);
172
        return $result;
173
    }
174
175
176
    /**
177
     * @return array
178
     */
179
    function getRow() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
180
        list($sql, $params) = $this->build();
181
        $result = $this->_con->fetchAssoc($sql, $params);
182
        return $result;
183
    }
184
185
186
    /**
187
     * @return array
188
     */
189
    function getAll() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
190
        list($sql, $params) = $this->build();
191
        $result = $this->_con->fetchAll($sql, $params);
192
        return $result;
193
    }
194
195
196
    /**
197
     * @return PagedResult
198
     */
199
    function pagedResult() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
200
        $result = new PagedResult();
201
        $result->data = $this->getAll();
202
        $result->totalRows = $this->getFoundRows();
203
        return $result;
204
    }
205
206
207
    /**
208
     * @return int
209
     */
210
    function getFoundRows() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
211
        return $this->_con->fetchColumn('SELECT FOUND_ROWS()');
212
    }
213
214
    /**
215
     * @param string $name
216
     * @param mixed $value
217
     * @return Select
218
     */
219
    function setParam($name, $value) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
220
        $this->_params[$name] = $value;
221
        return $this;
222
    }
223
224
225
226
    private function build() {
227
        $params = array();
228
        $sqlSelect = implode(', ', $this->_select);
229
        $sql = "SELECT SQL_CALC_FOUND_ROWS $sqlSelect \nFROM `{$this->_tableName}` ";
230
        if (!empty($this->_alias)) {
231
            $sql .= ' '.$this->_alias;
232
        }
233
        if (!empty($this->_join)) {
234
            $sql .= "\n";
235
            /** @var $chunk JoinChunk */
236
            foreach ($this->_join as $chunk) {
237
                $sql .= $chunk->build();
238
            }
239
        }
240
        if (!empty($this->_where)) {
241
            $arr = array();
242
            /** @var $chunk WhereChunk */
243
            foreach ($this->_where as $chunk) {
244
                list($wSQL, $wParams) = $chunk->build();
245
                $arr[] = $wSQL;
246
                foreach ($wParams as $k=>$v) {
247
                    $params[$k] = $v;
248
                }
249
            }
250
            $sql .= " WHERE ".implode(' AND ', $arr);
251
        }
252
        if (!empty($this->_sort)) {
253
            $arr = array();
254
            foreach ($this->_sort as $k=>$v) {
255
                if ($k) {
256
                    if ($v) {
257
                        $arr[] = "{$k} {$v}";
258
                    } else {
259
                        $arr[] = "$k";
260
                    }
261
                }
262
            }
263
            if (!empty($arr)) {
264
                $sql .= " ORDER BY ";
265
                $sql .= implode(', ', $arr);
266
            }
267
        }
268
        if ($this->_pageSize) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->_pageSize of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
269
            if ($this->_pageNum) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->_pageNum of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
270
                $from = ($this->_pageNum - 1) * $this->_pageSize;
271
                $sql .= " LIMIT {$from}, {$this->_pageSize} ";
272
            } else {
273
                $sql .= " LIMIT {$this->_pageSize} ";
274
            }
275
        }
276
        $params = array_merge($params, $this->_params);
277
        //print $sql;
278
        return array($sql, $params);
279
    }
280
    
281
}
282