@@ -2,25 +2,25 @@ |
||
2 | 2 | namespace Maphper\DataSource; |
3 | 3 | |
4 | 4 | class DatabaseModify { |
5 | - private $adapter; |
|
6 | - private $alterDb; |
|
7 | - private $table; |
|
5 | + private $adapter; |
|
6 | + private $alterDb; |
|
7 | + private $table; |
|
8 | 8 | |
9 | - public function __construct(DatabaseAdapter $adapter, $alterDb, $table) { |
|
10 | - $this->adapter = $adapter; |
|
11 | - $this->alterDb = $alterDb; |
|
12 | - $this->table = $table; |
|
13 | - } |
|
9 | + public function __construct(DatabaseAdapter $adapter, $alterDb, $table) { |
|
10 | + $this->adapter = $adapter; |
|
11 | + $this->alterDb = $alterDb; |
|
12 | + $this->table = $table; |
|
13 | + } |
|
14 | 14 | |
15 | - public function addIndex($args) { |
|
15 | + public function addIndex($args) { |
|
16 | 16 | if (Database::EDIT_INDEX & $this->alterDb) $this->adapter->addIndex($this->table, $args); |
17 | 17 | } |
18 | 18 | |
19 | - public function optimizeColumns() { |
|
20 | - if (Database::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($this->table); |
|
21 | - } |
|
19 | + public function optimizeColumns() { |
|
20 | + if (Database::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($this->table); |
|
21 | + } |
|
22 | 22 | |
23 | - public function getTryInsertAgain($tryagain) { |
|
24 | - return $tryagain && Database::EDIT_STRUCTURE & $this->alterDb; |
|
25 | - } |
|
23 | + public function getTryInsertAgain($tryagain) { |
|
24 | + return $tryagain && Database::EDIT_STRUCTURE & $this->alterDb; |
|
25 | + } |
|
26 | 26 | } |
@@ -2,31 +2,31 @@ discard block |
||
2 | 2 | namespace Maphper\DataSource; |
3 | 3 | |
4 | 4 | class DatabaseSelect { |
5 | - private $resultCache = []; |
|
6 | - private $idCache = []; |
|
7 | - private $selectBuilder; |
|
8 | - private $whereBuilder; |
|
9 | - private $adapter; |
|
10 | - private $databaseModify; |
|
11 | - private $defaultSort; |
|
12 | - private $table; |
|
13 | - |
|
14 | - public function __construct(DatabaseAdapter $adapter, DatabaseModify $databaseModify, $table, $defaultSort) { |
|
15 | - $this->adapter = $adapter; |
|
16 | - $this->databaseModify = $databaseModify; |
|
17 | - $this->selectBuilder = new \Maphper\Lib\SelectBuilder(); |
|
18 | - $this->whereBuilder = new \Maphper\Lib\Sql\WhereBuilder(); |
|
19 | - $this->defaultSort = $defaultSort; |
|
20 | - $this->table = $table; |
|
21 | - } |
|
22 | - |
|
23 | - public function findById($id, $pk) { |
|
5 | + private $resultCache = []; |
|
6 | + private $idCache = []; |
|
7 | + private $selectBuilder; |
|
8 | + private $whereBuilder; |
|
9 | + private $adapter; |
|
10 | + private $databaseModify; |
|
11 | + private $defaultSort; |
|
12 | + private $table; |
|
13 | + |
|
14 | + public function __construct(DatabaseAdapter $adapter, DatabaseModify $databaseModify, $table, $defaultSort) { |
|
15 | + $this->adapter = $adapter; |
|
16 | + $this->databaseModify = $databaseModify; |
|
17 | + $this->selectBuilder = new \Maphper\Lib\SelectBuilder(); |
|
18 | + $this->whereBuilder = new \Maphper\Lib\Sql\WhereBuilder(); |
|
19 | + $this->defaultSort = $defaultSort; |
|
20 | + $this->table = $table; |
|
21 | + } |
|
22 | + |
|
23 | + public function findById($id, $pk) { |
|
24 | 24 | if (!isset($this->idCache[$id])) { |
25 | 25 | try { |
26 | 26 | $result = $this->selectQuery($this->selectBuilder->select($this->table, $pk . ' = :id', [':id' => $id], ['limit' => 1])); |
27 | 27 | } |
28 | 28 | catch (\Exception $e) { |
29 | - // Don't issue an error if it cannot be found since we return null |
|
29 | + // Don't issue an error if it cannot be found since we return null |
|
30 | 30 | } |
31 | 31 | |
32 | 32 | if (isset($result[0])) $this->idCache[$id] = $result[0]; |
@@ -35,7 +35,7 @@ discard block |
||
35 | 35 | return $this->idCache[$id]; |
36 | 36 | } |
37 | 37 | |
38 | - public function findByField(array $fields, $options = []) { |
|
38 | + public function findByField(array $fields, $options = []) { |
|
39 | 39 | $cacheId = md5(serialize(func_get_args())); |
40 | 40 | if (!isset($this->resultCache[$cacheId])) { |
41 | 41 | $query = $this->whereBuilder->createSql($fields); |
@@ -55,7 +55,7 @@ discard block |
||
55 | 55 | return $this->resultCache[$cacheId]; |
56 | 56 | } |
57 | 57 | |
58 | - public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { |
|
58 | + public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { |
|
59 | 59 | //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 |
60 | 60 | if (is_array($field)) $field = $field[0]; |
61 | 61 | $query = $this->whereBuilder->createSql($criteria); |
@@ -72,34 +72,34 @@ discard block |
||
72 | 72 | } |
73 | 73 | } |
74 | 74 | |
75 | - private function determineAggregateResult($result, $group, $field) { |
|
76 | - if ($group != null) { |
|
77 | - $ret = []; |
|
78 | - foreach ($result as $res) $ret[$res->$field] = $res->val; |
|
79 | - return $ret; |
|
80 | - } |
|
81 | - else if (isset($result[0])) return $result[0]->val; |
|
82 | - else return 0; |
|
83 | - } |
|
84 | - |
|
85 | - private function selectQuery(\Maphper\Lib\Query $query) { |
|
86 | - return $this->adapter->query($query)->fetchAll(\PDO::FETCH_OBJ); |
|
87 | - } |
|
88 | - |
|
89 | - public function clearResultCache() { |
|
90 | - $this->resultCache = []; |
|
91 | - } |
|
92 | - |
|
93 | - public function clearIDCache() { |
|
94 | - $this->idCache = []; |
|
95 | - } |
|
96 | - |
|
97 | - public function updateCache($data, $pkValue) { |
|
75 | + private function determineAggregateResult($result, $group, $field) { |
|
76 | + if ($group != null) { |
|
77 | + $ret = []; |
|
78 | + foreach ($result as $res) $ret[$res->$field] = $res->val; |
|
79 | + return $ret; |
|
80 | + } |
|
81 | + else if (isset($result[0])) return $result[0]->val; |
|
82 | + else return 0; |
|
83 | + } |
|
84 | + |
|
85 | + private function selectQuery(\Maphper\Lib\Query $query) { |
|
86 | + return $this->adapter->query($query)->fetchAll(\PDO::FETCH_OBJ); |
|
87 | + } |
|
88 | + |
|
89 | + public function clearResultCache() { |
|
90 | + $this->resultCache = []; |
|
91 | + } |
|
92 | + |
|
93 | + public function clearIDCache() { |
|
94 | + $this->idCache = []; |
|
95 | + } |
|
96 | + |
|
97 | + public function updateCache($data, $pkValue) { |
|
98 | 98 | if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
99 | 99 | else $this->cache[$pkValue] = $data; |
100 | - } |
|
100 | + } |
|
101 | 101 | |
102 | - public function deleteIDFromCache($id) { |
|
103 | - unset($this->idCache[$id]); |
|
104 | - } |
|
102 | + public function deleteIDFromCache($id) { |
|
103 | + unset($this->idCache[$id]); |
|
104 | + } |
|
105 | 105 | } |
@@ -3,50 +3,50 @@ |
||
3 | 3 | use Maphper\Maphper; |
4 | 4 | |
5 | 5 | class ArrayFilter { |
6 | - private $array; |
|
6 | + private $array; |
|
7 | 7 | |
8 | - public function __construct(array $array) { |
|
9 | - $this->array = $array; |
|
10 | - } |
|
8 | + public function __construct(array $array) { |
|
9 | + $this->array = $array; |
|
10 | + } |
|
11 | 11 | |
12 | - public function filter($fields) { |
|
13 | - $filteredArray = array_filter($this->array, $this->getSearchFieldFunction($fields, \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND)); |
|
14 | - // Need to reset indexes |
|
15 | - $filteredArray = array_values($filteredArray); |
|
16 | - return $filteredArray; |
|
17 | - } |
|
12 | + public function filter($fields) { |
|
13 | + $filteredArray = array_filter($this->array, $this->getSearchFieldFunction($fields, \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND)); |
|
14 | + // Need to reset indexes |
|
15 | + $filteredArray = array_values($filteredArray); |
|
16 | + return $filteredArray; |
|
17 | + } |
|
18 | 18 | |
19 | - private function getSearchFieldFunction($fields, $mode) { |
|
20 | - return function ($data) use ($fields, $mode) { |
|
21 | - foreach ($fields as $key => $val) { |
|
22 | - $currentFieldResult = $this->getIfFieldMatches($key, $val, $data, $mode); |
|
19 | + private function getSearchFieldFunction($fields, $mode) { |
|
20 | + return function ($data) use ($fields, $mode) { |
|
21 | + foreach ($fields as $key => $val) { |
|
22 | + $currentFieldResult = $this->getIfFieldMatches($key, $val, $data, $mode); |
|
23 | 23 | |
24 | - if (Maphper::FIND_OR & $mode && $currentFieldResult === true) return true; |
|
25 | - else if (!(Maphper::FIND_OR & $mode) && $currentFieldResult === false) return false; |
|
26 | - } |
|
27 | - return !(Maphper::FIND_OR & $mode); |
|
28 | - }; |
|
29 | - } |
|
24 | + if (Maphper::FIND_OR & $mode && $currentFieldResult === true) return true; |
|
25 | + else if (!(Maphper::FIND_OR & $mode) && $currentFieldResult === false) return false; |
|
26 | + } |
|
27 | + return !(Maphper::FIND_OR & $mode); |
|
28 | + }; |
|
29 | + } |
|
30 | 30 | |
31 | - private function getIfFieldMatches($key, $val, $data, $mode) { |
|
32 | - if (is_numeric($key) && is_array($val)) { |
|
33 | - return $this->getSearchFieldFunction($val, $key)($data); |
|
34 | - } |
|
35 | - else if (!isset($data->$key)) return false; |
|
36 | - else if (!(Maphper::FIND_BETWEEN & $mode) && !is_numeric($key) && is_array($val)) |
|
37 | - return in_array($data->$key, $val); |
|
38 | - else |
|
39 | - return $this->processFilter($mode, $val, $data->$key); |
|
40 | - } |
|
31 | + private function getIfFieldMatches($key, $val, $data, $mode) { |
|
32 | + if (is_numeric($key) && is_array($val)) { |
|
33 | + return $this->getSearchFieldFunction($val, $key)($data); |
|
34 | + } |
|
35 | + else if (!isset($data->$key)) return false; |
|
36 | + else if (!(Maphper::FIND_BETWEEN & $mode) && !is_numeric($key) && is_array($val)) |
|
37 | + return in_array($data->$key, $val); |
|
38 | + else |
|
39 | + return $this->processFilter($mode, $val, $data->$key); |
|
40 | + } |
|
41 | 41 | |
42 | - private function processFilter($mode, $expected, $actual) { |
|
43 | - if (Maphper::FIND_NOT & $mode) return $expected != $actual; |
|
44 | - else if (Maphper::FIND_GREATER & $mode && Maphper::FIND_EXACT & $mode) return $expected <= $actual; |
|
45 | - else if (Maphper::FIND_LESS & $mode && Maphper::FIND_EXACT & $mode) return $expected >= $actual; |
|
46 | - else if (Maphper::FIND_GREATER & $mode) return $expected < $actual; |
|
47 | - else if (Maphper::FIND_LESS & $mode) return $expected > $actual; |
|
48 | - else if (Maphper::FIND_BETWEEN & $mode) return $expected[0] <= $actual && $actual <= $expected[1]; |
|
49 | - else if (Maphper::FIND_NOCASE & $mode) return strtolower($expected) == strtolower($actual); |
|
50 | - return $expected == $actual; |
|
51 | - } |
|
42 | + private function processFilter($mode, $expected, $actual) { |
|
43 | + if (Maphper::FIND_NOT & $mode) return $expected != $actual; |
|
44 | + else if (Maphper::FIND_GREATER & $mode && Maphper::FIND_EXACT & $mode) return $expected <= $actual; |
|
45 | + else if (Maphper::FIND_LESS & $mode && Maphper::FIND_EXACT & $mode) return $expected >= $actual; |
|
46 | + else if (Maphper::FIND_GREATER & $mode) return $expected < $actual; |
|
47 | + else if (Maphper::FIND_LESS & $mode) return $expected > $actual; |
|
48 | + else if (Maphper::FIND_BETWEEN & $mode) return $expected[0] <= $actual && $actual <= $expected[1]; |
|
49 | + else if (Maphper::FIND_NOCASE & $mode) return strtolower($expected) == strtolower($actual); |
|
50 | + return $expected == $actual; |
|
51 | + } |
|
52 | 52 | } |
@@ -2,76 +2,76 @@ |
||
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 | - $arrayFilter = new \Maphper\Lib\ArrayFilter(iterator_to_array($this->data->getIterator())); |
|
23 | - $filteredArray = $arrayFilter->filter($fields); |
|
24 | - if (isset($options['order'])) { |
|
25 | - list($columns, $order) = explode(' ', $options['order']); |
|
26 | - usort($filteredArray, $this->getOrderFunction($order, $columns)); |
|
27 | - } |
|
28 | - if (isset($options['offset'])) $filteredArray = array_slice($filteredArray, $options['offset']); |
|
29 | - if (isset($options['limit'])) $filteredArray = array_slice($filteredArray, 0, $options['limit']); |
|
30 | - return $filteredArray; |
|
31 | - } |
|
21 | + public function findByField(array $fields, $options = []) { |
|
22 | + $arrayFilter = new \Maphper\Lib\ArrayFilter(iterator_to_array($this->data->getIterator())); |
|
23 | + $filteredArray = $arrayFilter->filter($fields); |
|
24 | + if (isset($options['order'])) { |
|
25 | + list($columns, $order) = explode(' ', $options['order']); |
|
26 | + usort($filteredArray, $this->getOrderFunction($order, $columns)); |
|
27 | + } |
|
28 | + if (isset($options['offset'])) $filteredArray = array_slice($filteredArray, $options['offset']); |
|
29 | + if (isset($options['limit'])) $filteredArray = array_slice($filteredArray, 0, $options['limit']); |
|
30 | + return $filteredArray; |
|
31 | + } |
|
32 | 32 | |
33 | 33 | public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { |
34 | - return $function($this->findByField($criteria)); |
|
35 | - } |
|
34 | + return $function($this->findByField($criteria)); |
|
35 | + } |
|
36 | 36 | |
37 | 37 | public function deleteById($id) { |
38 | - unset($this->data[$id]); |
|
39 | - } |
|
38 | + unset($this->data[$id]); |
|
39 | + } |
|
40 | 40 | |
41 | 41 | public function deleteByField(array $fields, array $options) { |
42 | - foreach ($this->findByField($fields, $options) as $val) unset($this->data[$val->{$this->id[0]}]); |
|
43 | - } |
|
42 | + foreach ($this->findByField($fields, $options) as $val) unset($this->data[$val->{$this->id[0]}]); |
|
43 | + } |
|
44 | 44 | |
45 | - public function save($data) { |
|
46 | - if (isset($data->{$this->id[0]})) { |
|
47 | - $id = $data->{$this->id[0]}; |
|
48 | - } |
|
49 | - else { |
|
50 | - $id = count($this->data); |
|
51 | - $data->{$this->id[0]} = $id; |
|
52 | - } |
|
45 | + public function save($data) { |
|
46 | + if (isset($data->{$this->id[0]})) { |
|
47 | + $id = $data->{$this->id[0]}; |
|
48 | + } |
|
49 | + else { |
|
50 | + $id = count($this->data); |
|
51 | + $data->{$this->id[0]} = $id; |
|
52 | + } |
|
53 | 53 | |
54 | - $this->data[$id] = (object)array_merge($this->findById($id), (array)$data); |
|
55 | - } |
|
54 | + $this->data[$id] = (object)array_merge($this->findById($id), (array)$data); |
|
55 | + } |
|
56 | 56 | |
57 | - public function getErrors() { |
|
58 | - return []; |
|
59 | - } |
|
57 | + public function getErrors() { |
|
58 | + return []; |
|
59 | + } |
|
60 | 60 | |
61 | - private function getOrderFunction($order, $columns) { |
|
62 | - return function($a, $b) use ($order, $columns) { |
|
63 | - foreach (explode(',', $columns) as $column) { |
|
64 | - $aColumn = $a->$column; |
|
65 | - $bColumn = $b->$column; |
|
66 | - if ($aColumn === $bColumn) { |
|
67 | - $sortVal = 0; |
|
68 | - continue; |
|
69 | - } |
|
70 | - else $sortVal = ($aColumn < $bColumn) ? -1 : 1; |
|
71 | - break; |
|
72 | - } |
|
73 | - if ($order === 'desc') return -$sortVal; |
|
74 | - else return $sortVal; |
|
75 | - }; |
|
76 | - } |
|
61 | + private function getOrderFunction($order, $columns) { |
|
62 | + return function($a, $b) use ($order, $columns) { |
|
63 | + foreach (explode(',', $columns) as $column) { |
|
64 | + $aColumn = $a->$column; |
|
65 | + $bColumn = $b->$column; |
|
66 | + if ($aColumn === $bColumn) { |
|
67 | + $sortVal = 0; |
|
68 | + continue; |
|
69 | + } |
|
70 | + else $sortVal = ($aColumn < $bColumn) ? -1 : 1; |
|
71 | + break; |
|
72 | + } |
|
73 | + if ($order === 'desc') return -$sortVal; |
|
74 | + else return $sortVal; |
|
75 | + }; |
|
76 | + } |
|
77 | 77 | } |
@@ -3,18 +3,18 @@ |
||
3 | 3 | use Maphper\Maphper; |
4 | 4 | |
5 | 5 | class Like implements WhereConditional { |
6 | - public function matches($key, $value, $mode) { |
|
7 | - return Maphper::FIND_LIKE & $mode || Maphper::FIND_STARTS & $mode || |
|
8 | - Maphper::FIND_ENDS & $mode || Maphper::FIND_NOCASE & $mode; |
|
9 | - } |
|
6 | + public function matches($key, $value, $mode) { |
|
7 | + return Maphper::FIND_LIKE & $mode || Maphper::FIND_STARTS & $mode || |
|
8 | + Maphper::FIND_ENDS & $mode || Maphper::FIND_NOCASE & $mode; |
|
9 | + } |
|
10 | 10 | |
11 | - public function getSql($key, $value, $mode) { |
|
12 | - if (Maphper::FIND_LIKE & $mode || Maphper::FIND_STARTS & $mode) $value = '%' . $value; |
|
13 | - if (Maphper::FIND_LIKE & $mode || Maphper::FIND_ENDS & $mode) $value .= '%'; |
|
11 | + public function getSql($key, $value, $mode) { |
|
12 | + if (Maphper::FIND_LIKE & $mode || Maphper::FIND_STARTS & $mode) $value = '%' . $value; |
|
13 | + if (Maphper::FIND_LIKE & $mode || Maphper::FIND_ENDS & $mode) $value .= '%'; |
|
14 | 14 | |
15 | - return [ |
|
16 | - 'sql' => [$key . ' LIKE :' . $key], |
|
17 | - 'args' => [$key => $value] |
|
18 | - ]; |
|
19 | - } |
|
15 | + return [ |
|
16 | + 'sql' => [$key . ' LIKE :' . $key], |
|
17 | + 'args' => [$key => $value] |
|
18 | + ]; |
|
19 | + } |
|
20 | 20 | } |
@@ -2,6 +2,6 @@ |
||
2 | 2 | namespace Maphper\Lib\Sql; |
3 | 3 | |
4 | 4 | interface WhereConditional { |
5 | - public function matches($key, $value, $mode); |
|
6 | - public function getSql($key, $value, $mode); |
|
5 | + public function matches($key, $value, $mode); |
|
6 | + public function getSql($key, $value, $mode); |
|
7 | 7 | } |
@@ -2,62 +2,62 @@ |
||
2 | 2 | namespace Maphper\Lib\Sql; |
3 | 3 | |
4 | 4 | class WhereBuilder { |
5 | - private $conditionals = []; |
|
6 | - |
|
7 | - public function __construct() { |
|
8 | - $defaultConditionals = [ |
|
9 | - 'Maphper\Lib\Sql\Between', |
|
10 | - 'Maphper\Lib\Sql\In', |
|
11 | - 'Maphper\Lib\Sql\NullConditional', |
|
12 | - 'Maphper\Lib\Sql\Like', |
|
13 | - 'Maphper\Lib\Sql\GeneralOperator' |
|
14 | - ]; |
|
15 | - |
|
16 | - foreach ($defaultConditionals as $conditional) $this->addConditional(new $conditional); |
|
17 | - } |
|
5 | + private $conditionals = []; |
|
6 | + |
|
7 | + public function __construct() { |
|
8 | + $defaultConditionals = [ |
|
9 | + 'Maphper\Lib\Sql\Between', |
|
10 | + 'Maphper\Lib\Sql\In', |
|
11 | + 'Maphper\Lib\Sql\NullConditional', |
|
12 | + 'Maphper\Lib\Sql\Like', |
|
13 | + 'Maphper\Lib\Sql\GeneralOperator' |
|
14 | + ]; |
|
15 | + |
|
16 | + foreach ($defaultConditionals as $conditional) $this->addConditional(new $conditional); |
|
17 | + } |
|
18 | 18 | |
19 | - public function addConditional(WhereConditional $conditional) { |
|
20 | - $this->conditionals[] = $conditional; |
|
21 | - } |
|
19 | + public function addConditional(WhereConditional $conditional) { |
|
20 | + $this->conditionals[] = $conditional; |
|
21 | + } |
|
22 | 22 | |
23 | - //Needs to be broken up into better methods |
|
23 | + //Needs to be broken up into better methods |
|
24 | 24 | public function createSql($fields, $mode = \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND){ |
25 | 25 | $args = []; |
26 | 26 | $sql = []; |
27 | 27 | |
28 | - foreach ($fields as $key => $value) { |
|
29 | - $value = $this->convertDates($value); |
|
28 | + foreach ($fields as $key => $value) { |
|
29 | + $value = $this->convertDates($value); |
|
30 | 30 | |
31 | - if (is_object($value)) continue; |
|
31 | + if (is_object($value)) continue; |
|
32 | 32 | if (is_numeric($key) && is_array($value)) { |
33 | 33 | $result = $this->createSql($value, $key); |
34 | 34 | } |
35 | - else { |
|
36 | - $result = $this->getConditional($key, $value, $mode); |
|
37 | - } |
|
38 | - $sql = array_merge($sql, $result['sql']); |
|
39 | - $args = array_merge($args, $result['args']); |
|
40 | - } |
|
41 | - |
|
42 | - if (\Maphper\Maphper::FIND_OR & $mode) $query = implode(' OR ', $sql); |
|
35 | + else { |
|
36 | + $result = $this->getConditional($key, $value, $mode); |
|
37 | + } |
|
38 | + $sql = array_merge($sql, $result['sql']); |
|
39 | + $args = array_merge($args, $result['args']); |
|
40 | + } |
|
41 | + |
|
42 | + if (\Maphper\Maphper::FIND_OR & $mode) $query = implode(' OR ', $sql); |
|
43 | 43 | else $query = implode(' AND ', $sql); |
44 | 44 | if (!empty($query)) $query = '(' . $query . ')'; |
45 | 45 | return ['args' => $args, 'sql' => $query]; |
46 | 46 | } |
47 | 47 | |
48 | - private function getConditional($key, $value, $mode) { |
|
49 | - foreach ($this->conditionals as $conditional) { |
|
50 | - if ($conditional->matches($key, $value, $mode)) |
|
51 | - return $conditional->getSql($key, $value, $mode); |
|
52 | - } |
|
53 | - throw new \Exception("Invalid WHERE query"); |
|
54 | - } |
|
55 | - |
|
56 | - private function convertDates($value) { |
|
57 | - if ($value instanceof \DateTime) { |
|
58 | - if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
59 | - else $value = $value->format('Y-m-d H:i:s'); |
|
60 | - } |
|
61 | - return $value; |
|
62 | - } |
|
48 | + private function getConditional($key, $value, $mode) { |
|
49 | + foreach ($this->conditionals as $conditional) { |
|
50 | + if ($conditional->matches($key, $value, $mode)) |
|
51 | + return $conditional->getSql($key, $value, $mode); |
|
52 | + } |
|
53 | + throw new \Exception("Invalid WHERE query"); |
|
54 | + } |
|
55 | + |
|
56 | + private function convertDates($value) { |
|
57 | + if ($value instanceof \DateTime) { |
|
58 | + if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
59 | + else $value = $value->format('Y-m-d H:i:s'); |
|
60 | + } |
|
61 | + return $value; |
|
62 | + } |
|
63 | 63 | } |
@@ -2,21 +2,21 @@ |
||
2 | 2 | namespace Maphper\Lib\Sql; |
3 | 3 | |
4 | 4 | class In implements WhereConditional { |
5 | - public function matches($key, $value, $mode) { |
|
6 | - return !is_numeric($key) && is_array($value); |
|
7 | - } |
|
5 | + public function matches($key, $value, $mode) { |
|
6 | + return !is_numeric($key) && is_array($value); |
|
7 | + } |
|
8 | 8 | |
9 | - public function getSql($key, $value, $mode) { |
|
10 | - $args = []; |
|
11 | - $inSql = []; |
|
12 | - $count = count($value); |
|
13 | - for ($i = 0; $i < $count; $i++) { |
|
14 | - $args[$key . $i] = $value[$i]; |
|
15 | - $inSql[] = ':' . $key . $i; |
|
16 | - } |
|
17 | - if (count($inSql) == 0) return []; |
|
18 | - else $sql = [$key . ' IN ( ' . implode(', ', $inSql) . ')']; |
|
9 | + public function getSql($key, $value, $mode) { |
|
10 | + $args = []; |
|
11 | + $inSql = []; |
|
12 | + $count = count($value); |
|
13 | + for ($i = 0; $i < $count; $i++) { |
|
14 | + $args[$key . $i] = $value[$i]; |
|
15 | + $inSql[] = ':' . $key . $i; |
|
16 | + } |
|
17 | + if (count($inSql) == 0) return []; |
|
18 | + else $sql = [$key . ' IN ( ' . implode(', ', $inSql) . ')']; |
|
19 | 19 | |
20 | - return ['args' => $args, 'sql' => $sql]; |
|
21 | - } |
|
20 | + return ['args' => $args, 'sql' => $sql]; |
|
21 | + } |
|
22 | 22 | } |
@@ -2,20 +2,20 @@ |
||
2 | 2 | namespace Maphper\Lib\Sql; |
3 | 3 | |
4 | 4 | class Between implements WhereConditional { |
5 | - public function matches($key, $value, $mode) { |
|
6 | - return is_array($value) && \Maphper\Maphper::FIND_BETWEEN & $mode; |
|
7 | - } |
|
5 | + public function matches($key, $value, $mode) { |
|
6 | + return is_array($value) && \Maphper\Maphper::FIND_BETWEEN & $mode; |
|
7 | + } |
|
8 | 8 | |
9 | - public function getSql($key, $value, $mode) { |
|
10 | - return [ |
|
11 | - 'sql' => [ |
|
12 | - $key . '>= :' . $key . 'from', |
|
13 | - $key . ' <= :' . $key . 'to' |
|
14 | - ], |
|
15 | - 'args' => [ |
|
16 | - $key . 'from' => $value[0], |
|
17 | - $key . 'to' => $value[1] |
|
18 | - ] |
|
19 | - ]; |
|
20 | - } |
|
9 | + public function getSql($key, $value, $mode) { |
|
10 | + return [ |
|
11 | + 'sql' => [ |
|
12 | + $key . '>= :' . $key . 'from', |
|
13 | + $key . ' <= :' . $key . 'to' |
|
14 | + ], |
|
15 | + 'args' => [ |
|
16 | + $key . 'from' => $value[0], |
|
17 | + $key . 'to' => $value[1] |
|
18 | + ] |
|
19 | + ]; |
|
20 | + } |
|
21 | 21 | } |