@@ -6,7 +6,7 @@ discard block |
||
6 | 6 | const EDIT_OPTIMISE = 4; |
7 | 7 | |
8 | 8 | private $table; |
9 | - private $options; |
|
9 | + private $options; |
|
10 | 10 | private $cache = []; |
11 | 11 | private $primaryKey; |
12 | 12 | private $fields = '*'; |
@@ -15,7 +15,7 @@ discard block |
||
15 | 15 | private $alterDb = false; |
16 | 16 | private $adapter; |
17 | 17 | private $crudBuilder; |
18 | - private $selectBuilder; |
|
18 | + private $selectBuilder; |
|
19 | 19 | |
20 | 20 | public function __construct($db, $table, $primaryKey = 'id', array $options = []) { |
21 | 21 | $this->options = new DatabaseOptions($db, $options); |
@@ -36,9 +36,9 @@ discard block |
||
36 | 36 | $this->optimizeColumns(); |
37 | 37 | } |
38 | 38 | |
39 | - private function optimizeColumns() { |
|
40 | - if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($this->table); |
|
41 | - } |
|
39 | + private function optimizeColumns() { |
|
40 | + if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($this->table); |
|
41 | + } |
|
42 | 42 | |
43 | 43 | public function getPrimaryKey() { |
44 | 44 | return $this->primaryKey; |
@@ -80,15 +80,15 @@ discard block |
||
80 | 80 | } |
81 | 81 | } |
82 | 82 | |
83 | - private function determineAggregateResult($result, $group, $field) { |
|
84 | - if ($group != null) { |
|
85 | - $ret = []; |
|
86 | - foreach ($result as $res) $ret[$res->$field] = $res->val; |
|
87 | - return $ret; |
|
88 | - } |
|
89 | - else if (isset($result[0])) return $result[0]->val; |
|
90 | - else return 0; |
|
91 | - } |
|
83 | + private function determineAggregateResult($result, $group, $field) { |
|
84 | + if ($group != null) { |
|
85 | + $ret = []; |
|
86 | + foreach ($result as $res) $ret[$res->$field] = $res->val; |
|
87 | + return $ret; |
|
88 | + } |
|
89 | + else if (isset($result[0])) return $result[0]->val; |
|
90 | + else return 0; |
|
91 | + } |
|
92 | 92 | |
93 | 93 | private function addIndex($args) { |
94 | 94 | if (self::EDIT_INDEX & $this->alterDb) $this->adapter->addIndex($this->table, $args); |
@@ -118,7 +118,7 @@ discard block |
||
118 | 118 | |
119 | 119 | public function deleteByField(array $fields, array $options = []) { |
120 | 120 | $query = $this->selectBuilder->createSql($fields, \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND); |
121 | - $query['sql'] = array_filter($query['sql']); |
|
121 | + $query['sql'] = array_filter($query['sql']); |
|
122 | 122 | $this->adapter->query($this->crudBuilder->delete($this->table, $query['sql'], $query['args'], $options['limit'], null, $options['order'])); |
123 | 123 | $this->addIndex(array_keys($query['args'])); |
124 | 124 | |
@@ -127,22 +127,22 @@ discard block |
||
127 | 127 | $this->resultCache = []; |
128 | 128 | } |
129 | 129 | |
130 | - private function getIfNew($data) { |
|
131 | - $new = false; |
|
132 | - foreach ($this->primaryKey as $k) { |
|
133 | - if (empty($data->$k)) { |
|
134 | - $data->$k = null; |
|
135 | - $new = true; |
|
136 | - } |
|
137 | - } |
|
138 | - return $new; |
|
139 | - } |
|
130 | + private function getIfNew($data) { |
|
131 | + $new = false; |
|
132 | + foreach ($this->primaryKey as $k) { |
|
133 | + if (empty($data->$k)) { |
|
134 | + $data->$k = null; |
|
135 | + $new = true; |
|
136 | + } |
|
137 | + } |
|
138 | + return $new; |
|
139 | + } |
|
140 | 140 | |
141 | 141 | public function save($data, $tryagain = true) { |
142 | - $new = $this->getIfNew($data); |
|
142 | + $new = $this->getIfNew($data); |
|
143 | 143 | |
144 | 144 | try { |
145 | - $result = $this->insert($this->table, $this->primaryKey, $data); |
|
145 | + $result = $this->insert($this->table, $this->primaryKey, $data); |
|
146 | 146 | |
147 | 147 | //If there was an error but PDO is silent, trigger the catch block anyway |
148 | 148 | if ($result->errorCode() !== '00000') throw new \Exception('Could not insert into ' . $this->table); |
@@ -160,26 +160,26 @@ discard block |
||
160 | 160 | $this->updateCache($data); |
161 | 161 | } |
162 | 162 | |
163 | - private function getTryAgain($tryagain) { |
|
164 | - return $tryagain && self::EDIT_STRUCTURE & $this->alterDb; |
|
165 | - } |
|
163 | + private function getTryAgain($tryagain) { |
|
164 | + return $tryagain && self::EDIT_STRUCTURE & $this->alterDb; |
|
165 | + } |
|
166 | 166 | |
167 | - private function updatePK($data, $new) { |
|
168 | - if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
169 | - } |
|
167 | + private function updatePK($data, $new) { |
|
168 | + if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
169 | + } |
|
170 | 170 | |
171 | - private function checkIfUpdateWorked($data) { |
|
172 | - $updateWhere = $this->crudBuilder->update($this->table, $this->primaryKey, $data); |
|
173 | - $matched = $this->findByField($updateWhere->getArgs()); |
|
171 | + private function checkIfUpdateWorked($data) { |
|
172 | + $updateWhere = $this->crudBuilder->update($this->table, $this->primaryKey, $data); |
|
173 | + $matched = $this->findByField($updateWhere->getArgs()); |
|
174 | 174 | |
175 | - if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
176 | - } |
|
175 | + if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
176 | + } |
|
177 | 177 | |
178 | - private function updateCache($data) { |
|
179 | - $pkValue = $data->{$this->primaryKey[0]}; |
|
178 | + private function updateCache($data) { |
|
179 | + $pkValue = $data->{$this->primaryKey[0]}; |
|
180 | 180 | if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
181 | 181 | else $this->cache[$pkValue] = $data; |
182 | - } |
|
182 | + } |
|
183 | 183 | |
184 | 184 | private function insert($table, array $primaryKey, $data) { |
185 | 185 | $error = 0; |
@@ -191,16 +191,16 @@ discard block |
||
191 | 191 | } |
192 | 192 | |
193 | 193 | if ($error || $result->errorCode() !== '00000') { |
194 | - $result = $this->tryUpdate($table, $primaryKey, $data); |
|
195 | - } |
|
194 | + $result = $this->tryUpdate($table, $primaryKey, $data); |
|
195 | + } |
|
196 | 196 | |
197 | 197 | return $result; |
198 | 198 | } |
199 | 199 | |
200 | - private function tryUpdate($table, array $primaryKey, $data) { |
|
201 | - $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data)); |
|
202 | - if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data); |
|
200 | + private function tryUpdate($table, array $primaryKey, $data) { |
|
201 | + $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data)); |
|
202 | + if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data); |
|
203 | 203 | |
204 | - return $result; |
|
205 | - } |
|
204 | + return $result; |
|
205 | + } |
|
206 | 206 | } |
@@ -37,7 +37,9 @@ discard block |
||
37 | 37 | } |
38 | 38 | |
39 | 39 | private function optimizeColumns() { |
40 | - if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($this->table); |
|
40 | + if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) { |
|
41 | + $this->adapter->optimiseColumns($this->table); |
|
42 | + } |
|
41 | 43 | } |
42 | 44 | |
43 | 45 | public function getPrimaryKey() { |
@@ -53,19 +55,23 @@ discard block |
||
53 | 55 | if (!isset($this->cache[$id])) { |
54 | 56 | try { |
55 | 57 | $result = $this->adapter->query($this->selectBuilder->select($this->table, [$this->getPrimaryKey()[0] . ' = :id'], [':id' => $id], ['limit' => 1])); |
56 | - } |
|
57 | - catch (\Exception $e) { |
|
58 | + } catch (\Exception $e) { |
|
58 | 59 | } |
59 | 60 | |
60 | - if (isset($result[0])) $this->cache[$id] = $result[0]; |
|
61 | - else return null; |
|
61 | + if (isset($result[0])) { |
|
62 | + $this->cache[$id] = $result[0]; |
|
63 | + } else { |
|
64 | + return null; |
|
65 | + } |
|
62 | 66 | } |
63 | 67 | return $this->cache[$id]; |
64 | 68 | } |
65 | 69 | |
66 | 70 | public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { |
67 | 71 | //Cannot count/sum/max multiple fields, pick the first one. This should only come into play when trying to count() a mapper with multiple primary keys |
68 | - if (is_array($field)) $field = $field[0]; |
|
72 | + if (is_array($field)) { |
|
73 | + $field = $field[0]; |
|
74 | + } |
|
69 | 75 | $query = $this->selectBuilder->createSql($criteria, \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND); |
70 | 76 | |
71 | 77 | try { |
@@ -74,8 +80,7 @@ discard block |
||
74 | 80 | $result = $this->adapter->query($this->selectBuilder->aggregate($this->table, $function, $field, $query['sql'], $query['args'], $group)); |
75 | 81 | |
76 | 82 | return $this->determineAggregateResult($result, $group, $field); |
77 | - } |
|
78 | - catch (\Exception $e) { |
|
83 | + } catch (\Exception $e) { |
|
79 | 84 | return $group ? [] : 0; |
80 | 85 | } |
81 | 86 | } |
@@ -83,15 +88,21 @@ discard block |
||
83 | 88 | private function determineAggregateResult($result, $group, $field) { |
84 | 89 | if ($group != null) { |
85 | 90 | $ret = []; |
86 | - foreach ($result as $res) $ret[$res->$field] = $res->val; |
|
91 | + foreach ($result as $res) { |
|
92 | + $ret[$res->$field] = $res->val; |
|
93 | + } |
|
87 | 94 | return $ret; |
95 | + } else if (isset($result[0])) { |
|
96 | + return $result[0]->val; |
|
97 | + } else { |
|
98 | + return 0; |
|
88 | 99 | } |
89 | - else if (isset($result[0])) return $result[0]->val; |
|
90 | - else return 0; |
|
91 | 100 | } |
92 | 101 | |
93 | 102 | private function addIndex($args) { |
94 | - if (self::EDIT_INDEX & $this->alterDb) $this->adapter->addIndex($this->table, $args); |
|
103 | + if (self::EDIT_INDEX & $this->alterDb) { |
|
104 | + $this->adapter->addIndex($this->table, $args); |
|
105 | + } |
|
95 | 106 | } |
96 | 107 | |
97 | 108 | public function findByField(array $fields, $options = []) { |
@@ -99,7 +110,9 @@ discard block |
||
99 | 110 | if (!isset($this->resultCache[$cacheId])) { |
100 | 111 | $query = $this->selectBuilder->createSql($fields, \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND); |
101 | 112 | |
102 | - if (!isset($options['order'])) $options['order'] = $this->defaultSort; |
|
113 | + if (!isset($options['order'])) { |
|
114 | + $options['order'] = $this->defaultSort; |
|
115 | + } |
|
103 | 116 | |
104 | 117 | $query['sql'] = array_filter($query['sql']); |
105 | 118 | |
@@ -107,8 +120,7 @@ discard block |
||
107 | 120 | $this->resultCache[$cacheId] = $this->adapter->query($this->selectBuilder->select($this->table, $query['sql'], $query['args'], $options)); |
108 | 121 | $this->addIndex(array_keys($query['args'])); |
109 | 122 | $this->addIndex(explode(',', $options['order'])); |
110 | - } |
|
111 | - catch (\Exception $e) { |
|
123 | + } catch (\Exception $e) { |
|
112 | 124 | $this->errors[] = $e; |
113 | 125 | $this->resultCache[$cacheId] = []; |
114 | 126 | } |
@@ -145,10 +157,13 @@ discard block |
||
145 | 157 | $result = $this->insert($this->table, $this->primaryKey, $data); |
146 | 158 | |
147 | 159 | //If there was an error but PDO is silent, trigger the catch block anyway |
148 | - if ($result->errorCode() !== '00000') throw new \Exception('Could not insert into ' . $this->table); |
|
149 | - } |
|
150 | - catch (\Exception $e) { |
|
151 | - if (!$this->getTryAgain($tryagain)) throw $e; |
|
160 | + if ($result->errorCode() !== '00000') { |
|
161 | + throw new \Exception('Could not insert into ' . $this->table); |
|
162 | + } |
|
163 | + } catch (\Exception $e) { |
|
164 | + if (!$this->getTryAgain($tryagain)) { |
|
165 | + throw $e; |
|
166 | + } |
|
152 | 167 | |
153 | 168 | $this->adapter->alterDatabase($this->table, $this->primaryKey, $data); |
154 | 169 | $this->save($data, false); |
@@ -165,28 +180,34 @@ discard block |
||
165 | 180 | } |
166 | 181 | |
167 | 182 | private function updatePK($data, $new) { |
168 | - if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
183 | + if ($new && count($this->primaryKey) == 1) { |
|
184 | + $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
185 | + } |
|
169 | 186 | } |
170 | 187 | |
171 | 188 | private function checkIfUpdateWorked($data) { |
172 | 189 | $updateWhere = $this->crudBuilder->update($this->table, $this->primaryKey, $data); |
173 | 190 | $matched = $this->findByField($updateWhere->getArgs()); |
174 | 191 | |
175 | - if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
192 | + if (count($matched) == 0) { |
|
193 | + throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
194 | + } |
|
176 | 195 | } |
177 | 196 | |
178 | 197 | private function updateCache($data) { |
179 | 198 | $pkValue = $data->{$this->primaryKey[0]}; |
180 | - if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
181 | - else $this->cache[$pkValue] = $data; |
|
199 | + if (isset($this->cache[$pkValue])) { |
|
200 | + $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
201 | + } else { |
|
202 | + $this->cache[$pkValue] = $data; |
|
203 | + } |
|
182 | 204 | } |
183 | 205 | |
184 | 206 | private function insert($table, array $primaryKey, $data) { |
185 | 207 | $error = 0; |
186 | 208 | try { |
187 | 209 | $result = $this->adapter->query($this->crudBuilder->insert($table, $data)); |
188 | - } |
|
189 | - catch (\Exception $e) { |
|
210 | + } catch (\Exception $e) { |
|
190 | 211 | $error = 1; |
191 | 212 | } |
192 | 213 | |
@@ -199,7 +220,9 @@ discard block |
||
199 | 220 | |
200 | 221 | private function tryUpdate($table, array $primaryKey, $data) { |
201 | 222 | $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data)); |
202 | - if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data); |
|
223 | + if ($result->rowCount() === 0) { |
|
224 | + $this->checkIfUpdateWorked($data); |
|
225 | + } |
|
203 | 226 | |
204 | 227 | return $result; |
205 | 228 | } |
@@ -2,112 +2,112 @@ |
||
2 | 2 | namespace Maphper\DataSource; |
3 | 3 | use Maphper\Maphper; |
4 | 4 | class Mock implements \Maphper\DataSource { |
5 | - private $data; |
|
6 | - private $id; |
|
5 | + private $data; |
|
6 | + private $id; |
|
7 | 7 | |
8 | - public function __construct(\ArrayObject $data, $id) { |
|
9 | - $this->data = $data; |
|
10 | - $this->id = is_array($id) ? $id : [$id]; |
|
11 | - } |
|
8 | + public function __construct(\ArrayObject $data, $id) { |
|
9 | + $this->data = $data; |
|
10 | + $this->id = is_array($id) ? $id : [$id]; |
|
11 | + } |
|
12 | 12 | |
13 | - public function getPrimaryKey() { |
|
14 | - return $this->id; |
|
15 | - } |
|
13 | + public function getPrimaryKey() { |
|
14 | + return $this->id; |
|
15 | + } |
|
16 | 16 | |
17 | - public function findById($id) { |
|
18 | - return isset($this->data[$id]) ? (array)$this->data[$id] : []; |
|
19 | - } |
|
17 | + public function findById($id) { |
|
18 | + return isset($this->data[$id]) ? (array)$this->data[$id] : []; |
|
19 | + } |
|
20 | 20 | |
21 | - public function findByField(array $fields, $options = []) { |
|
22 | - $array = iterator_to_array($this->data->getIterator()); |
|
23 | - $filteredArray = array_filter($array, $this->getSearchFieldFunction($fields, \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND)); |
|
24 | - // Need to reset indexes |
|
25 | - $filteredArray = array_values($filteredArray); |
|
26 | - if (isset($options['order'])) { |
|
27 | - list($columns, $order) = explode(' ', $options['order']); |
|
28 | - usort($filteredArray, $this->getOrderFunction($order, $columns)); |
|
29 | - } |
|
30 | - if (isset($options['offset'])) $filteredArray = array_slice($filteredArray, $options['offset']); |
|
31 | - if (isset($options['limit'])) $filteredArray = array_slice($filteredArray, 0, $options['limit']); |
|
32 | - return $filteredArray; |
|
33 | - } |
|
21 | + public function findByField(array $fields, $options = []) { |
|
22 | + $array = iterator_to_array($this->data->getIterator()); |
|
23 | + $filteredArray = array_filter($array, $this->getSearchFieldFunction($fields, \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND)); |
|
24 | + // Need to reset indexes |
|
25 | + $filteredArray = array_values($filteredArray); |
|
26 | + if (isset($options['order'])) { |
|
27 | + list($columns, $order) = explode(' ', $options['order']); |
|
28 | + usort($filteredArray, $this->getOrderFunction($order, $columns)); |
|
29 | + } |
|
30 | + if (isset($options['offset'])) $filteredArray = array_slice($filteredArray, $options['offset']); |
|
31 | + if (isset($options['limit'])) $filteredArray = array_slice($filteredArray, 0, $options['limit']); |
|
32 | + return $filteredArray; |
|
33 | + } |
|
34 | 34 | |
35 | - private function getSearchFieldFunction($fields, $mode) { |
|
36 | - return function ($data) use ($fields, $mode) { |
|
37 | - foreach ($fields as $key => $val) { |
|
38 | - $currentFieldResult = $this->getIfFieldMatches($key, $val, $data, $mode); |
|
35 | + private function getSearchFieldFunction($fields, $mode) { |
|
36 | + return function ($data) use ($fields, $mode) { |
|
37 | + foreach ($fields as $key => $val) { |
|
38 | + $currentFieldResult = $this->getIfFieldMatches($key, $val, $data, $mode); |
|
39 | 39 | |
40 | - if (Maphper::FIND_OR & $mode && $currentFieldResult === true) return true; |
|
41 | - else if (!(Maphper::FIND_OR & $mode) && $currentFieldResult === false) return false; |
|
42 | - } |
|
43 | - return !(Maphper::FIND_OR & $mode); |
|
44 | - }; |
|
45 | - } |
|
40 | + if (Maphper::FIND_OR & $mode && $currentFieldResult === true) return true; |
|
41 | + else if (!(Maphper::FIND_OR & $mode) && $currentFieldResult === false) return false; |
|
42 | + } |
|
43 | + return !(Maphper::FIND_OR & $mode); |
|
44 | + }; |
|
45 | + } |
|
46 | 46 | |
47 | - private function getIfFieldMatches($key, $val, $data, $mode) { |
|
48 | - if (is_numeric($key) && is_array($val)) { |
|
49 | - return $this->getSearchFieldFunction($val, $key)($data); |
|
50 | - } |
|
51 | - else if (!isset($data->$key)) return false; |
|
52 | - else if (!(Maphper::FIND_BETWEEN & $mode) && !is_numeric($key) && is_array($val)) |
|
53 | - return in_array($data->$key, $val); |
|
54 | - else |
|
55 | - return $this->processFilter($mode, $val, $data->$key); |
|
56 | - } |
|
47 | + private function getIfFieldMatches($key, $val, $data, $mode) { |
|
48 | + if (is_numeric($key) && is_array($val)) { |
|
49 | + return $this->getSearchFieldFunction($val, $key)($data); |
|
50 | + } |
|
51 | + else if (!isset($data->$key)) return false; |
|
52 | + else if (!(Maphper::FIND_BETWEEN & $mode) && !is_numeric($key) && is_array($val)) |
|
53 | + return in_array($data->$key, $val); |
|
54 | + else |
|
55 | + return $this->processFilter($mode, $val, $data->$key); |
|
56 | + } |
|
57 | 57 | |
58 | 58 | public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { |
59 | - return $function($this->findByField($criteria)); |
|
60 | - } |
|
59 | + return $function($this->findByField($criteria)); |
|
60 | + } |
|
61 | 61 | |
62 | 62 | public function deleteById($id) { |
63 | - unset($this->data[$id]); |
|
64 | - } |
|
63 | + unset($this->data[$id]); |
|
64 | + } |
|
65 | 65 | |
66 | 66 | public function deleteByField(array $fields, array $options) { |
67 | - foreach ($this->findByField($fields, $options) as $val) unset($this->data[$val->{$this->id[0]}]); |
|
68 | - } |
|
67 | + foreach ($this->findByField($fields, $options) as $val) unset($this->data[$val->{$this->id[0]}]); |
|
68 | + } |
|
69 | 69 | |
70 | - public function save($data) { |
|
71 | - if (isset($data->{$this->id[0]})) { |
|
72 | - $id = $data->{$this->id[0]}; |
|
73 | - } |
|
74 | - else { |
|
75 | - $id = count($this->data); |
|
76 | - $data->{$this->id[0]} = $id; |
|
77 | - } |
|
70 | + public function save($data) { |
|
71 | + if (isset($data->{$this->id[0]})) { |
|
72 | + $id = $data->{$this->id[0]}; |
|
73 | + } |
|
74 | + else { |
|
75 | + $id = count($this->data); |
|
76 | + $data->{$this->id[0]} = $id; |
|
77 | + } |
|
78 | 78 | |
79 | - $this->data[$id] = (object)array_merge($this->findById($id), (array)$data); |
|
80 | - } |
|
79 | + $this->data[$id] = (object)array_merge($this->findById($id), (array)$data); |
|
80 | + } |
|
81 | 81 | |
82 | - public function getErrors() { |
|
83 | - return []; |
|
84 | - } |
|
82 | + public function getErrors() { |
|
83 | + return []; |
|
84 | + } |
|
85 | 85 | |
86 | - private function getOrderFunction($order, $columns) { |
|
87 | - return function($a, $b) use ($order, $columns) { |
|
88 | - foreach (explode(',', $columns) as $column) { |
|
89 | - $aColumn = $a->$column; |
|
90 | - $bColumn = $b->$column; |
|
91 | - if ($aColumn === $bColumn) { |
|
92 | - $sortVal = 0; |
|
93 | - continue; |
|
94 | - } |
|
95 | - else $sortVal = ($aColumn < $bColumn) ? -1 : 1; |
|
96 | - break; |
|
97 | - } |
|
98 | - if ($order === 'desc') return -$sortVal; |
|
99 | - else return $sortVal; |
|
100 | - }; |
|
101 | - } |
|
86 | + private function getOrderFunction($order, $columns) { |
|
87 | + return function($a, $b) use ($order, $columns) { |
|
88 | + foreach (explode(',', $columns) as $column) { |
|
89 | + $aColumn = $a->$column; |
|
90 | + $bColumn = $b->$column; |
|
91 | + if ($aColumn === $bColumn) { |
|
92 | + $sortVal = 0; |
|
93 | + continue; |
|
94 | + } |
|
95 | + else $sortVal = ($aColumn < $bColumn) ? -1 : 1; |
|
96 | + break; |
|
97 | + } |
|
98 | + if ($order === 'desc') return -$sortVal; |
|
99 | + else return $sortVal; |
|
100 | + }; |
|
101 | + } |
|
102 | 102 | |
103 | - private function processFilter($mode, $expected, $actual) { |
|
104 | - if (Maphper::FIND_NOT & $mode) return $expected != $actual; |
|
105 | - else if (Maphper::FIND_GREATER & $mode && Maphper::FIND_EXACT & $mode) return $expected <= $actual; |
|
106 | - else if (Maphper::FIND_LESS & $mode && Maphper::FIND_EXACT & $mode) return $expected >= $actual; |
|
107 | - else if (Maphper::FIND_GREATER & $mode) return $expected < $actual; |
|
108 | - else if (Maphper::FIND_LESS & $mode) return $expected > $actual; |
|
109 | - else if (Maphper::FIND_BETWEEN & $mode) return $expected[0] <= $actual && $actual <= $expected[1]; |
|
110 | - else if (Maphper::FIND_NOCASE & $mode) return strtolower($expected) == strtolower($actual); |
|
111 | - return $expected == $actual; |
|
112 | - } |
|
103 | + private function processFilter($mode, $expected, $actual) { |
|
104 | + if (Maphper::FIND_NOT & $mode) return $expected != $actual; |
|
105 | + else if (Maphper::FIND_GREATER & $mode && Maphper::FIND_EXACT & $mode) return $expected <= $actual; |
|
106 | + else if (Maphper::FIND_LESS & $mode && Maphper::FIND_EXACT & $mode) return $expected >= $actual; |
|
107 | + else if (Maphper::FIND_GREATER & $mode) return $expected < $actual; |
|
108 | + else if (Maphper::FIND_LESS & $mode) return $expected > $actual; |
|
109 | + else if (Maphper::FIND_BETWEEN & $mode) return $expected[0] <= $actual && $actual <= $expected[1]; |
|
110 | + else if (Maphper::FIND_NOCASE & $mode) return strtolower($expected) == strtolower($actual); |
|
111 | + return $expected == $actual; |
|
112 | + } |
|
113 | 113 | } |
@@ -27,8 +27,12 @@ discard block |
||
27 | 27 | list($columns, $order) = explode(' ', $options['order']); |
28 | 28 | usort($filteredArray, $this->getOrderFunction($order, $columns)); |
29 | 29 | } |
30 | - if (isset($options['offset'])) $filteredArray = array_slice($filteredArray, $options['offset']); |
|
31 | - if (isset($options['limit'])) $filteredArray = array_slice($filteredArray, 0, $options['limit']); |
|
30 | + if (isset($options['offset'])) { |
|
31 | + $filteredArray = array_slice($filteredArray, $options['offset']); |
|
32 | + } |
|
33 | + if (isset($options['limit'])) { |
|
34 | + $filteredArray = array_slice($filteredArray, 0, $options['limit']); |
|
35 | + } |
|
32 | 36 | return $filteredArray; |
33 | 37 | } |
34 | 38 | |
@@ -37,8 +41,11 @@ discard block |
||
37 | 41 | foreach ($fields as $key => $val) { |
38 | 42 | $currentFieldResult = $this->getIfFieldMatches($key, $val, $data, $mode); |
39 | 43 | |
40 | - if (Maphper::FIND_OR & $mode && $currentFieldResult === true) return true; |
|
41 | - else if (!(Maphper::FIND_OR & $mode) && $currentFieldResult === false) return false; |
|
44 | + if (Maphper::FIND_OR & $mode && $currentFieldResult === true) { |
|
45 | + return true; |
|
46 | + } else if (!(Maphper::FIND_OR & $mode) && $currentFieldResult === false) { |
|
47 | + return false; |
|
48 | + } |
|
42 | 49 | } |
43 | 50 | return !(Maphper::FIND_OR & $mode); |
44 | 51 | }; |
@@ -47,12 +54,13 @@ discard block |
||
47 | 54 | private function getIfFieldMatches($key, $val, $data, $mode) { |
48 | 55 | if (is_numeric($key) && is_array($val)) { |
49 | 56 | return $this->getSearchFieldFunction($val, $key)($data); |
57 | + } else if (!isset($data->$key)) { |
|
58 | + return false; |
|
59 | + } else if (!(Maphper::FIND_BETWEEN & $mode) && !is_numeric($key) && is_array($val)) { |
|
60 | + return in_array($data->$key, $val); |
|
61 | + } else { |
|
62 | + return $this->processFilter($mode, $val, $data->$key); |
|
50 | 63 | } |
51 | - else if (!isset($data->$key)) return false; |
|
52 | - else if (!(Maphper::FIND_BETWEEN & $mode) && !is_numeric($key) && is_array($val)) |
|
53 | - return in_array($data->$key, $val); |
|
54 | - else |
|
55 | - return $this->processFilter($mode, $val, $data->$key); |
|
56 | 64 | } |
57 | 65 | |
58 | 66 | public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { |
@@ -64,14 +72,15 @@ discard block |
||
64 | 72 | } |
65 | 73 | |
66 | 74 | public function deleteByField(array $fields, array $options) { |
67 | - foreach ($this->findByField($fields, $options) as $val) unset($this->data[$val->{$this->id[0]}]); |
|
75 | + foreach ($this->findByField($fields, $options) as $val) { |
|
76 | + unset($this->data[$val->{$this->id[0]}]); |
|
77 | + } |
|
68 | 78 | } |
69 | 79 | |
70 | 80 | public function save($data) { |
71 | 81 | if (isset($data->{$this->id[0]})) { |
72 | 82 | $id = $data->{$this->id[0]}; |
73 | - } |
|
74 | - else { |
|
83 | + } else { |
|
75 | 84 | $id = count($this->data); |
76 | 85 | $data->{$this->id[0]} = $id; |
77 | 86 | } |
@@ -91,23 +100,35 @@ discard block |
||
91 | 100 | if ($aColumn === $bColumn) { |
92 | 101 | $sortVal = 0; |
93 | 102 | continue; |
103 | + } else { |
|
104 | + $sortVal = ($aColumn < $bColumn) ? -1 : 1; |
|
94 | 105 | } |
95 | - else $sortVal = ($aColumn < $bColumn) ? -1 : 1; |
|
96 | 106 | break; |
97 | 107 | } |
98 | - if ($order === 'desc') return -$sortVal; |
|
99 | - else return $sortVal; |
|
108 | + if ($order === 'desc') { |
|
109 | + return -$sortVal; |
|
110 | + } else { |
|
111 | + return $sortVal; |
|
112 | + } |
|
100 | 113 | }; |
101 | 114 | } |
102 | 115 | |
103 | 116 | private function processFilter($mode, $expected, $actual) { |
104 | - if (Maphper::FIND_NOT & $mode) return $expected != $actual; |
|
105 | - else if (Maphper::FIND_GREATER & $mode && Maphper::FIND_EXACT & $mode) return $expected <= $actual; |
|
106 | - else if (Maphper::FIND_LESS & $mode && Maphper::FIND_EXACT & $mode) return $expected >= $actual; |
|
107 | - else if (Maphper::FIND_GREATER & $mode) return $expected < $actual; |
|
108 | - else if (Maphper::FIND_LESS & $mode) return $expected > $actual; |
|
109 | - else if (Maphper::FIND_BETWEEN & $mode) return $expected[0] <= $actual && $actual <= $expected[1]; |
|
110 | - else if (Maphper::FIND_NOCASE & $mode) return strtolower($expected) == strtolower($actual); |
|
117 | + if (Maphper::FIND_NOT & $mode) { |
|
118 | + return $expected != $actual; |
|
119 | + } else if (Maphper::FIND_GREATER & $mode && Maphper::FIND_EXACT & $mode) { |
|
120 | + return $expected <= $actual; |
|
121 | + } else if (Maphper::FIND_LESS & $mode && Maphper::FIND_EXACT & $mode) { |
|
122 | + return $expected >= $actual; |
|
123 | + } else if (Maphper::FIND_GREATER & $mode) { |
|
124 | + return $expected < $actual; |
|
125 | + } else if (Maphper::FIND_LESS & $mode) { |
|
126 | + return $expected > $actual; |
|
127 | + } else if (Maphper::FIND_BETWEEN & $mode) { |
|
128 | + return $expected[0] <= $actual && $actual <= $expected[1]; |
|
129 | + } else if (Maphper::FIND_NOCASE & $mode) { |
|
130 | + return strtolower($expected) == strtolower($actual); |
|
131 | + } |
|
111 | 132 | return $expected == $actual; |
112 | 133 | } |
113 | 134 | } |
@@ -8,7 +8,7 @@ |
||
8 | 8 | public function delete($table, array $criteria, $args, $limit = null, $offset = null, $order = null) { |
9 | 9 | $limit = $limit ? ' LIMIT ' . $limit : ''; |
10 | 10 | $offset = $offset ? ' OFFSET ' . $offset : ''; |
11 | - $order = $order ? ' ORDER BY ' . $order : ''; |
|
11 | + $order = $order ? ' ORDER BY ' . $order : ''; |
|
12 | 12 | return new Query('DELETE FROM ' . $table . ' WHERE ' . (!empty($criteria) ? implode(' AND ', $criteria) : '1 = 1 ') . $order . $limit . $offset, $args); |
13 | 13 | } |
14 | 14 |