@@ -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 | } |
@@ -8,7 +8,7 @@ |
||
8 | 8 | public function delete($table, $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 ' . ($criteria ?: '1 = 1 ') . $order . $limit . $offset, $args); |
13 | 13 | } |
14 | 14 |
@@ -24,8 +24,8 @@ discard block |
||
24 | 24 | } |
25 | 25 | |
26 | 26 | public function offsetGet($key) { |
27 | - $depth = $this->getDepth()+1; |
|
28 | - if (count($this->primaryKey)-1 == $depth) return $this->mapper->filter([$this->primaryKey[$depth] => $key])->item(0); |
|
27 | + $depth = $this->getDepth() + 1; |
|
28 | + if (count($this->primaryKey) - 1 == $depth) return $this->mapper->filter([$this->primaryKey[$depth] => $key])->item(0); |
|
29 | 29 | else return new MultiPk($this->mapper, $key, $this->primaryKey, $this); |
30 | 30 | } |
31 | 31 | |
@@ -44,7 +44,7 @@ discard block |
||
44 | 44 | |
45 | 45 | public function offsetUnset($key) { |
46 | 46 | $keys = $this->primaryKey; |
47 | - $this->mapper->filter([ array_pop($keys) => $key])->delete(); |
|
47 | + $this->mapper->filter([array_pop($keys) => $key])->delete(); |
|
48 | 48 | } |
49 | 49 | |
50 | 50 | public function offsetExists($key) { |
@@ -4,7 +4,7 @@ |
||
4 | 4 | private $parent; |
5 | 5 | private $primaryKey; |
6 | 6 | private $lookup; |
7 | - private $mapper; |
|
7 | + private $mapper; |
|
8 | 8 | |
9 | 9 | public function __construct(Maphper $mapper, $lookup, array $primaryKey, MultiPk $parent = null) { |
10 | 10 | $this->parent = $parent; |
@@ -37,7 +37,7 @@ discard block |
||
37 | 37 | //bit hacky, breaking encapsulation, but simplest way to work out the info for the other side of the many:many relationship. |
38 | 38 | private function getOtherFieldNameInfo() { |
39 | 39 | if ($this->otherInfo == null) { |
40 | - $propertyReader = function($name) {return $this->$name; }; |
|
40 | + $propertyReader = function($name) {return $this->$name; }; |
|
41 | 41 | |
42 | 42 | $reader = $propertyReader->bindTo($this->intermediateMapper, $this->intermediateMapper); |
43 | 43 | |
@@ -45,7 +45,7 @@ discard block |
||
45 | 45 | $propertyReader = $propertyReader->bindTo($relation, $relation); |
46 | 46 | if ($propertyReader('parentField') != $this->parentField) { |
47 | 47 | $relation = $relation->getData($this->object); |
48 | - $this->otherInfo = [$propertyReader('localField'), $propertyReader('parentField'), $propertyReader('mapper')]; |
|
48 | + $this->otherInfo = [$propertyReader('localField'), $propertyReader('parentField'), $propertyReader('mapper')]; |
|
49 | 49 | } |
50 | 50 | } |
51 | 51 | } |
@@ -88,7 +88,7 @@ discard block |
||
88 | 88 | list($relatedField, $valueField, $mapper) = $this->getOtherFieldNameInfo(); |
89 | 89 | if ($this->autoTraverse) { |
90 | 90 | $record = new \stdClass; |
91 | - $record->{$this->parentField} = $value->{$this->localField}; |
|
91 | + $record->{$this->parentField} = $value->{$this->localField}; |
|
92 | 92 | $record->$valueField = $this->object->{$relatedField}; |
93 | 93 | $this->intermediateMapper[] = $record; |
94 | 94 |
@@ -74,7 +74,7 @@ |
||
74 | 74 | |
75 | 75 | public function __get($name) { |
76 | 76 | if ($this->lazyLoad()) return $this->lazyLoad()->$name; |
77 | - else return null; |
|
77 | + else return null; |
|
78 | 78 | } |
79 | 79 | |
80 | 80 | public function __isset($name) { |
@@ -7,17 +7,17 @@ |
||
7 | 7 | private $intermediateName; |
8 | 8 | |
9 | 9 | public function __construct(\Maphper\Iterator $iterator, $intermediateName = null) { |
10 | - $this->iterator = $iterator; |
|
11 | - $this->intermediateName = $intermediateName; |
|
10 | + $this->iterator = $iterator; |
|
11 | + $this->intermediateName = $intermediateName; |
|
12 | 12 | } |
13 | 13 | |
14 | 14 | public function current() { |
15 | - if ($this->intermediateName) return $this->iterator->current()->{$this->intermediateName}; |
|
15 | + if ($this->intermediateName) return $this->iterator->current()->{$this->intermediateName}; |
|
16 | 16 | return $this->iterator->current(); |
17 | 17 | } |
18 | 18 | |
19 | 19 | public function key() { |
20 | - return $this->iterator->key(); |
|
20 | + return $this->iterator->key(); |
|
21 | 21 | } |
22 | 22 | |
23 | 23 | public function next() { |
@@ -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 = []; |
@@ -57,7 +57,7 @@ discard block |
||
57 | 57 | } |
58 | 58 | } |
59 | 59 | $results = $this->dataSource->findByField($this->settings['filter'], |
60 | - ['order' => $this->settings['sort'], 'limit' => $this->settings['limit'], 'offset' => $this->settings['offset'] ]); |
|
60 | + ['order' => $this->settings['sort'], 'limit' => $this->settings['limit'], 'offset' => $this->settings['offset']]); |
|
61 | 61 | |
62 | 62 | $siblings = new \ArrayObject(); |
63 | 63 | |
@@ -108,7 +108,7 @@ discard block |
||
108 | 108 | $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
109 | 109 | return isset($data[0]); |
110 | 110 | } |
111 | - return (bool) $this->dataSource->findById($offset); |
|
111 | + return (bool)$this->dataSource->findById($offset); |
|
112 | 112 | } |
113 | 113 | |
114 | 114 | public function offsetUnset($id) { |
@@ -96,15 +96,15 @@ discard block |
||
96 | 96 | $value = $this->entity->wrap($this->relations, $value); |
97 | 97 | $this->dataSource->save($value); |
98 | 98 | $visibilityOverride->write($value); |
99 | - $this->entity->create((array_merge((array)$value, (array)$valueCopy)), $this->relations); |
|
99 | + $this->entity->create((array_merge((array)$value, (array)$valueCopy)), $this->relations); |
|
100 | 100 | } |
101 | 101 | |
102 | 102 | public function offsetExists($offset) { |
103 | 103 | if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
104 | - if (!empty($this->settings['filter'])) { |
|
105 | - $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
106 | - return isset($data[0]); |
|
107 | - } |
|
104 | + if (!empty($this->settings['filter'])) { |
|
105 | + $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
106 | + return isset($data[0]); |
|
107 | + } |
|
108 | 108 | return (bool) $this->dataSource->findById($offset); |
109 | 109 | } |
110 | 110 | |
@@ -114,10 +114,10 @@ discard block |
||
114 | 114 | |
115 | 115 | public function offsetGet($offset) { |
116 | 116 | if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
117 | - if (!empty($this->settings['filter'])) { |
|
118 | - $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
119 | - return $this->entity->create(isset($data[0]) ? $data[0] : null, $this->relations); |
|
120 | - } |
|
117 | + if (!empty($this->settings['filter'])) { |
|
118 | + $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
119 | + return $this->entity->create(isset($data[0]) ? $data[0] : null, $this->relations); |
|
120 | + } |
|
121 | 121 | return $this->entity->create($this->dataSource->findById($offset), $this->relations); |
122 | 122 | } |
123 | 123 |
@@ -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 |
@@ -1,14 +1,14 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | namespace Maphper\DataSource; |
3 | 3 | class StmtCache { |
4 | - private $pdo; |
|
5 | - private $queryCache = []; |
|
4 | + private $pdo; |
|
5 | + private $queryCache = []; |
|
6 | 6 | |
7 | - public function __construct(\PDO $pdo) { |
|
8 | - $this->pdo = $pdo; |
|
9 | - } |
|
7 | + public function __construct(\PDO $pdo) { |
|
8 | + $this->pdo = $pdo; |
|
9 | + } |
|
10 | 10 | |
11 | - public function getCachedStmt($sql) { |
|
11 | + public function getCachedStmt($sql) { |
|
12 | 12 | $queryId = $this->getQueryId($sql); |
13 | 13 | if (isset($this->queryCache[$queryId])) $stmt = $this->queryCache[$queryId]; |
14 | 14 | else { |
@@ -18,15 +18,15 @@ discard block |
||
18 | 18 | return $stmt; |
19 | 19 | } |
20 | 20 | |
21 | - private function getQueryId($sql) { |
|
22 | - return md5($sql); |
|
23 | - } |
|
21 | + private function getQueryId($sql) { |
|
22 | + return md5($sql); |
|
23 | + } |
|
24 | 24 | |
25 | - public function deleteQueryFromCache($sql) { |
|
26 | - unset($this->queryCache[$this->getQueryId($sql)]); |
|
27 | - } |
|
25 | + public function deleteQueryFromCache($sql) { |
|
26 | + unset($this->queryCache[$this->getQueryId($sql)]); |
|
27 | + } |
|
28 | 28 | |
29 | - public function clearCache() { |
|
30 | - $this->queryCache = []; |
|
31 | - } |
|
29 | + public function clearCache() { |
|
30 | + $this->queryCache = []; |
|
31 | + } |
|
32 | 32 | } |
@@ -94,7 +94,7 @@ |
||
94 | 94 | } |
95 | 95 | |
96 | 96 | public function updateCache($data, $pkValue) { |
97 | - if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
97 | + if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object)array_merge((array)$this->cache[$pkValue], (array)$data); |
|
98 | 98 | else $this->cache[$pkValue] = $data; |
99 | 99 | } |
100 | 100 |
@@ -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 | } |