@@ -3,14 +3,14 @@ discard block |
||
3 | 3 | class MysqlAdapter implements DatabaseAdapter { |
4 | 4 | private $pdo; |
5 | 5 | private $stmtCache; |
6 | - private $generalEditor; |
|
6 | + private $generalEditor; |
|
7 | 7 | |
8 | 8 | public function __construct(\PDO $pdo) { |
9 | 9 | $this->pdo = $pdo; |
10 | 10 | //Set to strict mode to detect 'out of range' errors, action at a distance but it needs to be set for all INSERT queries |
11 | 11 | $this->pdo->query('SET sql_mode = STRICT_ALL_TABLES'); |
12 | - $this->stmtCache = new StmtCache($pdo); |
|
13 | - $this->generalEditor = new GeneralEditDatabase($this->pdo, ['short_string_max_len' => 191]); |
|
12 | + $this->stmtCache = new StmtCache($pdo); |
|
13 | + $this->generalEditor = new GeneralEditDatabase($this->pdo, ['short_string_max_len' => 191]); |
|
14 | 14 | } |
15 | 15 | |
16 | 16 | public function quote($str) { |
@@ -20,32 +20,32 @@ discard block |
||
20 | 20 | public function query(\Maphper\Lib\Query $query) { |
21 | 21 | $stmt = $this->stmtCache->getCachedStmt($query->getSql()); |
22 | 22 | $args = $query->getArgs(); |
23 | - $stmt->execute($args); |
|
23 | + $stmt->execute($args); |
|
24 | 24 | |
25 | 25 | return $stmt; |
26 | 26 | } |
27 | 27 | |
28 | - private function alterColumns($table, array $primaryKey, $data) { |
|
29 | - foreach ($data as $key => $value) { |
|
28 | + private function alterColumns($table, array $primaryKey, $data) { |
|
29 | + foreach ($data as $key => $value) { |
|
30 | 30 | if ($this->generalEditor->isNotSavableType($value, $key, $primaryKey)) continue; |
31 | 31 | |
32 | 32 | $type = $this->generalEditor->getType($value); |
33 | 33 | $this->tryAlteringColumn($table, $key, $type); |
34 | 34 | } |
35 | - } |
|
35 | + } |
|
36 | 36 | |
37 | - private function tryAlteringColumn($table, $key, $type) { |
|
38 | - try { |
|
39 | - if (!$this->pdo->query('ALTER TABLE ' . $table . ' ADD ' . $this->quote($key) . ' ' . $type)) throw new \Exception('Could not alter table'); |
|
40 | - } |
|
41 | - catch (\Exception $e) { |
|
42 | - $this->pdo->query('ALTER TABLE ' . $table . ' MODIFY ' . $this->quote($key) . ' ' . $type); |
|
43 | - } |
|
44 | - } |
|
37 | + private function tryAlteringColumn($table, $key, $type) { |
|
38 | + try { |
|
39 | + if (!$this->pdo->query('ALTER TABLE ' . $table . ' ADD ' . $this->quote($key) . ' ' . $type)) throw new \Exception('Could not alter table'); |
|
40 | + } |
|
41 | + catch (\Exception $e) { |
|
42 | + $this->pdo->query('ALTER TABLE ' . $table . ' MODIFY ' . $this->quote($key) . ' ' . $type); |
|
43 | + } |
|
44 | + } |
|
45 | 45 | |
46 | 46 | public function alterDatabase($table, array $primaryKey, $data) { |
47 | 47 | $this->generalEditor->createTable($table, $primaryKey, $data); |
48 | - $this->alterColumns($table, $primaryKey, $data); |
|
48 | + $this->alterColumns($table, $primaryKey, $data); |
|
49 | 49 | } |
50 | 50 | |
51 | 51 | public function lastInsertId() { |
@@ -27,7 +27,9 @@ discard block |
||
27 | 27 | |
28 | 28 | private function alterColumns($table, array $primaryKey, $data) { |
29 | 29 | foreach ($data as $key => $value) { |
30 | - if ($this->generalEditor->isNotSavableType($value, $key, $primaryKey)) continue; |
|
30 | + if ($this->generalEditor->isNotSavableType($value, $key, $primaryKey)) { |
|
31 | + continue; |
|
32 | + } |
|
31 | 33 | |
32 | 34 | $type = $this->generalEditor->getType($value); |
33 | 35 | $this->tryAlteringColumn($table, $key, $type); |
@@ -36,9 +38,10 @@ discard block |
||
36 | 38 | |
37 | 39 | private function tryAlteringColumn($table, $key, $type) { |
38 | 40 | try { |
39 | - if (!$this->pdo->query('ALTER TABLE ' . $table . ' ADD ' . $this->quote($key) . ' ' . $type)) throw new \Exception('Could not alter table'); |
|
40 | - } |
|
41 | - catch (\Exception $e) { |
|
41 | + if (!$this->pdo->query('ALTER TABLE ' . $table . ' ADD ' . $this->quote($key) . ' ' . $type)) { |
|
42 | + throw new \Exception('Could not alter table'); |
|
43 | + } |
|
44 | + } catch (\Exception $e) { |
|
42 | 45 | $this->pdo->query('ALTER TABLE ' . $table . ' MODIFY ' . $this->quote($key) . ' ' . $type); |
43 | 46 | } |
44 | 47 | } |
@@ -60,7 +63,9 @@ discard block |
||
60 | 63 | $keyName = $this->quote(implode('_', $fields)); |
61 | 64 | |
62 | 65 | $results = $this->pdo->query('SHOW INDEX FROM ' . $this->quote($table) . ' WHERE Key_Name = "' . $keyName . '"'); |
63 | - if ($results && count($results->fetchAll()) == 0) $this->pdo->query('CREATE INDEX ' . $keyName . ' ON ' . $this->quote($table) . ' (' . implode(', ', $fields) . ')'); |
|
66 | + if ($results && count($results->fetchAll()) == 0) { |
|
67 | + $this->pdo->query('CREATE INDEX ' . $keyName . ' ON ' . $this->quote($table) . ' (' . implode(', ', $fields) . ')'); |
|
68 | + } |
|
64 | 69 | } |
65 | 70 | |
66 | 71 | public function optimiseColumns($table) { |
@@ -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) { |
@@ -83,21 +83,21 @@ discard block |
||
83 | 83 | } |
84 | 84 | |
85 | 85 | public function offsetSet($offset, $valueObj) { |
86 | - if ($valueObj instanceof \Maphper\Relation) throw new \Exception(); |
|
86 | + if ($valueObj instanceof \Maphper\Relation) throw new \Exception(); |
|
87 | 87 | |
88 | - //Extract private properties from the object |
|
89 | - $visibilityOverride = new \Maphper\Lib\VisibilityOverride($valueObj); |
|
90 | - $value = $visibilityOverride->getProperties($valueObj); |
|
88 | + //Extract private properties from the object |
|
89 | + $visibilityOverride = new \Maphper\Lib\VisibilityOverride($valueObj); |
|
90 | + $value = $visibilityOverride->getProperties($valueObj); |
|
91 | 91 | |
92 | - $value = $this->processFilters($value); |
|
93 | - $pk = $this->dataSource->getPrimaryKey(); |
|
94 | - if ($offset !== null) $value->{$pk[0]} = $offset; |
|
95 | - $valueCopy = $this->removeRelations(clone $value, $pk); |
|
96 | - $value = $this->entity->wrap($this->relations, $value); |
|
97 | - $this->dataSource->save($value); |
|
98 | - $visibilityOverride->write($value); |
|
99 | - $value = $this->entity->create((array_merge((array)$value, (array)$valueCopy)), $this->relations); |
|
100 | - $visibilityOverride->write($value); |
|
92 | + $value = $this->processFilters($value); |
|
93 | + $pk = $this->dataSource->getPrimaryKey(); |
|
94 | + if ($offset !== null) $value->{$pk[0]} = $offset; |
|
95 | + $valueCopy = $this->removeRelations(clone $value, $pk); |
|
96 | + $value = $this->entity->wrap($this->relations, $value); |
|
97 | + $this->dataSource->save($value); |
|
98 | + $visibilityOverride->write($value); |
|
99 | + $value = $this->entity->create((array_merge((array)$value, (array)$valueCopy)), $this->relations); |
|
100 | + $visibilityOverride->write($value); |
|
101 | 101 | } |
102 | 102 | |
103 | 103 | private function removeRelations($obj, $pk) { // Prevent saving ManyMany twice except when pk isn't initially set |
@@ -110,10 +110,10 @@ discard block |
||
110 | 110 | |
111 | 111 | public function offsetExists($offset) { |
112 | 112 | if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
113 | - if (!empty($this->settings['filter'])) { |
|
114 | - $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
115 | - return isset($data[0]); |
|
116 | - } |
|
113 | + if (!empty($this->settings['filter'])) { |
|
114 | + $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
115 | + return isset($data[0]); |
|
116 | + } |
|
117 | 117 | return (bool) $this->dataSource->findById($offset); |
118 | 118 | } |
119 | 119 | |
@@ -123,10 +123,10 @@ discard block |
||
123 | 123 | |
124 | 124 | public function offsetGet($offset) { |
125 | 125 | if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
126 | - if (!empty($this->settings['filter'])) { |
|
127 | - $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
128 | - return $this->entity->create(isset($data[0]) ? $data[0] : null, $this->relations); |
|
129 | - } |
|
126 | + if (!empty($this->settings['filter'])) { |
|
127 | + $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
128 | + return $this->entity->create(isset($data[0]) ? $data[0] : null, $this->relations); |
|
129 | + } |
|
130 | 130 | return $this->entity->create($this->dataSource->findById($offset), $this->relations); |
131 | 131 | } |
132 | 132 |
@@ -60,7 +60,9 @@ discard block |
||
60 | 60 | |
61 | 61 | $siblings = new \ArrayObject(); |
62 | 62 | |
63 | - foreach ($results as &$result) $result = $this->entity->create($result, $this->relations, $siblings); |
|
63 | + foreach ($results as &$result) { |
|
64 | + $result = $this->entity->create($result, $this->relations, $siblings); |
|
65 | + } |
|
64 | 66 | |
65 | 67 | return $results; |
66 | 68 | } |
@@ -77,13 +79,17 @@ discard block |
||
77 | 79 | private function processFilters($value) { |
78 | 80 | //When saving to a mapper with filters, write the filters back into the object being stored |
79 | 81 | foreach ($this->settings['filter'] as $key => $filterValue) { |
80 | - if (empty($value->$key) && !is_array($filterValue)) $value->$key = $filterValue; |
|
82 | + if (empty($value->$key) && !is_array($filterValue)) { |
|
83 | + $value->$key = $filterValue; |
|
84 | + } |
|
81 | 85 | } |
82 | 86 | return $value; |
83 | 87 | } |
84 | 88 | |
85 | 89 | public function offsetSet($offset, $valueObj) { |
86 | - if ($valueObj instanceof \Maphper\Relation) throw new \Exception(); |
|
90 | + if ($valueObj instanceof \Maphper\Relation) { |
|
91 | + throw new \Exception(); |
|
92 | + } |
|
87 | 93 | |
88 | 94 | //Extract private properties from the object |
89 | 95 | $visibilityOverride = new \Maphper\Lib\VisibilityOverride($valueObj); |
@@ -91,7 +97,9 @@ discard block |
||
91 | 97 | |
92 | 98 | $value = $this->processFilters($value); |
93 | 99 | $pk = $this->dataSource->getPrimaryKey(); |
94 | - if ($offset !== null) $value->{$pk[0]} = $offset; |
|
100 | + if ($offset !== null) { |
|
101 | + $value->{$pk[0]} = $offset; |
|
102 | + } |
|
95 | 103 | $valueCopy = $this->removeRelations(clone $value, $pk); |
96 | 104 | $value = $this->entity->wrap($this->relations, $value); |
97 | 105 | $this->dataSource->save($value); |
@@ -101,15 +109,20 @@ discard block |
||
101 | 109 | } |
102 | 110 | |
103 | 111 | private function removeRelations($obj, $pk) { // Prevent saving ManyMany twice except when pk isn't initially set |
104 | - foreach ($this->relations as $name => $relation) |
|
105 | - if ($relation instanceOf \Maphper\Relation\ManyMany && isset($obj->$name) && !empty($obj->{$pk[0]})) unset($obj->$name); |
|
112 | + foreach ($this->relations as $name => $relation) { |
|
113 | + if ($relation instanceOf \Maphper\Relation\ManyMany && isset($obj->$name) && !empty($obj->{$pk[0]})) unset($obj->$name); |
|
114 | + } |
|
106 | 115 | |
107 | - if (empty($obj->{$pk[0]})) unset($obj->{$pk[0]}); |
|
116 | + if (empty($obj->{$pk[0]})) { |
|
117 | + unset($obj->{$pk[0]}); |
|
118 | + } |
|
108 | 119 | return $obj; |
109 | 120 | } |
110 | 121 | |
111 | 122 | public function offsetExists($offset) { |
112 | - 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 | + } |
|
113 | 126 | if (!empty($this->settings['filter'])) { |
114 | 127 | $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
115 | 128 | return isset($data[0]); |
@@ -122,7 +135,9 @@ discard block |
||
122 | 135 | } |
123 | 136 | |
124 | 137 | public function offsetGet($offset) { |
125 | - if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
|
138 | + if (count($this->dataSource->getPrimaryKey()) > 1) { |
|
139 | + return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
|
140 | + } |
|
126 | 141 | if (!empty($this->settings['filter'])) { |
127 | 142 | $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
128 | 143 | return $this->entity->create(isset($data[0]) ? $data[0] : null, $this->relations); |
@@ -133,11 +148,15 @@ discard block |
||
133 | 148 | public function __call($method, $args) { |
134 | 149 | if (array_key_exists($method, $this->settings)) { |
135 | 150 | $maphper = new Maphper($this->dataSource, $this->settings, $this->relations); |
136 | - if (is_array($maphper->settings[$method])) $maphper->settings[$method] = $args[0] + $maphper->settings[$method]; |
|
137 | - else $maphper->settings[$method] = $args[0]; |
|
151 | + if (is_array($maphper->settings[$method])) { |
|
152 | + $maphper->settings[$method] = $args[0] + $maphper->settings[$method]; |
|
153 | + } else { |
|
154 | + $maphper->settings[$method] = $args[0]; |
|
155 | + } |
|
138 | 156 | return $maphper; |
157 | + } else { |
|
158 | + throw new \Exception('Method Maphper::' . $method . ' does not exist'); |
|
139 | 159 | } |
140 | - else throw new \Exception('Method Maphper::' . $method . ' does not exist'); |
|
141 | 160 | } |
142 | 161 | |
143 | 162 | public function findAggregate($function, $field, $group = null) { |
@@ -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; |
@@ -7,17 +7,23 @@ |
||
7 | 7 | |
8 | 8 | if (isset($options['offset'])) { |
9 | 9 | $offset = ' OFFSET ' . $options['offset']; |
10 | - if (!$limit) $limit = ' LIMIT 1000'; |
|
10 | + if (!$limit) { |
|
11 | + $limit = ' LIMIT 1000'; |
|
12 | + } |
|
13 | + } else { |
|
14 | + $offset = ''; |
|
11 | 15 | } |
12 | - else $offset = ''; |
|
13 | 16 | |
14 | 17 | $order = isset($options['order']) ? ' ORDER BY ' . $options['order'] : ''; |
15 | 18 | return new Query('SELECT * FROM ' . $table . ' ' . $where . $order . $limit . $offset, $args); |
16 | 19 | } |
17 | 20 | |
18 | 21 | public function aggregate($table, $function, $field, $where, $args, $group) { |
19 | - if ($group == true) $groupBy = ' GROUP BY ' . $field; |
|
20 | - else $groupBy = ''; |
|
22 | + if ($group == true) { |
|
23 | + $groupBy = ' GROUP BY ' . $field; |
|
24 | + } else { |
|
25 | + $groupBy = ''; |
|
26 | + } |
|
21 | 27 | return new Query('SELECT ' . $function . '(' . $field . ') as val, ' . $field . ' FROM ' . $table . ($where != null ? ' WHERE ' . $where : '') . ' ' . $groupBy, $args); |
22 | 28 | } |
23 | 29 | } |
@@ -11,9 +11,9 @@ discard block |
||
11 | 11 | $this->writeClosure = function ($field, $value) use ($object) { $object->$field = $value; }; |
12 | 12 | } |
13 | 13 | else { |
14 | - $visOverride = $this; |
|
14 | + $visOverride = $this; |
|
15 | 15 | $this->readClosure = function() use ($visOverride) { |
16 | - return (object) array_filter(get_object_vars($this), [$visOverride, 'isReturnableDataType']); |
|
16 | + return (object) array_filter(get_object_vars($this), [$visOverride, 'isReturnableDataType']); |
|
17 | 17 | }; |
18 | 18 | $this->readClosure = $this->readClosure->bindTo($object, $object); |
19 | 19 | |
@@ -23,9 +23,9 @@ discard block |
||
23 | 23 | |
24 | 24 | } |
25 | 25 | |
26 | - public function isReturnableDataType($v) { |
|
27 | - return is_scalar($v) || is_null($v) || (is_object($v) && $v instanceof \DateTime); |
|
28 | - } |
|
26 | + public function isReturnableDataType($v) { |
|
27 | + return is_scalar($v) || is_null($v) || (is_object($v) && $v instanceof \DateTime); |
|
28 | + } |
|
29 | 29 | |
30 | 30 | public function getProperties() { |
31 | 31 | return ($this->readClosure)(); |
@@ -7,17 +7,17 @@ discard block |
||
7 | 7 | |
8 | 8 | public function __construct($object) { |
9 | 9 | if ($object instanceof \stdclass) { |
10 | - $this->readClosure = function() use ($object) { return $object; }; |
|
11 | - $this->writeClosure = function ($field, $value) use ($object) { $object->$field = $value; }; |
|
10 | + $this->readClosure = function() use ($object) { return $object; }; |
|
11 | + $this->writeClosure = function($field, $value) use ($object) { $object->$field = $value; }; |
|
12 | 12 | } |
13 | 13 | else { |
14 | 14 | $visOverride = $this; |
15 | 15 | $this->readClosure = function() use ($visOverride) { |
16 | - return (object) array_filter(get_object_vars($this), [$visOverride, 'isReturnableDataType']); |
|
16 | + return (object)array_filter(get_object_vars($this), [$visOverride, 'isReturnableDataType']); |
|
17 | 17 | }; |
18 | 18 | $this->readClosure = $this->readClosure->bindTo($object, $object); |
19 | 19 | |
20 | - $this->writeClosure = function ($field, $value) { $this->$field = $value; }; |
|
20 | + $this->writeClosure = function($field, $value) { $this->$field = $value; }; |
|
21 | 21 | $this->writeClosure = $this->writeClosure->bindTo($object, $object); |
22 | 22 | } |
23 | 23 | |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | public function write($data) { |
35 | 35 | if ($data != null) { |
36 | 36 | foreach ($data as $key => $value) { |
37 | - ($this->writeClosure)($key, $this->processDates($value)); |
|
37 | + ($this->writeClosure)($key, $this->processDates($value)); |
|
38 | 38 | } |
39 | 39 | } |
40 | 40 | } |
@@ -9,8 +9,7 @@ |
||
9 | 9 | if ($object instanceof \stdclass) { |
10 | 10 | $this->readClosure = function() use ($object) { return $object; }; |
11 | 11 | $this->writeClosure = function ($field, $value) use ($object) { $object->$field = $value; }; |
12 | - } |
|
13 | - else { |
|
12 | + } else { |
|
14 | 13 | $visOverride = $this; |
15 | 14 | $this->readClosure = function() use ($visOverride) { |
16 | 15 | return (object) array_filter(get_object_vars($this), [$visOverride, 'isReturnableDataType']); |
@@ -8,7 +8,7 @@ discard block |
||
8 | 8 | private $parent; |
9 | 9 | |
10 | 10 | public function __construct(\Maphper\Maphper $parent, $className = null) { |
11 | - $this->parent = $parent; |
|
11 | + $this->parent = $parent; |
|
12 | 12 | $this->className = $className; |
13 | 13 | } |
14 | 14 | |
@@ -25,12 +25,12 @@ discard block |
||
25 | 25 | return $object; |
26 | 26 | } |
27 | 27 | |
28 | - private function addRelationData($object, $name, $relation, $siblings) { |
|
29 | - if (isset($object->$name) && !($object->$name instanceof \Maphper\Relation) ) { |
|
30 | - //After overwriting the relation, does the parent object ($object) need overwriting as well? |
|
31 | - if ($relation->overwrite($object, $object->$name)) $this->parent[] = $object; |
|
32 | - } |
|
28 | + private function addRelationData($object, $name, $relation, $siblings) { |
|
29 | + if (isset($object->$name) && !($object->$name instanceof \Maphper\Relation) ) { |
|
30 | + //After overwriting the relation, does the parent object ($object) need overwriting as well? |
|
31 | + if ($relation->overwrite($object, $object->$name)) $this->parent[] = $object; |
|
32 | + } |
|
33 | 33 | |
34 | - $object->$name = $relation->getData($object, $siblings); |
|
35 | - } |
|
34 | + $object->$name = $relation->getData($object, $siblings); |
|
35 | + } |
|
36 | 36 | } |
@@ -26,7 +26,7 @@ |
||
26 | 26 | } |
27 | 27 | |
28 | 28 | private function addRelationData($object, $name, $relation, $siblings) { |
29 | - if (isset($object->$name) && !($object->$name instanceof \Maphper\Relation) ) { |
|
29 | + if (isset($object->$name) && !($object->$name instanceof \Maphper\Relation)) { |
|
30 | 30 | //After overwriting the relation, does the parent object ($object) need overwriting as well? |
31 | 31 | if ($relation->overwrite($object, $object->$name)) $this->parent[] = $object; |
32 | 32 | } |
@@ -21,14 +21,18 @@ |
||
21 | 21 | |
22 | 22 | public function wrap($relations, $object, $siblings = []) { |
23 | 23 | //see if any relations need overwriting |
24 | - foreach ($relations as $name => $relation) $this->addRelationData($object, $name, $relation, $siblings); |
|
24 | + foreach ($relations as $name => $relation) { |
|
25 | + $this->addRelationData($object, $name, $relation, $siblings); |
|
26 | + } |
|
25 | 27 | return $object; |
26 | 28 | } |
27 | 29 | |
28 | 30 | private function addRelationData($object, $name, $relation, $siblings) { |
29 | 31 | if (isset($object->$name) && !($object->$name instanceof \Maphper\Relation) ) { |
30 | 32 | //After overwriting the relation, does the parent object ($object) need overwriting as well? |
31 | - if ($relation->overwrite($object, $object->$name)) $this->parent[] = $object; |
|
33 | + if ($relation->overwrite($object, $object->$name)) { |
|
34 | + $this->parent[] = $object; |
|
35 | + } |
|
32 | 36 | } |
33 | 37 | |
34 | 38 | $object->$name = $relation->getData($object, $siblings); |
@@ -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 |
@@ -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 | } |
@@ -3,58 +3,58 @@ |
||
3 | 3 | use Maphper\Maphper; |
4 | 4 | |
5 | 5 | class ArrayFilter { |
6 | - private $array; |
|
7 | - |
|
8 | - public function __construct(array $array) { |
|
9 | - $this->array = $array; |
|
10 | - } |
|
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 | - } |
|
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); |
|
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 (bool)(Maphper::FIND_OR ^ $mode); |
|
28 | - }; |
|
29 | - } |
|
30 | - |
|
31 | - private function getIfFieldMatches($key, $val, $data, $mode) { |
|
32 | - if ($this->shouldRunDeepSearch($key, $val)) { |
|
33 | - return $this->getSearchFieldFunction($val, $key)($data); |
|
34 | - } |
|
35 | - else if (!isset($data->$key)) return false; |
|
36 | - else if ($this->isInArraySearch($mode, $key, $val)) |
|
37 | - return in_array($data->$key, $val); |
|
38 | - else |
|
39 | - return $this->processFilter($mode, $val, $data->$key); |
|
40 | - } |
|
41 | - |
|
42 | - private function shouldRunDeepSearch($key, $val) { |
|
43 | - return is_numeric($key) && is_array($val); |
|
44 | - } |
|
45 | - |
|
46 | - private function isInArraySearch($mode, $key, $val) { |
|
47 | - return !(Maphper::FIND_BETWEEN & $mode) && !is_numeric($key) && is_array($val); |
|
48 | - } |
|
49 | - |
|
50 | - private function processFilter($mode, $expected, $actual) { |
|
51 | - if (Maphper::FIND_NOT & $mode) return $expected != $actual; |
|
52 | - else if ((Maphper::FIND_GREATER | Maphper::FIND_EXACT) === $mode) return $expected <= $actual; |
|
53 | - else if ((Maphper::FIND_LESS | Maphper::FIND_EXACT) === $mode) return $expected >= $actual; |
|
54 | - else if (Maphper::FIND_GREATER & $mode) return $expected < $actual; |
|
55 | - else if (Maphper::FIND_LESS & $mode) return $expected > $actual; |
|
56 | - else if (Maphper::FIND_BETWEEN & $mode) return $expected[0] <= $actual && $actual <= $expected[1]; |
|
57 | - else if (Maphper::FIND_NOCASE & $mode) return strtolower($expected) == strtolower($actual); |
|
58 | - return $expected == $actual; |
|
59 | - } |
|
6 | + private $array; |
|
7 | + |
|
8 | + public function __construct(array $array) { |
|
9 | + $this->array = $array; |
|
10 | + } |
|
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 | + } |
|
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); |
|
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 (bool)(Maphper::FIND_OR ^ $mode); |
|
28 | + }; |
|
29 | + } |
|
30 | + |
|
31 | + private function getIfFieldMatches($key, $val, $data, $mode) { |
|
32 | + if ($this->shouldRunDeepSearch($key, $val)) { |
|
33 | + return $this->getSearchFieldFunction($val, $key)($data); |
|
34 | + } |
|
35 | + else if (!isset($data->$key)) return false; |
|
36 | + else if ($this->isInArraySearch($mode, $key, $val)) |
|
37 | + return in_array($data->$key, $val); |
|
38 | + else |
|
39 | + return $this->processFilter($mode, $val, $data->$key); |
|
40 | + } |
|
41 | + |
|
42 | + private function shouldRunDeepSearch($key, $val) { |
|
43 | + return is_numeric($key) && is_array($val); |
|
44 | + } |
|
45 | + |
|
46 | + private function isInArraySearch($mode, $key, $val) { |
|
47 | + return !(Maphper::FIND_BETWEEN & $mode) && !is_numeric($key) && is_array($val); |
|
48 | + } |
|
49 | + |
|
50 | + private function processFilter($mode, $expected, $actual) { |
|
51 | + if (Maphper::FIND_NOT & $mode) return $expected != $actual; |
|
52 | + else if ((Maphper::FIND_GREATER | Maphper::FIND_EXACT) === $mode) return $expected <= $actual; |
|
53 | + else if ((Maphper::FIND_LESS | Maphper::FIND_EXACT) === $mode) return $expected >= $actual; |
|
54 | + else if (Maphper::FIND_GREATER & $mode) return $expected < $actual; |
|
55 | + else if (Maphper::FIND_LESS & $mode) return $expected > $actual; |
|
56 | + else if (Maphper::FIND_BETWEEN & $mode) return $expected[0] <= $actual && $actual <= $expected[1]; |
|
57 | + else if (Maphper::FIND_NOCASE & $mode) return strtolower($expected) == strtolower($actual); |
|
58 | + return $expected == $actual; |
|
59 | + } |
|
60 | 60 | } |
@@ -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 |
@@ -21,8 +21,11 @@ discard block |
||
21 | 21 | foreach ($fields as $key => $val) { |
22 | 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; |
|
24 | + if (Maphper::FIND_OR & $mode && $currentFieldResult === true) { |
|
25 | + return true; |
|
26 | + } else if (Maphper::FIND_OR ^ $mode && $currentFieldResult === false) { |
|
27 | + return false; |
|
28 | + } |
|
26 | 29 | } |
27 | 30 | return (bool)(Maphper::FIND_OR ^ $mode); |
28 | 31 | }; |
@@ -31,12 +34,13 @@ discard block |
||
31 | 34 | private function getIfFieldMatches($key, $val, $data, $mode) { |
32 | 35 | if ($this->shouldRunDeepSearch($key, $val)) { |
33 | 36 | return $this->getSearchFieldFunction($val, $key)($data); |
37 | + } else if (!isset($data->$key)) { |
|
38 | + return false; |
|
39 | + } else if ($this->isInArraySearch($mode, $key, $val)) { |
|
40 | + return in_array($data->$key, $val); |
|
41 | + } else { |
|
42 | + return $this->processFilter($mode, $val, $data->$key); |
|
34 | 43 | } |
35 | - else if (!isset($data->$key)) return false; |
|
36 | - else if ($this->isInArraySearch($mode, $key, $val)) |
|
37 | - return in_array($data->$key, $val); |
|
38 | - else |
|
39 | - return $this->processFilter($mode, $val, $data->$key); |
|
40 | 44 | } |
41 | 45 | |
42 | 46 | private function shouldRunDeepSearch($key, $val) { |
@@ -48,13 +52,21 @@ discard block |
||
48 | 52 | } |
49 | 53 | |
50 | 54 | private function processFilter($mode, $expected, $actual) { |
51 | - if (Maphper::FIND_NOT & $mode) return $expected != $actual; |
|
52 | - else if ((Maphper::FIND_GREATER | Maphper::FIND_EXACT) === $mode) return $expected <= $actual; |
|
53 | - else if ((Maphper::FIND_LESS | Maphper::FIND_EXACT) === $mode) return $expected >= $actual; |
|
54 | - else if (Maphper::FIND_GREATER & $mode) return $expected < $actual; |
|
55 | - else if (Maphper::FIND_LESS & $mode) return $expected > $actual; |
|
56 | - else if (Maphper::FIND_BETWEEN & $mode) return $expected[0] <= $actual && $actual <= $expected[1]; |
|
57 | - else if (Maphper::FIND_NOCASE & $mode) return strtolower($expected) == strtolower($actual); |
|
55 | + if (Maphper::FIND_NOT & $mode) { |
|
56 | + return $expected != $actual; |
|
57 | + } else if ((Maphper::FIND_GREATER | Maphper::FIND_EXACT) === $mode) { |
|
58 | + return $expected <= $actual; |
|
59 | + } else if ((Maphper::FIND_LESS | Maphper::FIND_EXACT) === $mode) { |
|
60 | + return $expected >= $actual; |
|
61 | + } else if (Maphper::FIND_GREATER & $mode) { |
|
62 | + return $expected < $actual; |
|
63 | + } else if (Maphper::FIND_LESS & $mode) { |
|
64 | + return $expected > $actual; |
|
65 | + } else if (Maphper::FIND_BETWEEN & $mode) { |
|
66 | + return $expected[0] <= $actual && $actual <= $expected[1]; |
|
67 | + } else if (Maphper::FIND_NOCASE & $mode) { |
|
68 | + return strtolower($expected) == strtolower($actual); |
|
69 | + } |
|
58 | 70 | return $expected == $actual; |
59 | 71 | } |
60 | 72 | } |
@@ -2,15 +2,15 @@ |
||
2 | 2 | namespace Maphper\Lib\Sql; |
3 | 3 | |
4 | 4 | class NullConditional implements WhereConditional { |
5 | - public function matches($key, $value, $mode) { |
|
6 | - return $value === NULL; |
|
7 | - } |
|
5 | + public function matches($key, $value, $mode) { |
|
6 | + return $value === NULL; |
|
7 | + } |
|
8 | 8 | |
9 | - public function getSql($key, $value, $mode) { |
|
10 | - $nullSql = $key . ' IS '; |
|
11 | - if (\Maphper\Maphper::FIND_NOT & $mode) $nullSql .= 'NOT '; |
|
12 | - $sql = [$nullSql . 'NULL']; |
|
9 | + public function getSql($key, $value, $mode) { |
|
10 | + $nullSql = $key . ' IS '; |
|
11 | + if (\Maphper\Maphper::FIND_NOT & $mode) $nullSql .= 'NOT '; |
|
12 | + $sql = [$nullSql . 'NULL']; |
|
13 | 13 | |
14 | - return ['args' => [], 'sql' => $sql]; |
|
15 | - } |
|
14 | + return ['args' => [], 'sql' => $sql]; |
|
15 | + } |
|
16 | 16 | } |
@@ -8,7 +8,9 @@ |
||
8 | 8 | |
9 | 9 | public function getSql($key, $value, $mode) { |
10 | 10 | $nullSql = $key . ' IS '; |
11 | - if (\Maphper\Maphper::FIND_NOT & $mode) $nullSql .= 'NOT '; |
|
11 | + if (\Maphper\Maphper::FIND_NOT & $mode) { |
|
12 | + $nullSql .= 'NOT '; |
|
13 | + } |
|
12 | 14 | $sql = [$nullSql . 'NULL']; |
13 | 15 | |
14 | 16 | return ['args' => [], 'sql' => $sql]; |