@@ -6,25 +6,35 @@ |
||
6 | 6 | |
7 | 7 | public function replaceDates($obj, $reset = true) { |
8 | 8 | //prevent infinite recursion, only process each object once |
9 | - if ($this->checkCache($obj, $reset)) return $obj; |
|
9 | + if ($this->checkCache($obj, $reset)) { |
|
10 | + return $obj; |
|
11 | + } |
|
10 | 12 | |
11 | - if (is_array($obj) || (is_object($obj) && ($obj instanceof \Iterator))) foreach ($obj as &$o) $o = $this->replaceDates($o, false); |
|
13 | + if (is_array($obj) || (is_object($obj) && ($obj instanceof \Iterator))) { |
|
14 | + foreach ($obj as &$o) $o = $this->replaceDates($o, false); |
|
15 | + } |
|
12 | 16 | if (is_string($obj) && isset($obj[0]) && is_numeric($obj[0]) && strlen($obj) <= 20) { |
13 | 17 | try { |
14 | 18 | $date = new \DateTime($obj); |
15 | - if ($date->format('Y-m-d H:i:s') == substr($obj, 0, 20) || $date->format('Y-m-d') == substr($obj, 0, 10)) $obj = $date; |
|
16 | - } |
|
17 | - catch (\Exception $e) { //Doesn't need to do anything as the try/catch is working out whether $obj is a date |
|
19 | + if ($date->format('Y-m-d H:i:s') == substr($obj, 0, 20) || $date->format('Y-m-d') == substr($obj, 0, 10)) { |
|
20 | + $obj = $date; |
|
21 | + } |
|
22 | + } catch (\Exception $e) { //Doesn't need to do anything as the try/catch is working out whether $obj is a date |
|
18 | 23 | } |
19 | 24 | } |
20 | 25 | return $obj; |
21 | 26 | } |
22 | 27 | |
23 | 28 | private function checkCache($obj, $reset) { |
24 | - if ($reset) $this->processCache = new \SplObjectStorage(); |
|
29 | + if ($reset) { |
|
30 | + $this->processCache = new \SplObjectStorage(); |
|
31 | + } |
|
25 | 32 | |
26 | - if (is_object($obj) && $this->processCache->contains($obj)) return $obj; |
|
27 | - else if (is_object($obj)) $this->processCache->attach($obj, true); |
|
33 | + if (is_object($obj) && $this->processCache->contains($obj)) { |
|
34 | + return $obj; |
|
35 | + } else if (is_object($obj)) { |
|
36 | + $this->processCache->attach($obj, true); |
|
37 | + } |
|
28 | 38 | |
29 | 39 | return false; |
30 | 40 | } |
@@ -104,10 +104,10 @@ discard block |
||
104 | 104 | |
105 | 105 | public function offsetExists($offset) { |
106 | 106 | if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
107 | - if (!empty($this->settings['filter'])) { |
|
108 | - $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
109 | - return isset($data[0]); |
|
110 | - } |
|
107 | + if (!empty($this->settings['filter'])) { |
|
108 | + $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
109 | + return isset($data[0]); |
|
110 | + } |
|
111 | 111 | return (bool) $this->dataSource->findById($offset); |
112 | 112 | } |
113 | 113 | |
@@ -117,10 +117,10 @@ discard block |
||
117 | 117 | |
118 | 118 | public function offsetGet($offset) { |
119 | 119 | if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
120 | - if (!empty($this->settings['filter'])) { |
|
121 | - $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
122 | - return $this->entity->create(isset($data[0]) ? $data[0] : null, $this->relations); |
|
123 | - } |
|
120 | + if (!empty($this->settings['filter'])) { |
|
121 | + $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
122 | + return $this->entity->create(isset($data[0]) ? $data[0] : null, $this->relations); |
|
123 | + } |
|
124 | 124 | return $this->entity->create($this->dataSource->findById($offset), $this->relations); |
125 | 125 | } |
126 | 126 |
@@ -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) { |
@@ -61,7 +61,9 @@ discard block |
||
61 | 61 | |
62 | 62 | $siblings = new \ArrayObject(); |
63 | 63 | |
64 | - foreach ($results as &$result) $result = $this->entity->create($result, $this->relations, $siblings); |
|
64 | + foreach ($results as &$result) { |
|
65 | + $result = $this->entity->create($result, $this->relations, $siblings); |
|
66 | + } |
|
65 | 67 | |
66 | 68 | return $results; |
67 | 69 | } |
@@ -78,13 +80,17 @@ discard block |
||
78 | 80 | private function processFilters($value) { |
79 | 81 | //When saving to a mapper with filters, write the filters back into the object being stored |
80 | 82 | foreach ($this->settings['filter'] as $key => $filterValue) { |
81 | - if (empty($value->$key) && !is_array($filterValue)) $value->$key = $filterValue; |
|
83 | + if (empty($value->$key) && !is_array($filterValue)) { |
|
84 | + $value->$key = $filterValue; |
|
85 | + } |
|
82 | 86 | } |
83 | 87 | return $value; |
84 | 88 | } |
85 | 89 | |
86 | 90 | public function offsetSet($offset, $valueObj) { |
87 | - if ($valueObj instanceof \Maphper\Relation) throw new \Exception(); |
|
91 | + if ($valueObj instanceof \Maphper\Relation) { |
|
92 | + throw new \Exception(); |
|
93 | + } |
|
88 | 94 | |
89 | 95 | //Extract private properties from the object |
90 | 96 | $visibilityOverride = new \Maphper\Lib\VisibilityOverride($valueObj); |
@@ -92,7 +98,9 @@ discard block |
||
92 | 98 | |
93 | 99 | $value = $this->processFilters($value); |
94 | 100 | $pk = $this->dataSource->getPrimaryKey(); |
95 | - if ($offset !== null) $value->{$pk[0]} = $offset; |
|
101 | + if ($offset !== null) { |
|
102 | + $value->{$pk[0]} = $offset; |
|
103 | + } |
|
96 | 104 | $valueCopy = clone $value; |
97 | 105 | $value = $this->entity->wrap($this->relations, $value); |
98 | 106 | $this->dataSource->save($value); |
@@ -104,7 +112,9 @@ discard block |
||
104 | 112 | } |
105 | 113 | |
106 | 114 | public function offsetExists($offset) { |
107 | - if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
|
115 | + if (count($this->dataSource->getPrimaryKey()) > 1) { |
|
116 | + return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
|
117 | + } |
|
108 | 118 | if (!empty($this->settings['filter'])) { |
109 | 119 | $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
110 | 120 | return isset($data[0]); |
@@ -117,7 +127,9 @@ discard block |
||
117 | 127 | } |
118 | 128 | |
119 | 129 | public function offsetGet($offset) { |
120 | - if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
|
130 | + if (count($this->dataSource->getPrimaryKey()) > 1) { |
|
131 | + return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
|
132 | + } |
|
121 | 133 | if (!empty($this->settings['filter'])) { |
122 | 134 | $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
123 | 135 | return $this->entity->create(isset($data[0]) ? $data[0] : null, $this->relations); |
@@ -128,11 +140,15 @@ discard block |
||
128 | 140 | public function __call($method, $args) { |
129 | 141 | if (array_key_exists($method, $this->settings)) { |
130 | 142 | $maphper = new Maphper($this->dataSource, $this->settings, $this->relations); |
131 | - if (is_array($maphper->settings[$method])) $maphper->settings[$method] = $args[0] + $maphper->settings[$method]; |
|
132 | - else $maphper->settings[$method] = $args[0]; |
|
143 | + if (is_array($maphper->settings[$method])) { |
|
144 | + $maphper->settings[$method] = $args[0] + $maphper->settings[$method]; |
|
145 | + } else { |
|
146 | + $maphper->settings[$method] = $args[0]; |
|
147 | + } |
|
133 | 148 | return $maphper; |
149 | + } else { |
|
150 | + throw new \Exception('Method Maphper::' . $method . ' does not exist'); |
|
134 | 151 | } |
135 | - else throw new \Exception('Method Maphper::' . $method . ' does not exist'); |
|
136 | 152 | } |
137 | 153 | |
138 | 154 | public function findAggregate($function, $field, $group = null) { |
@@ -21,7 +21,7 @@ |
||
21 | 21 | public function wrap($relations, $object, $siblings = []) { |
22 | 22 | //see if any relations need overwriting |
23 | 23 | foreach ($relations as $name => $relation) { |
24 | - if (isset($object->$name) && !($object->$name instanceof \Maphper\Relation) ) { |
|
24 | + if (isset($object->$name) && !($object->$name instanceof \Maphper\Relation)) { |
|
25 | 25 | //After overwriting the relation, does the parent object ($object) need overwriting as well? |
26 | 26 | if ($relation->overwrite($object, $object->$name)) $this->parent[] = $object; |
27 | 27 | } |
@@ -23,7 +23,9 @@ |
||
23 | 23 | foreach ($relations as $name => $relation) { |
24 | 24 | if (isset($object->$name) && !($object->$name instanceof \Maphper\Relation) ) { |
25 | 25 | //After overwriting the relation, does the parent object ($object) need overwriting as well? |
26 | - if ($relation->overwrite($object, $object->$name)) $this->parent[] = $object; |
|
26 | + if ($relation->overwrite($object, $object->$name)) { |
|
27 | + $this->parent[] = $object; |
|
28 | + } |
|
27 | 29 | } |
28 | 30 | |
29 | 31 | $object->$name = $relation->getData($object, $siblings); |
@@ -7,20 +7,20 @@ 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 | $this->readClosure = function() { |
15 | 15 | $data = new \stdClass; |
16 | - foreach ($this as $k => $v) { |
|
16 | + foreach ($this as $k => $v) { |
|
17 | 17 | if (is_scalar($v) || is_null($v) || (is_object($v) && $v instanceof \DateTime)) $data->$k = $v; |
18 | 18 | } |
19 | 19 | return $data; |
20 | 20 | }; |
21 | 21 | $this->readClosure = $this->readClosure->bindTo($object, $object); |
22 | 22 | |
23 | - $this->writeClosure = function ($field, $value) { $this->$field = $value; }; |
|
23 | + $this->writeClosure = function($field, $value) { $this->$field = $value; }; |
|
24 | 24 | $this->writeClosure = $this->writeClosure->bindTo($object, $object); |
25 | 25 | } |
26 | 26 | |
@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | public function write($data) { |
34 | 34 | if ($data != null) { |
35 | 35 | foreach ($data as $key => $value) { |
36 | - ($this->writeClosure)($key, $this->processDates($value)); |
|
36 | + ($this->writeClosure)($key, $this->processDates($value)); |
|
37 | 37 | } |
38 | 38 | } |
39 | 39 | } |
@@ -9,12 +9,13 @@ |
||
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 | $this->readClosure = function() { |
15 | 14 | $data = new \stdClass; |
16 | 15 | foreach ($this as $k => $v) { |
17 | - if (is_scalar($v) || is_null($v) || (is_object($v) && $v instanceof \DateTime)) $data->$k = $v; |
|
16 | + if (is_scalar($v) || is_null($v) || (is_object($v) && $v instanceof \DateTime)) { |
|
17 | + $data->$k = $v; |
|
18 | + } |
|
18 | 19 | } |
19 | 20 | return $data; |
20 | 21 | }; |
@@ -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 |
@@ -11,16 +11,21 @@ discard block |
||
11 | 11 | foreach ($columns as $column) { |
12 | 12 | $parts = explode('.', $column->Field_name); |
13 | 13 | $name = $this->quote(end($parts)); |
14 | - if ($column->Min_value === null && $column->Max_value === null) $this->pdo->query('ALTER TABLE ' . $this->quote($table) . ' DROP COLUMN ' . $name); |
|
15 | - else { |
|
14 | + if ($column->Min_value === null && $column->Max_value === null) { |
|
15 | + $this->pdo->query('ALTER TABLE ' . $this->quote($table) . ' DROP COLUMN ' . $name); |
|
16 | + } else { |
|
16 | 17 | $type = $column->Optimal_fieldtype; |
17 | 18 | if ($column->Max_length < 11) { |
18 | 19 | //Check for dates |
19 | 20 | $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; |
20 | - if ($count == 0) $type = 'DATETIME'; |
|
21 | + if ($count == 0) { |
|
22 | + $type = 'DATETIME'; |
|
23 | + } |
|
21 | 24 | |
22 | 25 | $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; |
23 | - if ($count == 0) $type = 'DATE'; |
|
26 | + if ($count == 0) { |
|
27 | + $type = 'DATE'; |
|
28 | + } |
|
24 | 29 | } |
25 | 30 | |
26 | 31 | //If it's text, work out if it would be better to be something else |
@@ -30,8 +35,7 @@ discard block |
||
30 | 35 | if ($count == 0) { |
31 | 36 | $type = 'INT(11)'; |
32 | 37 | $runAgain = true; |
33 | - } |
|
34 | - else { |
|
38 | + } else { |
|
35 | 39 | //See if it's decimal |
36 | 40 | $count = $this->pdo->query('SELECT count(*) FROM ' . $table . ' WHERE concat(\'\', ' . $name . ' * 1) != ' . $name . ')')->fetch(\PDO::FETCH_OBJ)->count; |
37 | 41 | if ($count == 0) { |
@@ -45,6 +49,8 @@ discard block |
||
45 | 49 | } |
46 | 50 | } |
47 | 51 | //Sometimes a second pass is needed, if a column has gone from varchar -> int(11) a better int type may be needed |
48 | - if ($runAgain) $this->optimiseColumns($table); |
|
52 | + if ($runAgain) { |
|
53 | + $this->optimiseColumns($table); |
|
54 | + } |
|
49 | 55 | } |
50 | 56 | } |
51 | 57 | \ No newline at end of file |
@@ -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) { |
@@ -10,7 +10,9 @@ discard block |
||
10 | 10 | } |
11 | 11 | |
12 | 12 | public function getAdapter() { |
13 | - if (!($this->db instanceof \PDO)) return $this->db; |
|
13 | + if (!($this->db instanceof \PDO)) { |
|
14 | + return $this->db; |
|
15 | + } |
|
14 | 16 | |
15 | 17 | $adapter = '\\Maphper\\DataSource\\' . ucfirst($this->db->getAttribute(\PDO::ATTR_DRIVER_NAME)) . 'Adapter'; |
16 | 18 | |
@@ -18,7 +20,9 @@ discard block |
||
18 | 20 | } |
19 | 21 | |
20 | 22 | public function getEditMode() { |
21 | - if (!isset($this->options['editmode'])) return false; |
|
23 | + if (!isset($this->options['editmode'])) { |
|
24 | + return false; |
|
25 | + } |
|
22 | 26 | |
23 | 27 | return $this->options['editmode'] === true ? Database::EDIT_STRUCTURE | Database::EDIT_INDEX | Database::EDIT_OPTIMISE : $this->options['editmode']; |
24 | 28 | } |
@@ -114,7 +114,7 @@ |
||
114 | 114 | else $limit = ''; |
115 | 115 | |
116 | 116 | $query = $this->selectBuilder->createSql($fields, $mode); |
117 | - $query['sql'] = array_filter($query['sql']); |
|
117 | + $query['sql'] = array_filter($query['sql']); |
|
118 | 118 | $this->adapter->query($this->crudBuilder->delete($this->table, $query['sql'], $query['args'], $limit)); |
119 | 119 | $this->addIndex(array_keys($query['args'])); |
120 | 120 |
@@ -25,13 +25,13 @@ discard block |
||
25 | 25 | $this->crudBuilder = new \Maphper\Lib\CrudBuilder(); |
26 | 26 | $this->selectBuilder = new \Maphper\Lib\SelectBuilder(); |
27 | 27 | |
28 | - $this->fields = implode(',', array_map([$this->adapter, 'quote'], (array) $this->options->read('fields'))); |
|
28 | + $this->fields = implode(',', array_map([$this->adapter, 'quote'], (array)$this->options->read('fields'))); |
|
29 | 29 | |
30 | - $this->defaultSort = $this->options->read('defaultSort') !== false ? $this->options->read('defaultSort') : implode(', ', $this->primaryKey); |
|
30 | + $this->defaultSort = $this->options->read('defaultSort') !== false ? $this->options->read('defaultSort') : implode(', ', $this->primaryKey); |
|
31 | 31 | |
32 | 32 | $this->alterDb = $this->options->getEditMode(); |
33 | 33 | |
34 | - if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($table); |
|
34 | + if (self::EDIT_OPTIMISE & $this->alterDb && rand(0, 500) == 1) $this->adapter->optimiseColumns($table); |
|
35 | 35 | } |
36 | 36 | |
37 | 37 | public function getPrimaryKey() { |
@@ -161,7 +161,7 @@ discard block |
||
161 | 161 | //Something has changed, clear any cached results as they may now be incorrect |
162 | 162 | $this->resultCache = []; |
163 | 163 | $pkValue = $data->{$this->primaryKey[0]}; |
164 | - if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
164 | + if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object)array_merge((array)$this->cache[$pkValue], (array)$data); |
|
165 | 165 | else $this->cache[$pkValue] = $data; |
166 | 166 | } |
167 | 167 |
@@ -31,7 +31,9 @@ discard block |
||
31 | 31 | |
32 | 32 | $this->alterDb = $this->options->getEditMode(); |
33 | 33 | |
34 | - if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($table); |
|
34 | + if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) { |
|
35 | + $this->adapter->optimiseColumns($table); |
|
36 | + } |
|
35 | 37 | } |
36 | 38 | |
37 | 39 | public function getPrimaryKey() { |
@@ -47,20 +49,24 @@ discard block |
||
47 | 49 | if (!isset($this->cache[$id])) { |
48 | 50 | try { |
49 | 51 | $result = $this->adapter->query($this->selectBuilder->select($this->table, [$this->getPrimaryKey()[0] . ' = :id'], [':id' => $id], ['limit' => 1])); |
50 | - } |
|
51 | - catch (\Exception $e) { |
|
52 | + } catch (\Exception $e) { |
|
52 | 53 | $this->errors[] = $e; |
53 | 54 | } |
54 | 55 | |
55 | - if (isset($result[0])) $this->cache[$id] = $result[0]; |
|
56 | - else return null; |
|
56 | + if (isset($result[0])) { |
|
57 | + $this->cache[$id] = $result[0]; |
|
58 | + } else { |
|
59 | + return null; |
|
60 | + } |
|
57 | 61 | } |
58 | 62 | return $this->cache[$id]; |
59 | 63 | } |
60 | 64 | |
61 | 65 | public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { |
62 | 66 | //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 |
63 | - if (is_array($field)) $field = $field[0]; |
|
67 | + if (is_array($field)) { |
|
68 | + $field = $field[0]; |
|
69 | + } |
|
64 | 70 | $query = $this->selectBuilder->createSql($criteria, \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND); |
65 | 71 | |
66 | 72 | try { |
@@ -68,22 +74,27 @@ discard block |
||
68 | 74 | $this->addIndex(explode(',', $group)); |
69 | 75 | $result = $this->adapter->query($this->selectBuilder->aggregate($this->table, $function, $field, $query['sql'], $query['args'], $group)); |
70 | 76 | |
71 | - if (isset($result[0]) && $group == null) return $result[0]->val; |
|
72 | - else if ($group != null) { |
|
77 | + if (isset($result[0]) && $group == null) { |
|
78 | + return $result[0]->val; |
|
79 | + } else if ($group != null) { |
|
73 | 80 | $ret = []; |
74 | - foreach ($result as $res) $ret[$res->$field] = $res->val; |
|
81 | + foreach ($result as $res) { |
|
82 | + $ret[$res->$field] = $res->val; |
|
83 | + } |
|
75 | 84 | return $ret; |
85 | + } else { |
|
86 | + return 0; |
|
76 | 87 | } |
77 | - else return 0; |
|
78 | - } |
|
79 | - catch (\Exception $e) { |
|
88 | + } catch (\Exception $e) { |
|
80 | 89 | $this->errors[] = $e; |
81 | 90 | return $group ? [] : 0; |
82 | 91 | } |
83 | 92 | } |
84 | 93 | |
85 | 94 | private function addIndex($args) { |
86 | - if (self::EDIT_INDEX & $this->alterDb) $this->adapter->addIndex($this->table, $args); |
|
95 | + if (self::EDIT_INDEX & $this->alterDb) { |
|
96 | + $this->adapter->addIndex($this->table, $args); |
|
97 | + } |
|
87 | 98 | } |
88 | 99 | |
89 | 100 | public function findByField(array $fields, $options = []) { |
@@ -91,7 +102,9 @@ discard block |
||
91 | 102 | if (!isset($this->resultCache[$cacheId])) { |
92 | 103 | $query = $this->selectBuilder->createSql($fields, \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND); |
93 | 104 | |
94 | - if (!isset($options['order'])) $options['order'] = $this->defaultSort; |
|
105 | + if (!isset($options['order'])) { |
|
106 | + $options['order'] = $this->defaultSort; |
|
107 | + } |
|
95 | 108 | |
96 | 109 | $query['sql'] = array_filter($query['sql']); |
97 | 110 | |
@@ -99,8 +112,7 @@ discard block |
||
99 | 112 | $this->resultCache[$cacheId] = $this->adapter->query($this->selectBuilder->select($this->table, $query['sql'], $query['args'], $options)); |
100 | 113 | $this->addIndex(array_keys($query['args'])); |
101 | 114 | $this->addIndex(explode(',', $options['order'])); |
102 | - } |
|
103 | - catch (\Exception $e) { |
|
115 | + } catch (\Exception $e) { |
|
104 | 116 | $this->errors[] = $e; |
105 | 117 | $this->resultCache[$cacheId] = []; |
106 | 118 | } |
@@ -109,9 +121,14 @@ discard block |
||
109 | 121 | } |
110 | 122 | |
111 | 123 | public function deleteByField(array $fields, array $options = [], $mode = null) { |
112 | - if ($mode == null) $mode = \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND; |
|
113 | - if (isset($options['limit']) != null) $limit = ' LIMIT ' . $options['limit']; |
|
114 | - else $limit = ''; |
|
124 | + if ($mode == null) { |
|
125 | + $mode = \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND; |
|
126 | + } |
|
127 | + if (isset($options['limit']) != null) { |
|
128 | + $limit = ' LIMIT ' . $options['limit']; |
|
129 | + } else { |
|
130 | + $limit = ''; |
|
131 | + } |
|
115 | 132 | |
116 | 133 | $query = $this->selectBuilder->createSql($fields, $mode); |
117 | 134 | $query['sql'] = array_filter($query['sql']); |
@@ -137,7 +154,9 @@ discard block |
||
137 | 154 | $result = $this->insert($this->table, $this->primaryKey, $data); |
138 | 155 | |
139 | 156 | //If there was an error but PDO is silent, trigger the catch block anyway |
140 | - if ($result->errorCode() !== '00000') throw new \Exception('Could not insert into ' . $this->table); |
|
157 | + if ($result->errorCode() !== '00000') { |
|
158 | + throw new \Exception('Could not insert into ' . $this->table); |
|
159 | + } |
|
141 | 160 | |
142 | 161 | if ($tryagain === false && $result->rowCount() === 0) { |
143 | 162 | |
@@ -145,32 +164,38 @@ discard block |
||
145 | 164 | |
146 | 165 | $matched = $this->findByField($updateWhere->getArgs()); |
147 | 166 | |
148 | - if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
167 | + if (count($matched) == 0) { |
|
168 | + throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
169 | + } |
|
149 | 170 | } |
150 | 171 | |
151 | - } |
|
152 | - catch (\Exception $e) { |
|
172 | + } catch (\Exception $e) { |
|
153 | 173 | if ($tryagain) { |
154 | 174 | $this->adapter->alterDatabase($this->table, $this->primaryKey, $data); |
155 | 175 | $this->save($data, false); |
176 | + } else { |
|
177 | + throw $e; |
|
156 | 178 | } |
157 | - else throw $e; |
|
158 | 179 | } |
159 | 180 | //TODO: This will error if the primary key is a private field |
160 | - if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
181 | + if ($new && count($this->primaryKey) == 1) { |
|
182 | + $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
183 | + } |
|
161 | 184 | //Something has changed, clear any cached results as they may now be incorrect |
162 | 185 | $this->resultCache = []; |
163 | 186 | $pkValue = $data->{$this->primaryKey[0]}; |
164 | - if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
165 | - else $this->cache[$pkValue] = $data; |
|
187 | + if (isset($this->cache[$pkValue])) { |
|
188 | + $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
189 | + } else { |
|
190 | + $this->cache[$pkValue] = $data; |
|
191 | + } |
|
166 | 192 | } |
167 | 193 | |
168 | 194 | private function insert($table, array $primaryKey, $data) { |
169 | 195 | $error = 0; |
170 | 196 | try { |
171 | 197 | $result = $this->adapter->query($this->crudBuilder->insert($table, $data)); |
172 | - } |
|
173 | - catch (\Exception $e) { |
|
198 | + } catch (\Exception $e) { |
|
174 | 199 | $error = 1; |
175 | 200 | } |
176 | 201 |