@@ -100,15 +100,15 @@ discard block |
||
100 | 100 | $value = $this->wrap($value); |
101 | 101 | $this->dataSource->save($value); |
102 | 102 | $value = $this->wrap((object) array_merge((array)$value, (array)$valueCopy)); |
103 | - $this->createNew($value, $valueObj); |
|
103 | + $this->createNew($value, $valueObj); |
|
104 | 104 | } |
105 | 105 | |
106 | 106 | public function offsetExists($offset) { |
107 | 107 | if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
108 | - if (!empty($this->settings['filter'])) { |
|
109 | - $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
110 | - return isset($data[0]); |
|
111 | - } |
|
108 | + if (!empty($this->settings['filter'])) { |
|
109 | + $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
110 | + return isset($data[0]); |
|
111 | + } |
|
112 | 112 | return (bool) $this->dataSource->findById($offset); |
113 | 113 | } |
114 | 114 | |
@@ -118,10 +118,10 @@ discard block |
||
118 | 118 | |
119 | 119 | public function offsetGet($offset) { |
120 | 120 | if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
121 | - if (!empty($this->settings['filter'])) { |
|
122 | - $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
123 | - return $this->wrap($this->createNew(isset($data[0]) ? $data[0] : null)); |
|
124 | - } |
|
121 | + if (!empty($this->settings['filter'])) { |
|
122 | + $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
123 | + return $this->wrap($this->createNew(isset($data[0]) ? $data[0] : null)); |
|
124 | + } |
|
125 | 125 | return $this->wrap($this->createNew($this->dataSource->findById($offset))); |
126 | 126 | } |
127 | 127 |
@@ -1,19 +1,19 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | namespace Maphper; |
3 | 3 | class Maphper implements \Countable, \ArrayAccess, \IteratorAggregate { |
4 | - const FIND_EXACT = 0x1; |
|
5 | - const FIND_LIKE = 0x2; |
|
6 | - const FIND_STARTS = 0x4; |
|
7 | - const FIND_ENDS = 0x8; |
|
8 | - const FIND_BIT = 0x10; |
|
9 | - const FIND_GREATER = 0x20; |
|
10 | - const FIND_LESS = 0x40; |
|
11 | - const FIND_EXPRESSION = 0x80; |
|
12 | - const FIND_AND = 0x100; |
|
13 | - const FIND_OR = 0x200; |
|
14 | - const FIND_NOT = 0x400; |
|
15 | - const FIND_BETWEEN = 0x800; |
|
16 | - const FIND_NOCASE = 0x1000; |
|
4 | + const FIND_EXACT = 0x1; |
|
5 | + const FIND_LIKE = 0x2; |
|
6 | + const FIND_STARTS = 0x4; |
|
7 | + const FIND_ENDS = 0x8; |
|
8 | + const FIND_BIT = 0x10; |
|
9 | + const FIND_GREATER = 0x20; |
|
10 | + const FIND_LESS = 0x40; |
|
11 | + const FIND_EXPRESSION = 0x80; |
|
12 | + const FIND_AND = 0x100; |
|
13 | + const FIND_OR = 0x200; |
|
14 | + const FIND_NOT = 0x400; |
|
15 | + const FIND_BETWEEN = 0x800; |
|
16 | + const FIND_NOCASE = 0x1000; |
|
17 | 17 | |
18 | 18 | private $dataSource; |
19 | 19 | private $relations = []; |
@@ -54,7 +54,7 @@ discard block |
||
54 | 54 | } |
55 | 55 | } |
56 | 56 | $results = $this->dataSource->findByField($this->settings['filter'], |
57 | - ['order' => $this->settings['sort'], 'limit' => $this->settings['limit'], 'offset' => $this->settings['offset'] ]); |
|
57 | + ['order' => $this->settings['sort'], 'limit' => $this->settings['limit'], 'offset' => $this->settings['offset']]); |
|
58 | 58 | |
59 | 59 | $siblings = new \ArrayObject(); |
60 | 60 | |
@@ -94,7 +94,7 @@ discard block |
||
94 | 94 | $valueCopy = clone $value; |
95 | 95 | $value = $this->wrap($value); |
96 | 96 | $this->dataSource->save($value); |
97 | - $value = $this->wrap((object) array_merge((array)$value, (array)$valueCopy)); |
|
97 | + $value = $this->wrap((object)array_merge((array)$value, (array)$valueCopy)); |
|
98 | 98 | $this->createNew($value, $valueObj); |
99 | 99 | } |
100 | 100 | |
@@ -104,7 +104,7 @@ discard block |
||
104 | 104 | $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
105 | 105 | return isset($data[0]); |
106 | 106 | } |
107 | - return (bool) $this->dataSource->findById($offset); |
|
107 | + return (bool)$this->dataSource->findById($offset); |
|
108 | 108 | } |
109 | 109 | |
110 | 110 | public function offsetUnset($id) { |
@@ -139,7 +139,7 @@ discard block |
||
139 | 139 | private function wrap($object, $key = null, $siblings = null) { |
140 | 140 | //see if any relations need overwriting |
141 | 141 | foreach ($this->relations as $name => $relation) { |
142 | - if (isset($object->$name) && !($object->$name instanceof \Maphper\Relation) ) { |
|
142 | + if (isset($object->$name) && !($object->$name instanceof \Maphper\Relation)) { |
|
143 | 143 | //After overwriting the relation, does the parent object ($object) need overwriting as well? |
144 | 144 | if ($relation->overwrite($object, $object->$name)) $this[] = $object; |
145 | 145 | } |
@@ -76,13 +76,17 @@ discard block |
||
76 | 76 | private function processFilters($value) { |
77 | 77 | //When saving to a mapper with filters, write the filters back into the object being stored |
78 | 78 | foreach ($this->settings['filter'] as $key => $filterValue) { |
79 | - if (empty($value->$key) && !is_array($filterValue)) $value->$key = $filterValue; |
|
79 | + if (empty($value->$key) && !is_array($filterValue)) { |
|
80 | + $value->$key = $filterValue; |
|
81 | + } |
|
80 | 82 | } |
81 | 83 | return $value; |
82 | 84 | } |
83 | 85 | |
84 | 86 | public function offsetSet($offset, $valueObj) { |
85 | - if ($valueObj instanceof \Maphper\Relation) throw new \Exception(); |
|
87 | + if ($valueObj instanceof \Maphper\Relation) { |
|
88 | + throw new \Exception(); |
|
89 | + } |
|
86 | 90 | |
87 | 91 | //Extract private properties from the object |
88 | 92 | $propertyReader = new \Maphper\Lib\VisibilityOverride(); |
@@ -90,7 +94,9 @@ discard block |
||
90 | 94 | |
91 | 95 | $value = $this->processFilters($value); |
92 | 96 | $pk = $this->dataSource->getPrimaryKey(); |
93 | - if ($offset !== null) $value->{$pk[0]} = $offset; |
|
97 | + if ($offset !== null) { |
|
98 | + $value->{$pk[0]} = $offset; |
|
99 | + } |
|
94 | 100 | $valueCopy = clone $value; |
95 | 101 | $value = $this->wrap($value); |
96 | 102 | $this->dataSource->save($value); |
@@ -99,7 +105,9 @@ discard block |
||
99 | 105 | } |
100 | 106 | |
101 | 107 | public function offsetExists($offset) { |
102 | - if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
|
108 | + if (count($this->dataSource->getPrimaryKey()) > 1) { |
|
109 | + return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
|
110 | + } |
|
103 | 111 | if (!empty($this->settings['filter'])) { |
104 | 112 | $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
105 | 113 | return isset($data[0]); |
@@ -112,7 +120,9 @@ discard block |
||
112 | 120 | } |
113 | 121 | |
114 | 122 | public function offsetGet($offset) { |
115 | - if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
|
123 | + if (count($this->dataSource->getPrimaryKey()) > 1) { |
|
124 | + return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
|
125 | + } |
|
116 | 126 | if (!empty($this->settings['filter'])) { |
117 | 127 | $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
118 | 128 | return $this->wrap($this->createNew(isset($data[0]) ? $data[0] : null)); |
@@ -121,7 +131,9 @@ discard block |
||
121 | 131 | } |
122 | 132 | |
123 | 133 | private function createNew($data = [], $obj = null) { |
124 | - if (!$obj) $obj = (is_callable($this->settings['resultClass'])) ? call_user_func($this->settings['resultClass']) : new $this->settings['resultClass']; |
|
134 | + if (!$obj) { |
|
135 | + $obj = (is_callable($this->settings['resultClass'])) ? call_user_func($this->settings['resultClass']) : new $this->settings['resultClass']; |
|
136 | + } |
|
125 | 137 | $writer = new Lib\PropertyWriter($obj); |
126 | 138 | if ($data != null) { |
127 | 139 | foreach ($data as $key => $value) { |
@@ -141,7 +153,9 @@ discard block |
||
141 | 153 | foreach ($this->relations as $name => $relation) { |
142 | 154 | if (isset($object->$name) && !($object->$name instanceof \Maphper\Relation) ) { |
143 | 155 | //After overwriting the relation, does the parent object ($object) need overwriting as well? |
144 | - if ($relation->overwrite($object, $object->$name)) $this[] = $object; |
|
156 | + if ($relation->overwrite($object, $object->$name)) { |
|
157 | + $this[] = $object; |
|
158 | + } |
|
145 | 159 | } |
146 | 160 | |
147 | 161 | $object->$name = $relation->getData($object, $siblings); |
@@ -153,11 +167,15 @@ discard block |
||
153 | 167 | public function __call($method, $args) { |
154 | 168 | if (array_key_exists($method, $this->settings)) { |
155 | 169 | $maphper = new Maphper($this->dataSource, $this->settings, $this->relations); |
156 | - if (is_array($maphper->settings[$method])) $maphper->settings[$method] = $args[0] + $maphper->settings[$method]; |
|
157 | - else $maphper->settings[$method] = $args[0]; |
|
170 | + if (is_array($maphper->settings[$method])) { |
|
171 | + $maphper->settings[$method] = $args[0] + $maphper->settings[$method]; |
|
172 | + } else { |
|
173 | + $maphper->settings[$method] = $args[0]; |
|
174 | + } |
|
158 | 175 | return $maphper; |
176 | + } else { |
|
177 | + throw new \Exception('Method Maphper::' . $method . ' does not exist'); |
|
159 | 178 | } |
160 | - else throw new \Exception('Method Maphper::' . $method . ' does not exist'); |
|
161 | 179 | } |
162 | 180 | |
163 | 181 | public function findAggregate($function, $field, $group = null) { |
@@ -96,7 +96,7 @@ discard block |
||
96 | 96 | return; |
97 | 97 | |
98 | 98 | $runAgain = false; |
99 | - $columns = $this->pdo->query('SELECT * FROM '. $this->quote($table) . ' PROCEDURE ANALYSE(1,1)')->fetchAll(\PDO::FETCH_OBJ); |
|
99 | + $columns = $this->pdo->query('SELECT * FROM ' . $this->quote($table) . ' PROCEDURE ANALYSE(1,1)')->fetchAll(\PDO::FETCH_OBJ); |
|
100 | 100 | foreach ($columns as $column) { |
101 | 101 | $parts = explode('.', $column->Field_name); |
102 | 102 | $name = $this->quote(end($parts)); |
@@ -130,7 +130,7 @@ discard block |
||
130 | 130 | } |
131 | 131 | } |
132 | 132 | |
133 | - $this->pdo->query('ALTER TABLE ' . $this->quote($table) . ' MODIFY '. $name . ' ' . $type); |
|
133 | + $this->pdo->query('ALTER TABLE ' . $this->quote($table) . ' MODIFY ' . $name . ' ' . $type); |
|
134 | 134 | } |
135 | 135 | } |
136 | 136 | //Sometimes a second pass is needed, if a column has gone from varchar -> int(11) a better int type may be needed |
@@ -16,10 +16,13 @@ discard block |
||
16 | 16 | |
17 | 17 | private function getCachedStmt($sql) { |
18 | 18 | $queryId = md5($sql); |
19 | - if (isset($this->queryCache[$queryId])) $stmt = $this->queryCache[$queryId]; |
|
20 | - else { |
|
19 | + if (isset($this->queryCache[$queryId])) { |
|
20 | + $stmt = $this->queryCache[$queryId]; |
|
21 | + } else { |
|
21 | 22 | $stmt = $this->pdo->prepare($sql, [\PDO::ATTR_CURSOR => \PDO::CURSOR_FWDONLY]); |
22 | - if ($stmt) $this->queryCache[$queryId] = $stmt; |
|
23 | + if ($stmt) { |
|
24 | + $this->queryCache[$queryId] = $stmt; |
|
25 | + } |
|
23 | 26 | } |
24 | 27 | return $stmt; |
25 | 28 | } |
@@ -28,20 +31,30 @@ discard block |
||
28 | 31 | $stmt = $this->getCachedStmt($query->getSql()); |
29 | 32 | $args = $query->getArgs(); |
30 | 33 | foreach ($args as $name => &$arg) { |
31 | - if ($arg instanceof \DateTime) $arg = $arg->format('Y-m-d H:i:s'); |
|
34 | + if ($arg instanceof \DateTime) { |
|
35 | + $arg = $arg->format('Y-m-d H:i:s'); |
|
36 | + } |
|
32 | 37 | } |
33 | 38 | |
34 | 39 | $res = $stmt->execute($args); |
35 | 40 | |
36 | - if (strpos(trim($query->getSql()), 'SELECT') === 0) return $stmt->fetchAll(\PDO::FETCH_OBJ); |
|
37 | - else return $stmt; |
|
41 | + if (strpos(trim($query->getSql()), 'SELECT') === 0) { |
|
42 | + return $stmt->fetchAll(\PDO::FETCH_OBJ); |
|
43 | + } else { |
|
44 | + return $stmt; |
|
45 | + } |
|
38 | 46 | } |
39 | 47 | |
40 | 48 | private function getType($val) { |
41 | - if ($val instanceof \DateTime) return 'DATETIME'; |
|
42 | - else if (is_int($val)) return 'INT(11)'; |
|
43 | - else if (is_double($val)) return 'DECIMAL(9,' . strlen($val) - strrpos($val, '.') - 1 . ')'; |
|
44 | - else if (is_string($val)) return strlen($val) < 192 ? 'VARCHAR(191)' : 'LONGBLOB'; |
|
49 | + if ($val instanceof \DateTime) { |
|
50 | + return 'DATETIME'; |
|
51 | + } else if (is_int($val)) { |
|
52 | + return 'INT(11)'; |
|
53 | + } else if (is_double($val)) { |
|
54 | + return 'DECIMAL(9,' . strlen($val) - strrpos($val, '.') - 1 . ')'; |
|
55 | + } else if (is_string($val)) { |
|
56 | + return strlen($val) < 192 ? 'VARCHAR(191)' : 'LONGBLOB'; |
|
57 | + } |
|
45 | 58 | return 'VARCHAR(191)'; |
46 | 59 | } |
47 | 60 | |
@@ -50,8 +63,11 @@ discard block |
||
50 | 63 | $parts = []; |
51 | 64 | foreach ($primaryKey as $key) { |
52 | 65 | $pk = $data->$key; |
53 | - if ($pk == null) $parts[] = $key . ' INT(11) NOT NULL AUTO_INCREMENT'; |
|
54 | - else $parts[] = $key . ' ' . $this->getType($pk) . ' NOT NULL'; |
|
66 | + if ($pk == null) { |
|
67 | + $parts[] = $key . ' INT(11) NOT NULL AUTO_INCREMENT'; |
|
68 | + } else { |
|
69 | + $parts[] = $key . ' ' . $this->getType($pk) . ' NOT NULL'; |
|
70 | + } |
|
55 | 71 | } |
56 | 72 | |
57 | 73 | $pkField = implode(', ', $parts) . ', PRIMARY KEY(' . implode(', ', $primaryKey) . ')'; |
@@ -62,15 +78,20 @@ discard block |
||
62 | 78 | $this->createTable($table, $primaryKey, $data); |
63 | 79 | |
64 | 80 | foreach ($data as $key => $value) { |
65 | - if (is_array($value) || (is_object($value) && !($value instanceof \DateTime))) continue; |
|
66 | - if (in_array($key, $primaryKey)) continue; |
|
81 | + if (is_array($value) || (is_object($value) && !($value instanceof \DateTime))) { |
|
82 | + continue; |
|
83 | + } |
|
84 | + if (in_array($key, $primaryKey)) { |
|
85 | + continue; |
|
86 | + } |
|
67 | 87 | |
68 | 88 | $type = $this->getType($value); |
69 | 89 | |
70 | 90 | try { |
71 | - if (!$this->pdo->query('ALTER TABLE ' . $table . ' ADD ' . $this->quote($key) . ' ' . $type)) throw new \Exception('Could not alter table'); |
|
72 | - } |
|
73 | - catch (\Exception $e) { |
|
91 | + if (!$this->pdo->query('ALTER TABLE ' . $table . ' ADD ' . $this->quote($key) . ' ' . $type)) { |
|
92 | + throw new \Exception('Could not alter table'); |
|
93 | + } |
|
94 | + } catch (\Exception $e) { |
|
74 | 95 | $this->pdo->query('ALTER TABLE ' . $table . ' MODIFY ' . $this->quote($key) . ' ' . $type); |
75 | 96 | } |
76 | 97 | } |
@@ -88,7 +109,9 @@ discard block |
||
88 | 109 | $keyName = $this->quote(implode('_', $fields)); |
89 | 110 | |
90 | 111 | $results = $this->pdo->query('SHOW INDEX FROM ' . $this->quote($table) . ' WHERE Key_Name = "' . $keyName . '"'); |
91 | - if ($results && count($results->fetchAll()) == 0) $this->pdo->query('CREATE INDEX ' . $keyName . ' ON ' . $this->quote($table) . ' (' . implode(', ', $fields) . ')'); |
|
112 | + if ($results && count($results->fetchAll()) == 0) { |
|
113 | + $this->pdo->query('CREATE INDEX ' . $keyName . ' ON ' . $this->quote($table) . ' (' . implode(', ', $fields) . ')'); |
|
114 | + } |
|
92 | 115 | } |
93 | 116 | |
94 | 117 | public function optimiseColumns($table) { |
@@ -100,16 +123,21 @@ discard block |
||
100 | 123 | foreach ($columns as $column) { |
101 | 124 | $parts = explode('.', $column->Field_name); |
102 | 125 | $name = $this->quote(end($parts)); |
103 | - if ($column->Min_value === null && $column->Max_value === null) $this->pdo->query('ALTER TABLE ' . $this->quote($table) . ' DROP COLUMN ' . $name); |
|
104 | - else { |
|
126 | + if ($column->Min_value === null && $column->Max_value === null) { |
|
127 | + $this->pdo->query('ALTER TABLE ' . $this->quote($table) . ' DROP COLUMN ' . $name); |
|
128 | + } else { |
|
105 | 129 | $type = $column->Optimal_fieldtype; |
106 | 130 | if ($column->Max_length < 11) { |
107 | 131 | //Check for dates |
108 | 132 | $count = $this->pdo->query('SELECT count(*) as `count` FROM ' . $this->quote($table) . ' WHERE STR_TO_DATE(' . $name . ',\'%Y-%m-%d %H:%i:s\') IS NULL OR STR_TO_DATE(' . $name . ',\'%Y-%m-%d %H:%i:s\') != ' . $name . ' LIMIT 1')->fetch(\PDO::FETCH_OBJ)->count; |
109 | - if ($count == 0) $type = 'DATETIME'; |
|
133 | + if ($count == 0) { |
|
134 | + $type = 'DATETIME'; |
|
135 | + } |
|
110 | 136 | |
111 | 137 | $count = $this->pdo->query('SELECT count(*) as `count` FROM ' . $this->quote($table) . ' WHERE STR_TO_DATE(' . $name . ',\'%Y-%m-%d\') IS NULL OR STR_TO_DATE(' . $name . ',\'%Y-%m-%d\') != ' . $name . ' LIMIT 1')->fetch(\PDO::FETCH_OBJ)->count; |
112 | - if ($count == 0) $type = 'DATE'; |
|
138 | + if ($count == 0) { |
|
139 | + $type = 'DATE'; |
|
140 | + } |
|
113 | 141 | } |
114 | 142 | |
115 | 143 | //If it's text, work out if it would be better to be something else |
@@ -119,8 +147,7 @@ discard block |
||
119 | 147 | if ($count == 0) { |
120 | 148 | $type = 'INT(11)'; |
121 | 149 | $runAgain = true; |
122 | - } |
|
123 | - else { |
|
150 | + } else { |
|
124 | 151 | //See if it's decimal |
125 | 152 | $count = $this->pdo->query('SELECT count(*) FROM ' . $table . ' WHERE concat(\'\', ' . $name . ' * 1) != ' . $name . ')')->fetch(\PDO::FETCH_OBJ)->count; |
126 | 153 | if ($count == 0) { |
@@ -134,7 +161,9 @@ discard block |
||
134 | 161 | } |
135 | 162 | } |
136 | 163 | //Sometimes a second pass is needed, if a column has gone from varchar -> int(11) a better int type may be needed |
137 | - if ($runAgain) $this->optimiseColumns($table); |
|
164 | + if ($runAgain) { |
|
165 | + $this->optimiseColumns($table); |
|
166 | + } |
|
138 | 167 | } |
139 | 168 | |
140 | 169 | } |
@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | } |
34 | 34 | |
35 | 35 | private function getAdapter(\PDO $pdo) { |
36 | - $adapter = '\\Maphper\\DataSource\\' . ucfirst($pdo->getAttribute(\PDO::ATTR_DRIVER_NAME)) . 'Adapter'; |
|
36 | + $adapter = '\\Maphper\\DataSource\\' . ucfirst($pdo->getAttribute(\PDO::ATTR_DRIVER_NAME)) . 'Adapter'; |
|
37 | 37 | return new $adapter($pdo); |
38 | 38 | } |
39 | 39 | |
@@ -121,7 +121,7 @@ discard block |
||
121 | 121 | else $limit = ''; |
122 | 122 | |
123 | 123 | $query = $this->selectBuilder->createSql($fields, $mode); |
124 | - $query['sql'] = array_filter($query['sql']); |
|
124 | + $query['sql'] = array_filter($query['sql']); |
|
125 | 125 | $this->adapter->query($this->crudBuilder->delete($this->table, $query['sql'], $query['args'], $limit)); |
126 | 126 | $this->addIndex(array_keys($query['args'])); |
127 | 127 |
@@ -29,7 +29,7 @@ discard block |
||
29 | 29 | if (isset($options['fields'])) $this->fields = implode(',', array_map([$this->adapter, 'quote'], $options['fields'])); |
30 | 30 | $this->defaultSort = (isset($options['defaultSort'])) ? $options['defaultSort'] : implode(', ', $this->primaryKey); |
31 | 31 | if (isset($options['editmode'])) $this->alterDb = $options['editmode'] == true ? self::EDIT_STRUCTURE | self::EDIT_INDEX | self::EDIT_OPTIMISE : $options['editmode']; |
32 | - if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($table); |
|
32 | + if (self::EDIT_OPTIMISE & $this->alterDb && rand(0, 500) == 1) $this->adapter->optimiseColumns($table); |
|
33 | 33 | } |
34 | 34 | |
35 | 35 | private function getAdapter(\PDO $pdo) { |
@@ -168,7 +168,7 @@ discard block |
||
168 | 168 | //Something has changed, clear any cached results as they may now be incorrect |
169 | 169 | $this->resultCache = []; |
170 | 170 | $pkValue = $data->{$this->primaryKey[0]}; |
171 | - if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
171 | + if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object)array_merge((array)$this->cache[$pkValue], (array)$data); |
|
172 | 172 | else $this->cache[$pkValue] = $data; |
173 | 173 | } |
174 | 174 |
@@ -17,8 +17,11 @@ discard block |
||
17 | 17 | private $crudBuilder; |
18 | 18 | |
19 | 19 | public function __construct($db, $table, $primaryKey = 'id', array $options = []) { |
20 | - if ($db instanceof \PDO) $this->adapter = $this->getAdapter($db); |
|
21 | - else $this->adapter = $db; |
|
20 | + if ($db instanceof \PDO) { |
|
21 | + $this->adapter = $this->getAdapter($db); |
|
22 | + } else { |
|
23 | + $this->adapter = $db; |
|
24 | + } |
|
22 | 25 | |
23 | 26 | $this->table = $table; |
24 | 27 | $this->primaryKey = is_array($primaryKey) ? $primaryKey : [$primaryKey]; |
@@ -26,10 +29,16 @@ discard block |
||
26 | 29 | $this->crudBuilder = new \Maphper\Lib\CrudBuilder(); |
27 | 30 | $this->selectBuilder = new \Maphper\Lib\SelectBuilder(); |
28 | 31 | |
29 | - if (isset($options['fields'])) $this->fields = implode(',', array_map([$this->adapter, 'quote'], $options['fields'])); |
|
32 | + if (isset($options['fields'])) { |
|
33 | + $this->fields = implode(',', array_map([$this->adapter, 'quote'], $options['fields'])); |
|
34 | + } |
|
30 | 35 | $this->defaultSort = (isset($options['defaultSort'])) ? $options['defaultSort'] : implode(', ', $this->primaryKey); |
31 | - if (isset($options['editmode'])) $this->alterDb = $options['editmode'] == true ? self::EDIT_STRUCTURE | self::EDIT_INDEX | self::EDIT_OPTIMISE : $options['editmode']; |
|
32 | - if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($table); |
|
36 | + if (isset($options['editmode'])) { |
|
37 | + $this->alterDb = $options['editmode'] == true ? self::EDIT_STRUCTURE | self::EDIT_INDEX | self::EDIT_OPTIMISE : $options['editmode']; |
|
38 | + } |
|
39 | + if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) { |
|
40 | + $this->adapter->optimiseColumns($table); |
|
41 | + } |
|
33 | 42 | } |
34 | 43 | |
35 | 44 | private function getAdapter(\PDO $pdo) { |
@@ -54,20 +63,24 @@ discard block |
||
54 | 63 | if (!isset($this->cache[$id])) { |
55 | 64 | try { |
56 | 65 | $result = $this->adapter->query($this->selectBuilder->select($this->table, [$this->getPrimaryKey()[0] . ' = :id'], [':id' => $id], ['limit' => 1])); |
57 | - } |
|
58 | - catch (\Exception $e) { |
|
66 | + } catch (\Exception $e) { |
|
59 | 67 | $this->errors[] = $e; |
60 | 68 | } |
61 | 69 | |
62 | - if (isset($result[0])) $this->cache[$id] = $result[0]; |
|
63 | - else return null; |
|
70 | + if (isset($result[0])) { |
|
71 | + $this->cache[$id] = $result[0]; |
|
72 | + } else { |
|
73 | + return null; |
|
74 | + } |
|
64 | 75 | } |
65 | 76 | return $this->cache[$id]; |
66 | 77 | } |
67 | 78 | |
68 | 79 | public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { |
69 | 80 | //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 |
70 | - if (is_array($field)) $field = $field[0]; |
|
81 | + if (is_array($field)) { |
|
82 | + $field = $field[0]; |
|
83 | + } |
|
71 | 84 | $query = $this->selectBuilder->createSql($criteria, \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND); |
72 | 85 | |
73 | 86 | try { |
@@ -75,22 +88,27 @@ discard block |
||
75 | 88 | $this->addIndex(explode(',', $group)); |
76 | 89 | $result = $this->adapter->query($this->selectBuilder->aggregate($this->table, $function, $field, $query['sql'], $query['args'], $group)); |
77 | 90 | |
78 | - if (isset($result[0]) && $group == null) return $result[0]->val; |
|
79 | - else if ($group != null) { |
|
91 | + if (isset($result[0]) && $group == null) { |
|
92 | + return $result[0]->val; |
|
93 | + } else if ($group != null) { |
|
80 | 94 | $ret = []; |
81 | - foreach ($result as $res) $ret[$res->$field] = $res->val; |
|
95 | + foreach ($result as $res) { |
|
96 | + $ret[$res->$field] = $res->val; |
|
97 | + } |
|
82 | 98 | return $ret; |
99 | + } else { |
|
100 | + return 0; |
|
83 | 101 | } |
84 | - else return 0; |
|
85 | - } |
|
86 | - catch (\Exception $e) { |
|
102 | + } catch (\Exception $e) { |
|
87 | 103 | $this->errors[] = $e; |
88 | 104 | return $group ? [] : 0; |
89 | 105 | } |
90 | 106 | } |
91 | 107 | |
92 | 108 | private function addIndex($args) { |
93 | - if (self::EDIT_INDEX & $this->alterDb) $this->adapter->addIndex($this->table, $args); |
|
109 | + if (self::EDIT_INDEX & $this->alterDb) { |
|
110 | + $this->adapter->addIndex($this->table, $args); |
|
111 | + } |
|
94 | 112 | } |
95 | 113 | |
96 | 114 | public function findByField(array $fields, $options = []) { |
@@ -98,7 +116,9 @@ discard block |
||
98 | 116 | if (!isset($this->resultCache[$cacheId])) { |
99 | 117 | $query = $this->selectBuilder->createSql($fields, \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND); |
100 | 118 | |
101 | - if (!isset($options['order'])) $options['order'] = $this->defaultSort; |
|
119 | + if (!isset($options['order'])) { |
|
120 | + $options['order'] = $this->defaultSort; |
|
121 | + } |
|
102 | 122 | |
103 | 123 | $query['sql'] = array_filter($query['sql']); |
104 | 124 | |
@@ -106,8 +126,7 @@ discard block |
||
106 | 126 | $this->resultCache[$cacheId] = $this->adapter->query($this->selectBuilder->select($this->table, $query['sql'], $query['args'], $options)); |
107 | 127 | $this->addIndex(array_keys($query['args'])); |
108 | 128 | $this->addIndex(explode(',', $options['order'])); |
109 | - } |
|
110 | - catch (\Exception $e) { |
|
129 | + } catch (\Exception $e) { |
|
111 | 130 | $this->errors[] = $e; |
112 | 131 | $this->resultCache[$cacheId] = []; |
113 | 132 | } |
@@ -116,9 +135,14 @@ discard block |
||
116 | 135 | } |
117 | 136 | |
118 | 137 | public function deleteByField(array $fields, array $options = [], $mode = null) { |
119 | - if ($mode == null) $mode = \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND; |
|
120 | - if (isset($options['limit']) != null) $limit = ' LIMIT ' . $options['limit']; |
|
121 | - else $limit = ''; |
|
138 | + if ($mode == null) { |
|
139 | + $mode = \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND; |
|
140 | + } |
|
141 | + if (isset($options['limit']) != null) { |
|
142 | + $limit = ' LIMIT ' . $options['limit']; |
|
143 | + } else { |
|
144 | + $limit = ''; |
|
145 | + } |
|
122 | 146 | |
123 | 147 | $query = $this->selectBuilder->createSql($fields, $mode); |
124 | 148 | $query['sql'] = array_filter($query['sql']); |
@@ -144,7 +168,9 @@ discard block |
||
144 | 168 | $result = $this->insert($this->table, $this->primaryKey, $data); |
145 | 169 | |
146 | 170 | //If there was an error but PDO is silent, trigger the catch block anyway |
147 | - if ($result->errorCode() !== '00000') throw new \Exception('Could not insert into ' . $this->table); |
|
171 | + if ($result->errorCode() !== '00000') { |
|
172 | + throw new \Exception('Could not insert into ' . $this->table); |
|
173 | + } |
|
148 | 174 | |
149 | 175 | if ($tryagain === false && $result->rowCount() === 0) { |
150 | 176 | |
@@ -152,32 +178,38 @@ discard block |
||
152 | 178 | |
153 | 179 | $matched = $this->findByField($updateWhere->getArgs()); |
154 | 180 | |
155 | - if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
181 | + if (count($matched) == 0) { |
|
182 | + throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
183 | + } |
|
156 | 184 | } |
157 | 185 | |
158 | - } |
|
159 | - catch (\Exception $e) { |
|
186 | + } catch (\Exception $e) { |
|
160 | 187 | if ($tryagain) { |
161 | 188 | $this->adapter->alterDatabase($this->table, $this->primaryKey, $data); |
162 | 189 | $this->save($data, false); |
190 | + } else { |
|
191 | + throw $e; |
|
163 | 192 | } |
164 | - else throw $e; |
|
165 | 193 | } |
166 | 194 | //TODO: This will error if the primary key is a private field |
167 | - if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
195 | + if ($new && count($this->primaryKey) == 1) { |
|
196 | + $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
197 | + } |
|
168 | 198 | //Something has changed, clear any cached results as they may now be incorrect |
169 | 199 | $this->resultCache = []; |
170 | 200 | $pkValue = $data->{$this->primaryKey[0]}; |
171 | - if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
172 | - else $this->cache[$pkValue] = $data; |
|
201 | + if (isset($this->cache[$pkValue])) { |
|
202 | + $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
203 | + } else { |
|
204 | + $this->cache[$pkValue] = $data; |
|
205 | + } |
|
173 | 206 | } |
174 | 207 | |
175 | 208 | private function insert($table, array $primaryKey, $data) { |
176 | 209 | $error = 0; |
177 | 210 | try { |
178 | 211 | $result = $this->adapter->query($this->crudBuilder->insert($table, $data)); |
179 | - } |
|
180 | - catch (\Exception $e) { |
|
212 | + } catch (\Exception $e) { |
|
181 | 213 | $error = 1; |
182 | 214 | } |
183 | 215 |
@@ -2,113 +2,113 @@ |
||
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 | - $array = iterator_to_array($this->data); |
|
60 | - return $function($this->findByField($criteria)); |
|
61 | - } |
|
59 | + $array = iterator_to_array($this->data); |
|
60 | + return $function($this->findByField($criteria)); |
|
61 | + } |
|
62 | 62 | |
63 | 63 | public function deleteById($id) { |
64 | - unset($this->data[$id]); |
|
65 | - } |
|
64 | + unset($this->data[$id]); |
|
65 | + } |
|
66 | 66 | |
67 | 67 | public function deleteByField(array $fields) { |
68 | - foreach ($this->findByField($fields) as $val) unset($this->data[$val->{$this->id[0]}]); |
|
69 | - } |
|
68 | + foreach ($this->findByField($fields) as $val) unset($this->data[$val->{$this->id[0]}]); |
|
69 | + } |
|
70 | 70 | |
71 | - public function save($data) { |
|
72 | - if (isset($data->{$this->id[0]})) { |
|
73 | - $id = $data->{$this->id[0]}; |
|
74 | - } |
|
75 | - else { |
|
76 | - $id = count($this->data); |
|
77 | - $data->{$this->id[0]} = $id; |
|
78 | - } |
|
71 | + public function save($data) { |
|
72 | + if (isset($data->{$this->id[0]})) { |
|
73 | + $id = $data->{$this->id[0]}; |
|
74 | + } |
|
75 | + else { |
|
76 | + $id = count($this->data); |
|
77 | + $data->{$this->id[0]} = $id; |
|
78 | + } |
|
79 | 79 | |
80 | - $this->data[$id] = (object)array_merge($this->findById($id), (array)$data); |
|
81 | - } |
|
80 | + $this->data[$id] = (object)array_merge($this->findById($id), (array)$data); |
|
81 | + } |
|
82 | 82 | |
83 | - public function getErrors() { |
|
84 | - return []; |
|
85 | - } |
|
83 | + public function getErrors() { |
|
84 | + return []; |
|
85 | + } |
|
86 | 86 | |
87 | - private function getOrderFunction($order, $columns) { |
|
88 | - return function($a, $b) use ($order, $columns) { |
|
89 | - foreach (explode(',', $columns) as $column) { |
|
90 | - $aColumn = $a->$column; |
|
91 | - $bColumn = $b->$column; |
|
92 | - if ($aColumn === $bColumn) { |
|
93 | - $sortVal = 0; |
|
94 | - continue; |
|
95 | - } |
|
96 | - else $sortVal = ($aColumn < $bColumn) ? -1 : 1; |
|
97 | - break; |
|
98 | - } |
|
99 | - if ($order === 'desc') return -$sortVal; |
|
100 | - else return $sortVal; |
|
101 | - }; |
|
102 | - } |
|
87 | + private function getOrderFunction($order, $columns) { |
|
88 | + return function($a, $b) use ($order, $columns) { |
|
89 | + foreach (explode(',', $columns) as $column) { |
|
90 | + $aColumn = $a->$column; |
|
91 | + $bColumn = $b->$column; |
|
92 | + if ($aColumn === $bColumn) { |
|
93 | + $sortVal = 0; |
|
94 | + continue; |
|
95 | + } |
|
96 | + else $sortVal = ($aColumn < $bColumn) ? -1 : 1; |
|
97 | + break; |
|
98 | + } |
|
99 | + if ($order === 'desc') return -$sortVal; |
|
100 | + else return $sortVal; |
|
101 | + }; |
|
102 | + } |
|
103 | 103 | |
104 | - private function processFilter($mode, $expected, $actual) { |
|
105 | - if (Maphper::FIND_NOT & $mode) return $expected != $actual; |
|
106 | - else if (Maphper::FIND_GREATER & $mode && Maphper::FIND_EXACT & $mode) return $expected <= $actual; |
|
107 | - else if (Maphper::FIND_LESS & $mode && Maphper::FIND_EXACT & $mode) return $expected >= $actual; |
|
108 | - else if (Maphper::FIND_GREATER & $mode) return $expected < $actual; |
|
109 | - else if (Maphper::FIND_LESS & $mode) return $expected > $actual; |
|
110 | - else if (Maphper::FIND_BETWEEN & $mode) return $expected[0] <= $actual && $actual <= $expected[1]; |
|
111 | - else if (Maphper::FIND_NOCASE & $mode) return strtolower($expected) == strtolower($actual); |
|
112 | - return $expected == $actual; |
|
113 | - } |
|
104 | + private function processFilter($mode, $expected, $actual) { |
|
105 | + if (Maphper::FIND_NOT & $mode) return $expected != $actual; |
|
106 | + else if (Maphper::FIND_GREATER & $mode && Maphper::FIND_EXACT & $mode) return $expected <= $actual; |
|
107 | + else if (Maphper::FIND_LESS & $mode && Maphper::FIND_EXACT & $mode) return $expected >= $actual; |
|
108 | + else if (Maphper::FIND_GREATER & $mode) return $expected < $actual; |
|
109 | + else if (Maphper::FIND_LESS & $mode) return $expected > $actual; |
|
110 | + else if (Maphper::FIND_BETWEEN & $mode) return $expected[0] <= $actual && $actual <= $expected[1]; |
|
111 | + else if (Maphper::FIND_NOCASE & $mode) return strtolower($expected) == strtolower($actual); |
|
112 | + return $expected == $actual; |
|
113 | + } |
|
114 | 114 | } |
@@ -33,7 +33,7 @@ |
||
33 | 33 | } |
34 | 34 | |
35 | 35 | private function getSearchFieldFunction($fields, $mode) { |
36 | - return function ($data) use ($fields, $mode) { |
|
36 | + return function($data) use ($fields, $mode) { |
|
37 | 37 | foreach ($fields as $key => $val) { |
38 | 38 | $currentFieldResult = $this->getIfFieldMatches($key, $val, $data, $mode); |
39 | 39 |
@@ -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 = []) { |
@@ -65,14 +73,15 @@ discard block |
||
65 | 73 | } |
66 | 74 | |
67 | 75 | public function deleteByField(array $fields) { |
68 | - foreach ($this->findByField($fields) as $val) unset($this->data[$val->{$this->id[0]}]); |
|
76 | + foreach ($this->findByField($fields) as $val) { |
|
77 | + unset($this->data[$val->{$this->id[0]}]); |
|
78 | + } |
|
69 | 79 | } |
70 | 80 | |
71 | 81 | public function save($data) { |
72 | 82 | if (isset($data->{$this->id[0]})) { |
73 | 83 | $id = $data->{$this->id[0]}; |
74 | - } |
|
75 | - else { |
|
84 | + } else { |
|
76 | 85 | $id = count($this->data); |
77 | 86 | $data->{$this->id[0]} = $id; |
78 | 87 | } |
@@ -92,23 +101,35 @@ discard block |
||
92 | 101 | if ($aColumn === $bColumn) { |
93 | 102 | $sortVal = 0; |
94 | 103 | continue; |
104 | + } else { |
|
105 | + $sortVal = ($aColumn < $bColumn) ? -1 : 1; |
|
95 | 106 | } |
96 | - else $sortVal = ($aColumn < $bColumn) ? -1 : 1; |
|
97 | 107 | break; |
98 | 108 | } |
99 | - if ($order === 'desc') return -$sortVal; |
|
100 | - else return $sortVal; |
|
109 | + if ($order === 'desc') { |
|
110 | + return -$sortVal; |
|
111 | + } else { |
|
112 | + return $sortVal; |
|
113 | + } |
|
101 | 114 | }; |
102 | 115 | } |
103 | 116 | |
104 | 117 | private function processFilter($mode, $expected, $actual) { |
105 | - if (Maphper::FIND_NOT & $mode) return $expected != $actual; |
|
106 | - else if (Maphper::FIND_GREATER & $mode && Maphper::FIND_EXACT & $mode) return $expected <= $actual; |
|
107 | - else if (Maphper::FIND_LESS & $mode && Maphper::FIND_EXACT & $mode) return $expected >= $actual; |
|
108 | - else if (Maphper::FIND_GREATER & $mode) return $expected < $actual; |
|
109 | - else if (Maphper::FIND_LESS & $mode) return $expected > $actual; |
|
110 | - else if (Maphper::FIND_BETWEEN & $mode) return $expected[0] <= $actual && $actual <= $expected[1]; |
|
111 | - else if (Maphper::FIND_NOCASE & $mode) return strtolower($expected) == strtolower($actual); |
|
118 | + if (Maphper::FIND_NOT & $mode) { |
|
119 | + return $expected != $actual; |
|
120 | + } else if (Maphper::FIND_GREATER & $mode && Maphper::FIND_EXACT & $mode) { |
|
121 | + return $expected <= $actual; |
|
122 | + } else if (Maphper::FIND_LESS & $mode && Maphper::FIND_EXACT & $mode) { |
|
123 | + return $expected >= $actual; |
|
124 | + } else if (Maphper::FIND_GREATER & $mode) { |
|
125 | + return $expected < $actual; |
|
126 | + } else if (Maphper::FIND_LESS & $mode) { |
|
127 | + return $expected > $actual; |
|
128 | + } else if (Maphper::FIND_BETWEEN & $mode) { |
|
129 | + return $expected[0] <= $actual && $actual <= $expected[1]; |
|
130 | + } else if (Maphper::FIND_NOCASE & $mode) { |
|
131 | + return strtolower($expected) == strtolower($actual); |
|
132 | + } |
|
112 | 133 | return $expected == $actual; |
113 | 134 | } |
114 | 135 | } |
@@ -23,7 +23,7 @@ discard block |
||
23 | 23 | |
24 | 24 | $args = $query->getArgs(); |
25 | 25 | foreach ($args as &$arg) if ($arg instanceof \DateTime) { |
26 | - if ($arg->format('H:i:s') == '00:00:00') $arg = $arg->format('Y-m-d'); |
|
26 | + if ($arg->format('H:i:s') == '00:00:00') $arg = $arg->format('Y-m-d'); |
|
27 | 27 | else $arg = $arg->format('Y-m-d H:i:s'); |
28 | 28 | } |
29 | 29 | |
@@ -63,7 +63,7 @@ discard block |
||
63 | 63 | } |
64 | 64 | |
65 | 65 | private function tableExists($name) { |
66 | - $result = $this->pdo->query('SELECT name FROM sqlite_master WHERE type="table" and name="'. $name.'"'); |
|
66 | + $result = $this->pdo->query('SELECT name FROM sqlite_master WHERE type="table" and name="' . $name . '"'); |
|
67 | 67 | return count($result->fetchAll()) == 1; |
68 | 68 | } |
69 | 69 | |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | // SQLSTATE[HY000]: General error: 17 database schema has changed |
83 | 83 | $this->queryCache = []; |
84 | 84 | |
85 | - $affix = '_'.substr(md5($table), 0, 6); |
|
85 | + $affix = '_' . substr(md5($table), 0, 6); |
|
86 | 86 | $this->createTable($table . $affix, $primaryKey, $data); |
87 | 87 | $fields = []; |
88 | 88 | foreach ($data as $key => $value) { $fields[] = $key; } |
@@ -91,7 +91,7 @@ discard block |
||
91 | 91 | $columns = implode(', ', $this->getColumns($table)); |
92 | 92 | |
93 | 93 | $this->pdo->query('INSERT INTO ' . $this->quote($table . $affix) . '(' . $columns . ') SELECT ' . $columns . ' FROM ' . $this->quote($table)); |
94 | - $this->pdo->query('DROP TABLE IF EXISTS ' . $table ); |
|
94 | + $this->pdo->query('DROP TABLE IF EXISTS ' . $table); |
|
95 | 95 | } |
96 | 96 | } |
97 | 97 | catch (\PDOException $e) { |
@@ -99,8 +99,8 @@ discard block |
||
99 | 99 | echo $e->getMessage(); |
100 | 100 | } |
101 | 101 | |
102 | - $this->pdo->query('DROP TABLE IF EXISTS ' . $table ); |
|
103 | - $this->pdo->query('ALTER TABLE ' . $table . $affix. ' RENAME TO '. $table ); |
|
102 | + $this->pdo->query('DROP TABLE IF EXISTS ' . $table); |
|
103 | + $this->pdo->query('ALTER TABLE ' . $table . $affix . ' RENAME TO ' . $table); |
|
104 | 104 | |
105 | 105 | } |
106 | 106 | |
@@ -114,7 +114,7 @@ discard block |
||
114 | 114 | |
115 | 115 | $pkField = implode(', ', $parts) . ', PRIMARY KEY(' . implode(', ', $primaryKey) . ')'; |
116 | 116 | |
117 | - $this->pdo->query('DROP TABLE IF EXISTS ' . $table ); |
|
117 | + $this->pdo->query('DROP TABLE IF EXISTS ' . $table); |
|
118 | 118 | $this->pdo->query('CREATE TABLE ' . $table . ' (' . $pkField . ')'); |
119 | 119 | |
120 | 120 | foreach ($data as $key => $value) { |
@@ -15,32 +15,45 @@ discard block |
||
15 | 15 | |
16 | 16 | public function query(\Maphper\Lib\Query $query) { |
17 | 17 | $queryId = md5($query->getSql()); |
18 | - if (isset($this->queryCache[$queryId])) $stmt = $this->queryCache[$queryId]; |
|
19 | - else { |
|
18 | + if (isset($this->queryCache[$queryId])) { |
|
19 | + $stmt = $this->queryCache[$queryId]; |
|
20 | + } else { |
|
20 | 21 | $stmt = $this->pdo->prepare($query->getSql(), [\PDO::ATTR_CURSOR => \PDO::CURSOR_FWDONLY]); |
21 | - if ($stmt) $this->queryCache[$queryId] = $stmt; |
|
22 | + if ($stmt) { |
|
23 | + $this->queryCache[$queryId] = $stmt; |
|
24 | + } |
|
22 | 25 | } |
23 | 26 | |
24 | 27 | $args = $query->getArgs(); |
25 | - foreach ($args as &$arg) if ($arg instanceof \DateTime) { |
|
28 | + foreach ($args as &$arg) { |
|
29 | + if ($arg instanceof \DateTime) { |
|
26 | 30 | if ($arg->format('H:i:s') == '00:00:00') $arg = $arg->format('Y-m-d'); |
27 | - else $arg = $arg->format('Y-m-d H:i:s'); |
|
31 | + } |
|
32 | + else { |
|
33 | + $arg = $arg->format('Y-m-d H:i:s'); |
|
34 | + } |
|
28 | 35 | } |
29 | 36 | |
30 | 37 | if ($stmt !== false) { |
31 | 38 | try { |
32 | - if (count($args) > 0) $res = $stmt->execute($args); |
|
33 | - else $res = $stmt->execute(); |
|
34 | - if (substr($query->getSql(), 0, 6) === 'SELECT') return $stmt->fetchAll(\PDO::FETCH_OBJ); |
|
35 | - else return $stmt; |
|
36 | - } |
|
37 | - catch (\Exception $e) { |
|
39 | + if (count($args) > 0) { |
|
40 | + $res = $stmt->execute($args); |
|
41 | + } else { |
|
42 | + $res = $stmt->execute(); |
|
43 | + } |
|
44 | + if (substr($query->getSql(), 0, 6) === 'SELECT') { |
|
45 | + return $stmt->fetchAll(\PDO::FETCH_OBJ); |
|
46 | + } else { |
|
47 | + return $stmt; |
|
48 | + } |
|
49 | + } catch (\Exception $e) { |
|
38 | 50 | //SQLite causes an error if when the DB schema changes, rebuild $stmt and try again. |
39 | 51 | if ($e->getMessage() == 'SQLSTATE[HY000]: General error: 17 database schema has changed') { |
40 | 52 | unset($this->queryCache[$queryId]); |
41 | 53 | return $this->query($query); |
54 | + } else { |
|
55 | + return $stmt; |
|
42 | 56 | } |
43 | - else return $stmt; |
|
44 | 57 | } |
45 | 58 | } |
46 | 59 | //Handle SQLite when PDO_ERRMODE is set to SILENT |
@@ -54,12 +67,19 @@ discard block |
||
54 | 67 | } |
55 | 68 | |
56 | 69 | private function getType($val) { |
57 | - if ($val instanceof \DateTime) return 'DATETIME'; |
|
58 | - else if (is_int($val)) return 'INTEGER'; |
|
59 | - else if (is_double($val)) return 'DECIMAL(9,' . strlen($val) - strrpos($val, '.') - 1 . ')'; |
|
60 | - else if (is_string($val) && strlen($val) < 256) return 'VARCHAR(255)'; |
|
61 | - else if (is_string($val) && strlen($val) > 256) return 'LONGBLOG'; |
|
62 | - else return 'VARCHAR(255)'; |
|
70 | + if ($val instanceof \DateTime) { |
|
71 | + return 'DATETIME'; |
|
72 | + } else if (is_int($val)) { |
|
73 | + return 'INTEGER'; |
|
74 | + } else if (is_double($val)) { |
|
75 | + return 'DECIMAL(9,' . strlen($val) - strrpos($val, '.') - 1 . ')'; |
|
76 | + } else if (is_string($val) && strlen($val) < 256) { |
|
77 | + return 'VARCHAR(255)'; |
|
78 | + } else if (is_string($val) && strlen($val) > 256) { |
|
79 | + return 'LONGBLOG'; |
|
80 | + } else { |
|
81 | + return 'VARCHAR(255)'; |
|
82 | + } |
|
63 | 83 | } |
64 | 84 | |
65 | 85 | private function tableExists($name) { |
@@ -93,8 +113,7 @@ discard block |
||
93 | 113 | $this->pdo->query('INSERT INTO ' . $this->quote($table . $affix) . '(' . $columns . ') SELECT ' . $columns . ' FROM ' . $this->quote($table)); |
94 | 114 | $this->pdo->query('DROP TABLE IF EXISTS ' . $table ); |
95 | 115 | } |
96 | - } |
|
97 | - catch (\PDOException $e) { |
|
116 | + } catch (\PDOException $e) { |
|
98 | 117 | // No data to copy |
99 | 118 | echo $e->getMessage(); |
100 | 119 | } |
@@ -108,8 +127,11 @@ discard block |
||
108 | 127 | $parts = []; |
109 | 128 | foreach ($primaryKey as $key) { |
110 | 129 | $pk = $data->$key; |
111 | - if ($pk == null) $parts[] = $key . ' INTEGER'; |
|
112 | - else $parts[] = $key . ' ' . $this->getType($pk) . ' NOT NULL'; |
|
130 | + if ($pk == null) { |
|
131 | + $parts[] = $key . ' INTEGER'; |
|
132 | + } else { |
|
133 | + $parts[] = $key . ' ' . $this->getType($pk) . ' NOT NULL'; |
|
134 | + } |
|
113 | 135 | } |
114 | 136 | |
115 | 137 | $pkField = implode(', ', $parts) . ', PRIMARY KEY(' . implode(', ', $primaryKey) . ')'; |
@@ -118,8 +140,12 @@ discard block |
||
118 | 140 | $this->pdo->query('CREATE TABLE ' . $table . ' (' . $pkField . ')'); |
119 | 141 | |
120 | 142 | foreach ($data as $key => $value) { |
121 | - if (is_array($value) || (is_object($value) && !($value instanceof \DateTime))) continue; |
|
122 | - if (in_array($key, $primaryKey)) continue; |
|
143 | + if (is_array($value) || (is_object($value) && !($value instanceof \DateTime))) { |
|
144 | + continue; |
|
145 | + } |
|
146 | + if (in_array($key, $primaryKey)) { |
|
147 | + continue; |
|
148 | + } |
|
123 | 149 | |
124 | 150 | $type = $this->getType($value); |
125 | 151 | |
@@ -129,10 +155,14 @@ discard block |
||
129 | 155 | |
130 | 156 | |
131 | 157 | public function addIndex($table, array $fields) { |
132 | - if (empty($fields)) return false; |
|
158 | + if (empty($fields)) { |
|
159 | + return false; |
|
160 | + } |
|
133 | 161 | |
134 | 162 | //SQLite doesn't support ASC/DESC indexes, remove the keywords |
135 | - foreach ($fields as &$field) $field = str_ireplace([' desc', ' asc'], '', $field); |
|
163 | + foreach ($fields as &$field) { |
|
164 | + $field = str_ireplace([' desc', ' asc'], '', $field); |
|
165 | + } |
|
136 | 166 | sort($fields); |
137 | 167 | $fields = array_map('strtolower', $fields); |
138 | 168 | $fields = array_map('trim', $fields); |
@@ -141,8 +171,7 @@ discard block |
||
141 | 171 | |
142 | 172 | try { |
143 | 173 | $this->pdo->query('CREATE INDEX IF NOT EXISTS ' . $keyName . ' ON ' . $table . ' (' . implode(', ', $fields) . ')'); |
144 | - } |
|
145 | - catch (\Exception $e) { |
|
174 | + } catch (\Exception $e) { |
|
146 | 175 | |
147 | 176 | } |
148 | 177 | } |
@@ -20,8 +20,7 @@ |
||
20 | 20 | if (count($this->pk) == 1) { |
21 | 21 | $pk = end($this->pk); |
22 | 22 | return $this->array[$this->iterator]->$pk; |
23 | - } |
|
24 | - else { |
|
23 | + } else { |
|
25 | 24 | $current = $this->current(); |
26 | 25 | return array_map(function($pkName) use ($current) { |
27 | 26 | return $current->$pkName; |
@@ -80,17 +80,17 @@ |
||
80 | 80 | return ['args' => $args, 'sql' => [$query]]; |
81 | 81 | } |
82 | 82 | |
83 | - private function getOperator($mode) { |
|
84 | - $operator = ""; |
|
83 | + private function getOperator($mode) { |
|
84 | + $operator = ""; |
|
85 | 85 | |
86 | - if (\Maphper\Maphper::FIND_NOCASE & $mode) $operator = 'LIKE'; |
|
87 | - else if (\Maphper\Maphper::FIND_BIT & $mode) $operator = '&'; |
|
88 | - else if (\Maphper\Maphper::FIND_GREATER & $mode) $operator = '>'; |
|
89 | - else if (\Maphper\Maphper::FIND_LESS & $mode) $operator = '<'; |
|
90 | - else if (\Maphper\Maphper::FIND_NOT & $mode) $operator = '!='; |
|
86 | + if (\Maphper\Maphper::FIND_NOCASE & $mode) $operator = 'LIKE'; |
|
87 | + else if (\Maphper\Maphper::FIND_BIT & $mode) $operator = '&'; |
|
88 | + else if (\Maphper\Maphper::FIND_GREATER & $mode) $operator = '>'; |
|
89 | + else if (\Maphper\Maphper::FIND_LESS & $mode) $operator = '<'; |
|
90 | + else if (\Maphper\Maphper::FIND_NOT & $mode) $operator = '!='; |
|
91 | 91 | |
92 | - if (\Maphper\Maphper::FIND_EXACT & $mode) $operator .= '='; |
|
92 | + if (\Maphper\Maphper::FIND_EXACT & $mode) $operator .= '='; |
|
93 | 93 | |
94 | - return $operator; |
|
95 | - } |
|
94 | + return $operator; |
|
95 | + } |
|
96 | 96 | } |
@@ -25,7 +25,7 @@ discard block |
||
25 | 25 | |
26 | 26 | |
27 | 27 | //Needs to be broken up into better methods |
28 | - public function createSql($fields, $mode){ |
|
28 | + public function createSql($fields, $mode) { |
|
29 | 29 | $args = []; |
30 | 30 | $sql = []; |
31 | 31 | |
@@ -50,7 +50,7 @@ discard block |
||
50 | 50 | $inSql[] = ':' . $key . $i; |
51 | 51 | } |
52 | 52 | if (count($inSql) == 0) return []; |
53 | - else $sql[] = $key . ' IN ( ' . implode(', ', $inSql) . ')'; |
|
53 | + else $sql[] = $key . ' IN ( ' . implode(', ', $inSql) . ')'; |
|
54 | 54 | } |
55 | 55 | else if ($value === NULL) { |
56 | 56 | $nullSql = $key . ' IS '; |
@@ -9,17 +9,23 @@ discard block |
||
9 | 9 | |
10 | 10 | if (isset($options['offset'])) { |
11 | 11 | $offset = ' OFFSET ' . $options['offset']; |
12 | - if (!$limit) $limit = ' LIMIT 1000'; |
|
12 | + if (!$limit) { |
|
13 | + $limit = ' LIMIT 1000'; |
|
14 | + } |
|
15 | + } else { |
|
16 | + $offset = ''; |
|
13 | 17 | } |
14 | - else $offset = ''; |
|
15 | 18 | |
16 | 19 | $order = isset($options['order']) ? ' ORDER BY ' . $options['order'] : ''; |
17 | 20 | return new Query('SELECT * FROM ' . $table . ' ' . $where . $order . $limit . $offset, $args); |
18 | 21 | } |
19 | 22 | |
20 | 23 | public function aggregate($table, $function, $field, $where, $args, $group) { |
21 | - if ($group == true) $groupBy = ' GROUP BY ' . $field; |
|
22 | - else $groupBy = ''; |
|
24 | + if ($group == true) { |
|
25 | + $groupBy = ' GROUP BY ' . $field; |
|
26 | + } else { |
|
27 | + $groupBy = ''; |
|
28 | + } |
|
23 | 29 | return new Query('SELECT ' . $function . '(' . $field . ') as val, ' . $field . ' FROM ' . $table . ($where[0] != null ? ' WHERE ' : '') . implode(' AND ', $where) . ' ' . $groupBy, $args); |
24 | 30 | } |
25 | 31 | |
@@ -32,64 +38,82 @@ discard block |
||
32 | 38 | foreach ($fields as $key => $value) { |
33 | 39 | if (is_numeric($key) && is_array($value)) { |
34 | 40 | $result = $this->createSql($value, $key); |
35 | - foreach ($result['args'] as $arg_key => $arg) $args[$arg_key] = $arg; |
|
36 | - foreach ($result['sql'] as $arg) $sql[] = $arg; |
|
37 | - } |
|
38 | - else if (\Maphper\Maphper::FIND_BETWEEN & $mode) { |
|
41 | + foreach ($result['args'] as $arg_key => $arg) { |
|
42 | + $args[$arg_key] = $arg; |
|
43 | + } |
|
44 | + foreach ($result['sql'] as $arg) { |
|
45 | + $sql[] = $arg; |
|
46 | + } |
|
47 | + } else if (\Maphper\Maphper::FIND_BETWEEN & $mode) { |
|
39 | 48 | $sql[] = $key . '>= :' . $key . 'from'; |
40 | 49 | $sql[] = $key . ' <= :' . $key . 'to'; |
41 | 50 | |
42 | 51 | $args[$key . 'from'] = $value[0]; |
43 | 52 | $args[$key . 'to'] = $value[1]; |
44 | - } |
|
45 | - else if (!is_numeric($key) && is_array($value)) { |
|
53 | + } else if (!is_numeric($key) && is_array($value)) { |
|
46 | 54 | $inSql = []; |
47 | 55 | $count = count($value); |
48 | 56 | for ($i = 0; $i < $count; $i++) { |
49 | 57 | $args[$key . $i] = $value[$i]; |
50 | 58 | $inSql[] = ':' . $key . $i; |
51 | 59 | } |
52 | - if (count($inSql) == 0) return []; |
|
53 | - else $sql[] = $key . ' IN ( ' . implode(', ', $inSql) . ')'; |
|
54 | - } |
|
55 | - else if ($value === NULL) { |
|
60 | + if (count($inSql) == 0) { |
|
61 | + return []; |
|
62 | + } else { |
|
63 | + $sql[] = $key . ' IN ( ' . implode(', ', $inSql) . ')'; |
|
64 | + } |
|
65 | + } else if ($value === NULL) { |
|
56 | 66 | $nullSql = $key . ' IS '; |
57 | - if (\Maphper\Maphper::FIND_NOT & $mode) $nullSql .= 'NOT '; |
|
67 | + if (\Maphper\Maphper::FIND_NOT & $mode) { |
|
68 | + $nullSql .= 'NOT '; |
|
69 | + } |
|
58 | 70 | $sql[] = $nullSql . 'NULL'; |
59 | - } |
|
60 | - else { |
|
71 | + } else { |
|
61 | 72 | |
62 | 73 | if (\Maphper\Maphper::FIND_LIKE & $mode) { |
63 | 74 | $operator = 'LIKE'; |
64 | 75 | $value = '%' . $value . '%'; |
65 | - } |
|
66 | - else if (\Maphper\Maphper::FIND_STARTS & $mode) { |
|
76 | + } else if (\Maphper\Maphper::FIND_STARTS & $mode) { |
|
67 | 77 | $operator = 'LIKE'; |
68 | 78 | $value = $value . '%'; |
79 | + } else { |
|
80 | + $operator = $this->getOperator($mode); |
|
69 | 81 | } |
70 | - else $operator = $this->getOperator($mode); |
|
71 | 82 | |
72 | 83 | $args[$key] = $value; |
73 | 84 | $sql[] = $key . ' ' . $operator . ' :' . $key; |
74 | 85 | } |
75 | 86 | } |
76 | 87 | |
77 | - if (\Maphper\Maphper::FIND_OR & $mode) $query = implode(' OR ', $sql); |
|
78 | - else $query = implode(' AND ', $sql); |
|
79 | - if (!empty($query)) $query = '(' . $query . ')'; |
|
88 | + if (\Maphper\Maphper::FIND_OR & $mode) { |
|
89 | + $query = implode(' OR ', $sql); |
|
90 | + } else { |
|
91 | + $query = implode(' AND ', $sql); |
|
92 | + } |
|
93 | + if (!empty($query)) { |
|
94 | + $query = '(' . $query . ')'; |
|
95 | + } |
|
80 | 96 | return ['args' => $args, 'sql' => [$query]]; |
81 | 97 | } |
82 | 98 | |
83 | 99 | private function getOperator($mode) { |
84 | 100 | $operator = ""; |
85 | 101 | |
86 | - if (\Maphper\Maphper::FIND_NOCASE & $mode) $operator = 'LIKE'; |
|
87 | - else if (\Maphper\Maphper::FIND_BIT & $mode) $operator = '&'; |
|
88 | - else if (\Maphper\Maphper::FIND_GREATER & $mode) $operator = '>'; |
|
89 | - else if (\Maphper\Maphper::FIND_LESS & $mode) $operator = '<'; |
|
90 | - else if (\Maphper\Maphper::FIND_NOT & $mode) $operator = '!='; |
|
102 | + if (\Maphper\Maphper::FIND_NOCASE & $mode) { |
|
103 | + $operator = 'LIKE'; |
|
104 | + } else if (\Maphper\Maphper::FIND_BIT & $mode) { |
|
105 | + $operator = '&'; |
|
106 | + } else if (\Maphper\Maphper::FIND_GREATER & $mode) { |
|
107 | + $operator = '>'; |
|
108 | + } else if (\Maphper\Maphper::FIND_LESS & $mode) { |
|
109 | + $operator = '<'; |
|
110 | + } else if (\Maphper\Maphper::FIND_NOT & $mode) { |
|
111 | + $operator = '!='; |
|
112 | + } |
|
91 | 113 | |
92 | - if (\Maphper\Maphper::FIND_EXACT & $mode) $operator .= '='; |
|
114 | + if (\Maphper\Maphper::FIND_EXACT & $mode) { |
|
115 | + $operator .= '='; |
|
116 | + } |
|
93 | 117 | |
94 | 118 | return $operator; |
95 | 119 | } |
@@ -7,7 +7,7 @@ |
||
7 | 7 | public function __construct() { |
8 | 8 | $this->readClosure = function() { |
9 | 9 | $data = new \stdClass; |
10 | - foreach ($this as $k => $v) { |
|
10 | + foreach ($this as $k => $v) { |
|
11 | 11 | if (is_scalar($v) || is_null($v) || (is_object($v) && $v instanceof \DateTime)) $data->$k = $v; |
12 | 12 | } |
13 | 13 | return $data; |
@@ -8,7 +8,9 @@ discard block |
||
8 | 8 | $this->readClosure = function() { |
9 | 9 | $data = new \stdClass; |
10 | 10 | foreach ($this as $k => $v) { |
11 | - if (is_scalar($v) || is_null($v) || (is_object($v) && $v instanceof \DateTime)) $data->$k = $v; |
|
11 | + if (is_scalar($v) || is_null($v) || (is_object($v) && $v instanceof \DateTime)) { |
|
12 | + $data->$k = $v; |
|
13 | + } |
|
12 | 14 | } |
13 | 15 | return $data; |
14 | 16 | }; |
@@ -16,7 +18,9 @@ discard block |
||
16 | 18 | |
17 | 19 | public function getProperties($obj) { |
18 | 20 | $reflect = new \Reflectionclass($obj); |
19 | - if ($reflect->isInternal()) return $obj; |
|
21 | + if ($reflect->isInternal()) { |
|
22 | + return $obj; |
|
23 | + } |
|
20 | 24 | $read = $this->readClosure->bindTo($obj, $obj); |
21 | 25 | return $read(); |
22 | 26 | } |
@@ -19,11 +19,11 @@ discard block |
||
19 | 19 | //For dates with times set, search on time, if the time is not set, search on date only. |
20 | 20 | //E.g. searching for all records posted on '2015-11-14' should return all records that day, not just the ones posted at 00:00:00 on that day |
21 | 21 | if ($value instanceof \DateTime) { |
22 | - if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
22 | + if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
23 | 23 | else $value = $value->format('Y-m-d H:i:s'); |
24 | 24 | } |
25 | 25 | if (is_object($value)) continue; |
26 | - if ($prependField){ |
|
26 | + if ($prependField) { |
|
27 | 27 | $sql[] = $this->quote($field) . ' = :' . $field; |
28 | 28 | } else { |
29 | 29 | $sql[] = ':' . $field; |
@@ -35,13 +35,13 @@ discard block |
||
35 | 35 | |
36 | 36 | public function insert($table, $data) { |
37 | 37 | $query = $this->buildSaveQuery($data); |
38 | - return new Query('INSERT INTO ' . $this->quote($table) . ' (' .implode(', ', array_keys($query['args'])).') VALUES ( ' . implode(', ', $query['sql']). ' )', $query['args']); |
|
38 | + return new Query('INSERT INTO ' . $this->quote($table) . ' (' . implode(', ', array_keys($query['args'])) . ') VALUES ( ' . implode(', ', $query['sql']) . ' )', $query['args']); |
|
39 | 39 | } |
40 | 40 | |
41 | 41 | public function update($table, array $primaryKey, $data) { |
42 | 42 | $query = $this->buildSaveQuery($data, true); |
43 | 43 | $where = []; |
44 | - foreach($primaryKey as $field) $where[] = $this->quote($field) . ' = :' . $field; |
|
45 | - return new Query('UPDATE ' . $this->quote($table) . ' SET ' . implode(', ', $query['sql']). ' WHERE '. implode(' AND ', $where), $query['args']); |
|
44 | + foreach ($primaryKey as $field) $where[] = $this->quote($field) . ' = :' . $field; |
|
45 | + return new Query('UPDATE ' . $this->quote($table) . ' SET ' . implode(', ', $query['sql']) . ' WHERE ' . implode(' AND ', $where), $query['args']); |
|
46 | 46 | } |
47 | 47 | } |
@@ -19,10 +19,15 @@ discard block |
||
19 | 19 | //For dates with times set, search on time, if the time is not set, search on date only. |
20 | 20 | //E.g. searching for all records posted on '2015-11-14' should return all records that day, not just the ones posted at 00:00:00 on that day |
21 | 21 | if ($value instanceof \DateTime) { |
22 | - if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
23 | - else $value = $value->format('Y-m-d H:i:s'); |
|
22 | + if ($value->format('H:i:s') == '00:00:00') { |
|
23 | + $value = $value->format('Y-m-d'); |
|
24 | + } else { |
|
25 | + $value = $value->format('Y-m-d H:i:s'); |
|
26 | + } |
|
27 | + } |
|
28 | + if (is_object($value)) { |
|
29 | + continue; |
|
24 | 30 | } |
25 | - if (is_object($value)) continue; |
|
26 | 31 | if ($prependField){ |
27 | 32 | $sql[] = $this->quote($field) . ' = :' . $field; |
28 | 33 | } else { |
@@ -41,7 +46,9 @@ discard block |
||
41 | 46 | public function update($table, array $primaryKey, $data) { |
42 | 47 | $query = $this->buildSaveQuery($data, true); |
43 | 48 | $where = []; |
44 | - foreach($primaryKey as $field) $where[] = $this->quote($field) . ' = :' . $field; |
|
49 | + foreach($primaryKey as $field) { |
|
50 | + $where[] = $this->quote($field) . ' = :' . $field; |
|
51 | + } |
|
45 | 52 | return new Query('UPDATE ' . $this->quote($table) . ' SET ' . implode(', ', $query['sql']). ' WHERE '. implode(' AND ', $where), $query['args']); |
46 | 53 | } |
47 | 54 | } |