1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ntentan\nibii; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Description of QueryParameters |
7
|
|
|
* |
8
|
|
|
* @author ekow |
9
|
|
|
*/ |
10
|
|
|
class QueryParameters { |
11
|
|
|
|
12
|
|
|
private $whereClause; |
13
|
|
|
private $and = ''; |
14
|
|
|
private $boundData = []; |
15
|
|
|
private $preparedBoundData = false; |
16
|
|
|
private $boundArrays = []; |
17
|
|
|
private $fields = []; |
18
|
|
|
private $table; |
19
|
|
|
private $firstOnly = false; |
20
|
|
|
private $eagerLoad = []; |
21
|
|
|
private $limit; |
22
|
|
|
private $offset; |
23
|
|
|
private $sorts = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* |
27
|
|
|
* @param string $table The name of the table |
28
|
|
|
*/ |
29
|
1 |
|
public function __construct($table = null) { |
30
|
1 |
|
$this->table = $table; |
31
|
1 |
|
} |
32
|
|
|
|
33
|
1 |
|
public function getFields() { |
34
|
1 |
|
$fields = '*'; |
35
|
|
|
|
36
|
1 |
|
if (count($this->fields) > 0) { |
37
|
|
|
$fields = implode(', ', $this->fields); |
38
|
|
|
} |
39
|
|
|
|
40
|
1 |
|
return $fields; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getEagerLoad() { |
44
|
|
|
return $this->eagerLoad; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function setFields($fields) { |
48
|
|
|
$this->fields = $fields; |
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
public function getTable() { |
53
|
1 |
|
return $this->table; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function setTable($table) { |
57
|
|
|
$this->table = $table; |
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
public function getLimit() { |
|
|
|
|
62
|
1 |
|
return $this->limit > 0 ? " LIMIT {$this->limit}" : null; |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
public function getOffset() { |
|
|
|
|
66
|
1 |
|
return $this->offset > 0 ? " OFFSET {$this->offset}" : null; |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
public function getWhereClause() { |
|
|
|
|
70
|
1 |
|
if($this->whereClause) { |
71
|
|
|
foreach($this->boundArrays as $boundArray) { |
72
|
|
|
$where = ""; |
73
|
|
|
$comma = ""; |
74
|
|
|
for($i = 0; $i < count($this->boundData[$boundArray]); $i++) { |
|
|
|
|
75
|
|
|
$where .= "{$comma}:{$boundArray}_{$i}"; |
76
|
|
|
$comma = ', '; |
77
|
|
|
} |
78
|
|
|
$this->whereClause = str_replace("%{$boundArray}%", $where, $this->whereClause); |
79
|
|
|
} |
80
|
|
|
} |
81
|
1 |
|
return $this->whereClause ? " WHERE {$this->whereClause}" : ''; |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
public function getBoundData() { |
85
|
1 |
|
if($this->preparedBoundData === false) { |
86
|
1 |
|
$this->preparedBoundData = []; |
|
|
|
|
87
|
1 |
|
foreach($this->boundData as $key => $value) { |
88
|
|
|
if(in_array($key, $this->boundArrays)) { |
89
|
|
|
foreach($value as $i => $v) { |
90
|
|
|
$this->preparedBoundData["{$key}_{$i}"] = $v; |
91
|
|
|
} |
92
|
|
|
} else { |
93
|
|
|
$this->preparedBoundData[$key] = $value; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
1 |
|
return $this->preparedBoundData; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function setBoundData($key, $value) { |
101
|
|
|
if(isset($this->boundData[$key])){ |
102
|
|
|
$isArray = is_array($value); |
103
|
|
|
$boundArray = in_array($key, $this->boundArrays); |
104
|
|
|
if($isArray && !$boundArray) { |
105
|
|
|
throw new NibiiException("{$key} cannot be bound to an array"); |
106
|
|
|
} else if (!$isArray && $boundArray) { |
107
|
|
|
throw new NibiiException("{$key} must be bound to an array"); |
108
|
|
|
} |
109
|
|
|
$this->boundData[$key] = $value; |
110
|
|
|
$this->preparedBoundData = false; |
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
throw new NibiiException("{$key} has not been bound to the current query"); |
114
|
|
|
} |
115
|
|
|
|
116
|
1 |
|
public function getSorts() { |
117
|
1 |
|
return count($this->sorts) ? " ORDER BY " . implode(", ", $this->sorts) : null; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function addFilter($field, $values = null) { |
121
|
|
|
$this->whereClause .= $this->and; |
122
|
|
|
|
123
|
|
|
if (is_array($values)) { |
124
|
|
|
$this->whereClause .= "{$field} IN (%{$field}%)"; |
125
|
|
|
$this->boundArrays[] = $field; |
126
|
|
|
$this->boundData[$field] = $values; |
127
|
|
|
} else { |
128
|
|
|
if ($values === null) { |
129
|
|
|
$this->whereClause .= "{$field} is NULL"; |
130
|
|
|
} else { |
131
|
|
|
$this->whereClause .= "{$field} = :$field"; |
132
|
|
|
$this->boundData[$field] = $values; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
$this->and = ' AND '; |
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function setFilter($filter, $values) { |
140
|
|
|
$filterCompiler = new FilterCompiler(); |
141
|
|
|
$compiledFilter = $filterCompiler->compile($filter); |
142
|
|
|
$compiledValues = $filterCompiler->rewriteBoundData($values); |
143
|
|
|
$this->whereClause .= "{$this->and}$compiledFilter"; |
144
|
|
|
$this->boundData += $compiledValues; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param boolean $firstOnly |
149
|
|
|
*/ |
150
|
|
|
public function setFirstOnly($firstOnly) { |
151
|
|
|
$this->firstOnly = $firstOnly; |
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
1 |
|
public function getFirstOnly() { |
156
|
1 |
|
return $this->firstOnly; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function setLimit($numItems) { |
160
|
|
|
$this->limit = $numItems; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function setOffset($offset) { |
164
|
|
|
$this->offset = $offset; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param string $field |
169
|
|
|
*/ |
170
|
|
|
public function addSort($field, $direction = 'ASC') { |
171
|
|
|
$this->sorts[] = "$field $direction"; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
} |
175
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.