Passed
Push — master ( 380864...509b8c )
by Richard
01:39
created
maphper/datasource/StmtCache.php 2 patches
Braces   +6 added lines, -3 removed lines patch added patch discarded remove patch
@@ -10,10 +10,13 @@
 block discarded – undo
10 10
 
11 11
     public function getCachedStmt($sql) {
12 12
 		$queryId = $this->getQueryId($sql);
13
-		if (isset($this->queryCache[$queryId])) $stmt = $this->queryCache[$queryId];
14
-		else {
13
+		if (isset($this->queryCache[$queryId])) {
14
+			$stmt = $this->queryCache[$queryId];
15
+		} else {
15 16
 			$stmt = $this->pdo->prepare($sql, [\PDO::ATTR_CURSOR => \PDO::CURSOR_FWDONLY]);
16
-			if ($stmt) $this->queryCache[$queryId] = $stmt;
17
+			if ($stmt) {
18
+				$this->queryCache[$queryId] = $stmt;
19
+			}
17 20
 		}
18 21
 		return $stmt;
19 22
 	}
Please login to merge, or discard this patch.
Indentation   +15 added lines, -15 removed lines patch added patch discarded remove patch
@@ -1,14 +1,14 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 }
Please login to merge, or discard this patch.
maphper/lib/selectbuilder.php 1 patch
Braces   +10 added lines, -4 removed lines patch added patch discarded remove patch
@@ -7,17 +7,23 @@
 block discarded – undo
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
 }
Please login to merge, or discard this patch.
maphper/datasource/DatabaseSelect.php 3 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -94,7 +94,7 @@
 block discarded – undo
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
 
Please login to merge, or discard this patch.
Indentation   +49 added lines, -49 removed lines patch added patch discarded remove patch
@@ -2,31 +2,31 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 }
Please login to merge, or discard this patch.
Braces   +26 added lines, -15 removed lines patch added patch discarded remove patch
@@ -24,13 +24,15 @@  discard block
 block discarded – undo
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
-			}
28
-			catch (\Exception $e) {
27
+			} catch (\Exception $e) {
29 28
                 // Don't issue an error if it cannot be found since we return null
30 29
 			}
31 30
 
32
-			if (isset($result[0])) 	$this->idCache[$id] = $result[0];
33
-			else return null;
31
+			if (isset($result[0])) {
32
+				$this->idCache[$id] = $result[0];
33
+			} else {
34
+				return null;
35
+			}
34 36
 		}
35 37
 		return $this->idCache[$id];
36 38
 	}
@@ -40,14 +42,15 @@  discard block
 block discarded – undo
40 42
 		if (!isset($this->resultCache[$cacheId])) {
41 43
 			$query = $this->whereBuilder->createSql($fields);
42 44
 
43
-			if (!isset($options['order'])) $options['order'] = $this->defaultSort;
45
+			if (!isset($options['order'])) {
46
+				$options['order'] = $this->defaultSort;
47
+			}
44 48
 
45 49
 			try {
46 50
 				$this->resultCache[$cacheId] = $this->selectQuery($this->selectBuilder->select($this->table, $query['sql'], $query['args'], $options));
47 51
 				$this->databaseModify->addIndex(array_keys($query['args']));
48 52
 				$this->databaseModify->addIndex(explode(',', $options['order']));
49
-			}
50
-			catch (\Exception $e) {
53
+			} catch (\Exception $e) {
51 54
 				$this->errors[] = $e;
52 55
 				$this->resultCache[$cacheId] = [];
53 56
 			}
@@ -57,7 +60,9 @@  discard block
 block discarded – undo
57 60
 
58 61
     public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) {
59 62
 		//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
-		if (is_array($field)) $field = $field[0];
63
+		if (is_array($field)) {
64
+			$field = $field[0];
65
+		}
61 66
 		$query = $this->whereBuilder->createSql($criteria);
62 67
 
63 68
 		try {
@@ -66,8 +71,7 @@  discard block
 block discarded – undo
66 71
 			$result = $this->selectQuery($this->selectBuilder->aggregate($this->table, $function, $field, $query['sql'], $query['args'], $group));
67 72
 
68 73
 			return $this->determineAggregateResult($result, $group, $field);
69
-		}
70
-		catch (\Exception $e) {
74
+		} catch (\Exception $e) {
71 75
 			return $group ? [] : 0;
72 76
 		}
73 77
 	}
@@ -75,11 +79,15 @@  discard block
 block discarded – undo
75 79
     private function determineAggregateResult($result, $group, $field) {
76 80
         if ($group != null) {
77 81
             $ret = [];
78
-            foreach ($result as $res) $ret[$res->$field] = $res->val;
82
+            foreach ($result as $res) {
83
+            	$ret[$res->$field] = $res->val;
84
+            }
79 85
             return $ret;
86
+        } else if (isset($result[0])) {
87
+        	return $result[0]->val;
88
+        } else {
89
+        	return 0;
80 90
         }
81
-        else if (isset($result[0])) return $result[0]->val;
82
-        else return 0;
83 91
     }
84 92
 
85 93
     private function selectQuery(\Maphper\Lib\Query $query) {
@@ -95,8 +103,11 @@  discard block
 block discarded – undo
95 103
     }
96 104
 
97 105
     public function updateCache($data, $pkValue) {
98
-		if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data);
99
-		else $this->cache[$pkValue] = $data;
106
+		if (isset($this->cache[$pkValue])) {
107
+			$this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data);
108
+		} else {
109
+			$this->cache[$pkValue] = $data;
110
+		}
100 111
     }
101 112
 
102 113
     public function deleteIDFromCache($id) {
Please login to merge, or discard this patch.
maphper/datasource/DatabaseCrud.php 2 patches
Indentation   +50 added lines, -50 removed lines patch added patch discarded remove patch
@@ -2,30 +2,30 @@  discard block
 block discarded – undo
2 2
 namespace Maphper\DataSource;
3 3
 
4 4
 class DatabaseCrud {
5
-    private $crudBuilder;
6
-    private $whereBuilder;
7
-    private $adapter;
8
-    private $databaseModify;
9
-    private $databaseSelect;
10
-    private $table;
11
-    private $primaryKey;
12
-
13
-    public function __construct(DatabaseAdapter $adapter, DatabaseModify $databaseModify, DatabaseSelect $databaseSelect, $table, $primaryKey) {
14
-        $this->adapter = $adapter;
15
-        $this->databaseModify = $databaseModify;
16
-        $this->databaseSelect = $databaseSelect;
17
-        $this->crudBuilder = new \Maphper\Lib\CrudBuilder();
18
-        $this->whereBuilder = new \Maphper\Lib\Sql\WhereBuilder();
19
-        $this->table = $table;
20
-        $this->primaryKey = $primaryKey;
21
-    }
22
-
23
-    public function deleteById($id) {
24
-        $this->adapter->query($this->crudBuilder->delete($this->table, [$this->primaryKey[0] . ' = :id'], [':id' => $id], 1));
25
-        $this->databaseSelect->deleteIDFromCache($id);
5
+	private $crudBuilder;
6
+	private $whereBuilder;
7
+	private $adapter;
8
+	private $databaseModify;
9
+	private $databaseSelect;
10
+	private $table;
11
+	private $primaryKey;
12
+
13
+	public function __construct(DatabaseAdapter $adapter, DatabaseModify $databaseModify, DatabaseSelect $databaseSelect, $table, $primaryKey) {
14
+		$this->adapter = $adapter;
15
+		$this->databaseModify = $databaseModify;
16
+		$this->databaseSelect = $databaseSelect;
17
+		$this->crudBuilder = new \Maphper\Lib\CrudBuilder();
18
+		$this->whereBuilder = new \Maphper\Lib\Sql\WhereBuilder();
19
+		$this->table = $table;
20
+		$this->primaryKey = $primaryKey;
26 21
 	}
27 22
 
28
-    public function deleteByField(array $fields, array $options = []) {
23
+	public function deleteById($id) {
24
+		$this->adapter->query($this->crudBuilder->delete($this->table, [$this->primaryKey[0] . ' = :id'], [':id' => $id], 1));
25
+		$this->databaseSelect->deleteIDFromCache($id);
26
+	}
27
+
28
+	public function deleteByField(array $fields, array $options = []) {
29 29
 		$query = $this->whereBuilder->createSql($fields);
30 30
 		$this->adapter->query($this->crudBuilder->delete($this->table, $query['sql'], $query['args'], $options['limit'], null, $options['order']));
31 31
 		$this->databaseModify->addIndex(array_keys($query['args']));
@@ -35,22 +35,22 @@  discard block
 block discarded – undo
35 35
 		$this->databaseSelect->clearResultCache();
36 36
 	}
37 37
 
38
-    private function getIfNew($data) {
39
-        $new = false;
40
-        foreach ($this->primaryKey as $k) {
41
-            if (empty($data->$k)) {
42
-                $data->$k = null;
43
-                $new = true;
44
-            }
45
-        }
46
-        return $new;
47
-    }
38
+	private function getIfNew($data) {
39
+		$new = false;
40
+		foreach ($this->primaryKey as $k) {
41
+			if (empty($data->$k)) {
42
+				$data->$k = null;
43
+				$new = true;
44
+			}
45
+		}
46
+		return $new;
47
+	}
48 48
 
49
-    public function save($data, $tryagain = true) {
50
-        $new = $this->getIfNew($data);
49
+	public function save($data, $tryagain = true) {
50
+		$new = $this->getIfNew($data);
51 51
 
52 52
 		try {
53
-            $result = $this->insert($this->table, $this->primaryKey, $data);
53
+			$result = $this->insert($this->table, $this->primaryKey, $data);
54 54
 
55 55
 			//If there was an error but PDO is silent, trigger the catch block anyway
56 56
 			if ($result->errorCode() !== '00000') throw new \Exception('Could not insert into ' . $this->table);
@@ -68,15 +68,15 @@  discard block
 block discarded – undo
68 68
 		$this->databaseSelect->updateCache($data, $data->{$this->primaryKey[0]});
69 69
 	}
70 70
 
71
-    private function updatePK($data, $new) {
72
-        if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId();
73
-    }
71
+	private function updatePK($data, $new) {
72
+		if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId();
73
+	}
74 74
 
75
-    private function checkIfUpdateWorked($data) {
76
-        $updateWhere = $this->whereBuilder->createSql($data);
77
-        $matched = $this->databaseSelect->findByField($updateWhere['args']);
78
-        if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints');
79
-    }
75
+	private function checkIfUpdateWorked($data) {
76
+		$updateWhere = $this->whereBuilder->createSql($data);
77
+		$matched = $this->databaseSelect->findByField($updateWhere['args']);
78
+		if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints');
79
+	}
80 80
 
81 81
 	private function insert($table, array $primaryKey, $data) {
82 82
 		$error = 0;
@@ -88,16 +88,16 @@  discard block
 block discarded – undo
88 88
 		}
89 89
 
90 90
  		if ($error || $result->errorCode() !== '00000') {
91
-            $result = $this->tryUpdate($table, $primaryKey, $data);
92
-        }
91
+			$result = $this->tryUpdate($table, $primaryKey, $data);
92
+		}
93 93
 
94 94
 		return $result;
95 95
 	}
96 96
 
97
-    private function tryUpdate($table, array $primaryKey, $data) {
98
-        $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data));
99
-        if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data);
97
+	private function tryUpdate($table, array $primaryKey, $data) {
98
+		$result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data));
99
+		if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data);
100 100
 
101
-        return $result;
102
-    }
101
+		return $result;
102
+	}
103 103
 }
Please login to merge, or discard this patch.
Braces   +17 added lines, -9 removed lines patch added patch discarded remove patch
@@ -53,10 +53,13 @@  discard block
 block discarded – undo
53 53
             $result = $this->insert($this->table, $this->primaryKey, $data);
54 54
 
55 55
 			//If there was an error but PDO is silent, trigger the catch block anyway
56
-			if ($result->errorCode() !== '00000') throw new \Exception('Could not insert into ' . $this->table);
57
-		}
58
-		catch (\Exception $e) {
59
-			if (!$this->databaseModify->getTryInsertAgain($tryagain)) throw $e;
56
+			if ($result->errorCode() !== '00000') {
57
+				throw new \Exception('Could not insert into ' . $this->table);
58
+			}
59
+		} catch (\Exception $e) {
60
+			if (!$this->databaseModify->getTryInsertAgain($tryagain)) {
61
+				throw $e;
62
+			}
60 63
 
61 64
 			$this->adapter->alterDatabase($this->table, $this->primaryKey, $data);
62 65
 			$this->save($data, false);
@@ -69,21 +72,24 @@  discard block
 block discarded – undo
69 72
 	}
70 73
 
71 74
     private function updatePK($data, $new) {
72
-        if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId();
75
+        if ($new && count($this->primaryKey) == 1) {
76
+        	$data->{$this->primaryKey[0]} = $this->adapter->lastInsertId();
77
+        }
73 78
     }
74 79
 
75 80
     private function checkIfUpdateWorked($data) {
76 81
         $updateWhere = $this->whereBuilder->createSql($data);
77 82
         $matched = $this->databaseSelect->findByField($updateWhere['args']);
78
-        if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints');
83
+        if (count($matched) == 0) {
84
+        	throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints');
85
+        }
79 86
     }
80 87
 
81 88
 	private function insert($table, array $primaryKey, $data) {
82 89
 		$error = 0;
83 90
 		try {
84 91
 			$result = $this->adapter->query($this->crudBuilder->insert($table, $data));
85
-		}
86
-		catch (\Exception $e) {
92
+		} catch (\Exception $e) {
87 93
 			$error = 1;
88 94
 		}
89 95
 
@@ -96,7 +102,9 @@  discard block
 block discarded – undo
96 102
 
97 103
     private function tryUpdate($table, array $primaryKey, $data) {
98 104
         $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data));
99
-        if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data);
105
+        if ($result->rowCount() === 0) {
106
+        	$this->checkIfUpdateWorked($data);
107
+        }
100 108
 
101 109
         return $result;
102 110
     }
Please login to merge, or discard this patch.
maphper/datasource/database.php 2 patches
Indentation   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -7,8 +7,8 @@  discard block
 block discarded – undo
7 7
 
8 8
 	private $primaryKey;
9 9
 	private $fields = '*';
10
-    private $databaseSelect;
11
-    private $databaseCrud;
10
+	private $databaseSelect;
11
+	private $databaseCrud;
12 12
 
13 13
 	public function __construct($db, $table, $primaryKey = 'id', array $options = []) {
14 14
 		$options = new DatabaseOptions($db, $options);
@@ -20,10 +20,10 @@  discard block
 block discarded – undo
20 20
 
21 21
 		$defaultSort = $options->read('defaultSort') !== false ? $options->read('defaultSort')  : implode(', ', $this->primaryKey);
22 22
 
23
-        $databaseModify = new DatabaseModify($adapter, $options->getEditMode(), $table);
23
+		$databaseModify = new DatabaseModify($adapter, $options->getEditMode(), $table);
24 24
 
25
-        $this->databaseSelect = new DatabaseSelect($adapter, $databaseModify, $table, $defaultSort);
26
-        $this->databaseCrud = new DatabaseCrud($adapter, $databaseModify, $this->databaseSelect, $table, $this->primaryKey);
25
+		$this->databaseSelect = new DatabaseSelect($adapter, $databaseModify, $table, $defaultSort);
26
+		$this->databaseCrud = new DatabaseCrud($adapter, $databaseModify, $this->databaseSelect, $table, $this->primaryKey);
27 27
 
28 28
 		$databaseModify->optimizeColumns();
29 29
 	}
@@ -45,7 +45,7 @@  discard block
 block discarded – undo
45 45
 	}
46 46
 
47 47
 	public function findByField(array $fields, $options = []) {
48
-        return $this->databaseSelect->findByField($fields, $options);
48
+		return $this->databaseSelect->findByField($fields, $options);
49 49
 	}
50 50
 
51 51
 	public function deleteByField(array $fields, array $options = []) {
@@ -53,6 +53,6 @@  discard block
 block discarded – undo
53 53
 	}
54 54
 
55 55
 	public function save($data) {
56
-        $this->databaseCrud->save($data, true);
56
+		$this->databaseCrud->save($data, true);
57 57
 	}
58 58
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -16,9 +16,9 @@
 block discarded – undo
16 16
 
17 17
 		$this->primaryKey = is_array($primaryKey) ? $primaryKey : [$primaryKey];
18 18
 
19
-		$this->fields = implode(',', array_map([$adapter, 'quote'], (array) $options->read('fields')));
19
+		$this->fields = implode(',', array_map([$adapter, 'quote'], (array)$options->read('fields')));
20 20
 
21
-		$defaultSort = $options->read('defaultSort') !== false ? $options->read('defaultSort')  : implode(', ', $this->primaryKey);
21
+		$defaultSort = $options->read('defaultSort') !== false ? $options->read('defaultSort') : implode(', ', $this->primaryKey);
22 22
 
23 23
         $databaseModify = new DatabaseModify($adapter, $options->getEditMode(), $table);
24 24
 
Please login to merge, or discard this patch.
maphper/datasource/DatabaseModify.php 3 patches
Indentation   +15 added lines, -15 removed lines patch added patch discarded remove patch
@@ -2,25 +2,25 @@
 block discarded – undo
2 2
 namespace Maphper\DataSource;
3 3
 
4 4
 class DatabaseModify {
5
-    private $adapter;
6
-    private $alterDb;
7
-    private $table;
5
+	private $adapter;
6
+	private $alterDb;
7
+	private $table;
8 8
 
9
-    public function __construct(DatabaseAdapter $adapter, $alterDb, $table) {
10
-        $this->adapter = $adapter;
11
-        $this->alterDb = $alterDb;
12
-        $this->table = $table;
13
-    }
9
+	public function __construct(DatabaseAdapter $adapter, $alterDb, $table) {
10
+		$this->adapter = $adapter;
11
+		$this->alterDb = $alterDb;
12
+		$this->table = $table;
13
+	}
14 14
 
15
-    public function addIndex($args) {
15
+	public function addIndex($args) {
16 16
 		if (Database::EDIT_INDEX & $this->alterDb) $this->adapter->addIndex($this->table, $args);
17 17
 	}
18 18
 
19
-    public function optimizeColumns() {
20
-        if (Database::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($this->table);
21
-    }
19
+	public function optimizeColumns() {
20
+		if (Database::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($this->table);
21
+	}
22 22
 
23
-    public function getTryInsertAgain($tryagain) {
24
-        return $tryagain && Database::EDIT_STRUCTURE & $this->alterDb;
25
-    }
23
+	public function getTryInsertAgain($tryagain) {
24
+		return $tryagain && Database::EDIT_STRUCTURE & $this->alterDb;
25
+	}
26 26
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -17,7 +17,7 @@
 block discarded – undo
17 17
 	}
18 18
 
19 19
     public function optimizeColumns() {
20
-        if (Database::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($this->table);
20
+        if (Database::EDIT_OPTIMISE & $this->alterDb && rand(0, 500) == 1) $this->adapter->optimiseColumns($this->table);
21 21
     }
22 22
 
23 23
     public function getTryInsertAgain($tryagain) {
Please login to merge, or discard this patch.
Braces   +6 added lines, -2 removed lines patch added patch discarded remove patch
@@ -13,11 +13,15 @@
 block discarded – undo
13 13
     }
14 14
 
15 15
     public function addIndex($args) {
16
-		if (Database::EDIT_INDEX & $this->alterDb) $this->adapter->addIndex($this->table, $args);
16
+		if (Database::EDIT_INDEX & $this->alterDb) {
17
+			$this->adapter->addIndex($this->table, $args);
18
+		}
17 19
 	}
18 20
 
19 21
     public function optimizeColumns() {
20
-        if (Database::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($this->table);
22
+        if (Database::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) {
23
+        	$this->adapter->optimiseColumns($this->table);
24
+        }
21 25
     }
22 26
 
23 27
     public function getTryInsertAgain($tryagain) {
Please login to merge, or discard this patch.
maphper/lib/ArrayFilter.php 3 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -33,7 +33,7 @@
 block discarded – undo
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
 
Please login to merge, or discard this patch.
Indentation   +54 added lines, -54 removed lines patch added patch discarded remove patch
@@ -3,58 +3,58 @@
 block discarded – undo
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
 }
Please login to merge, or discard this patch.
Braces   +26 added lines, -14 removed lines patch added patch discarded remove patch
@@ -21,8 +21,11 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 }
Please login to merge, or discard this patch.
maphper/datasource/mock.php 2 patches
Indentation   +58 added lines, -58 removed lines patch added patch discarded remove patch
@@ -2,76 +2,76 @@
 block discarded – undo
2 2
 namespace Maphper\DataSource;
3 3
 use Maphper\Maphper;
4 4
 class Mock implements \Maphper\DataSource {
5
-    private $data;
6
-    private $id;
5
+	private $data;
6
+	private $id;
7 7
 
8
-    public function __construct(\ArrayObject $data, $id) {
9
-        $this->data = $data;
10
-        $this->id = is_array($id) ? $id : [$id];
11
-    }
8
+	public function __construct(\ArrayObject $data, $id) {
9
+		$this->data = $data;
10
+		$this->id = is_array($id) ? $id : [$id];
11
+	}
12 12
 
13
-    public function getPrimaryKey() {
14
-        return $this->id;
15
-    }
13
+	public function getPrimaryKey() {
14
+		return $this->id;
15
+	}
16 16
 
17
-    public function findById($id) {
18
-        return isset($this->data[$id]) ? (array)$this->data[$id] : [];
19
-    }
17
+	public function findById($id) {
18
+		return isset($this->data[$id]) ? (array)$this->data[$id] : [];
19
+	}
20 20
 
21
-    public function findByField(array $fields, $options = []) {
22
-        $arrayFilter = new \Maphper\Lib\ArrayFilter(iterator_to_array($this->data->getIterator()));
23
-        $filteredArray = $arrayFilter->filter($fields);
24
-        if (isset($options['order'])) {
25
-            list($columns, $order) = explode(' ', $options['order']);
26
-            usort($filteredArray, $this->getOrderFunction($order, $columns));
27
-        }
28
-        if (isset($options['offset'])) $filteredArray = array_slice($filteredArray, $options['offset']);
29
-        if (isset($options['limit'])) $filteredArray = array_slice($filteredArray, 0, $options['limit']);
30
-        return $filteredArray;
31
-    }
21
+	public function findByField(array $fields, $options = []) {
22
+		$arrayFilter = new \Maphper\Lib\ArrayFilter(iterator_to_array($this->data->getIterator()));
23
+		$filteredArray = $arrayFilter->filter($fields);
24
+		if (isset($options['order'])) {
25
+			list($columns, $order) = explode(' ', $options['order']);
26
+			usort($filteredArray, $this->getOrderFunction($order, $columns));
27
+		}
28
+		if (isset($options['offset'])) $filteredArray = array_slice($filteredArray, $options['offset']);
29
+		if (isset($options['limit'])) $filteredArray = array_slice($filteredArray, 0, $options['limit']);
30
+		return $filteredArray;
31
+	}
32 32
 
33 33
   	public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) {
34
-        return $function($this->findByField($criteria));
35
-    }
34
+		return $function($this->findByField($criteria));
35
+	}
36 36
 
37 37
   	public function deleteById($id) {
38
-        unset($this->data[$id]);
39
-    }
38
+		unset($this->data[$id]);
39
+	}
40 40
 
41 41
   	public function deleteByField(array $fields, array $options) {
42
-        foreach ($this->findByField($fields, $options) as $val) unset($this->data[$val->{$this->id[0]}]);
43
-    }
42
+		foreach ($this->findByField($fields, $options) as $val) unset($this->data[$val->{$this->id[0]}]);
43
+	}
44 44
 
45
-    public function save($data) {
46
-        if (isset($data->{$this->id[0]})) {
47
-            $id = $data->{$this->id[0]};
48
-        }
49
-        else {
50
-            $id = count($this->data);
51
-            $data->{$this->id[0]} = $id;
52
-        }
45
+	public function save($data) {
46
+		if (isset($data->{$this->id[0]})) {
47
+			$id = $data->{$this->id[0]};
48
+		}
49
+		else {
50
+			$id = count($this->data);
51
+			$data->{$this->id[0]} = $id;
52
+		}
53 53
 
54
-        $this->data[$id] = (object)array_merge($this->findById($id), (array)$data);
55
-    }
54
+		$this->data[$id] = (object)array_merge($this->findById($id), (array)$data);
55
+	}
56 56
 
57
-    public function getErrors() {
58
-        return [];
59
-    }
57
+	public function getErrors() {
58
+		return [];
59
+	}
60 60
 
61
-    private function getOrderFunction($order, $columns) {
62
-        return function($a, $b) use ($order, $columns) {
63
-          foreach (explode(',', $columns) as $column) {
64
-            $aColumn = $a->$column;
65
-            $bColumn = $b->$column;
66
-            if ($aColumn === $bColumn) {
67
-              $sortVal = 0;
68
-              continue;
69
-            }
70
-            else $sortVal = ($aColumn < $bColumn) ? -1 : 1;
71
-            break;
72
-          }
73
-          if ($order === 'desc') return -$sortVal;
74
-          else return $sortVal;
75
-        };
76
-    }
61
+	private function getOrderFunction($order, $columns) {
62
+		return function($a, $b) use ($order, $columns) {
63
+		  foreach (explode(',', $columns) as $column) {
64
+			$aColumn = $a->$column;
65
+			$bColumn = $b->$column;
66
+			if ($aColumn === $bColumn) {
67
+			  $sortVal = 0;
68
+			  continue;
69
+			}
70
+			else $sortVal = ($aColumn < $bColumn) ? -1 : 1;
71
+			break;
72
+		  }
73
+		  if ($order === 'desc') return -$sortVal;
74
+		  else return $sortVal;
75
+		};
76
+	}
77 77
 }
Please login to merge, or discard this patch.
Braces   +17 added lines, -8 removed lines patch added patch discarded remove patch
@@ -25,8 +25,12 @@  discard block
 block discarded – undo
25 25
             list($columns, $order) = explode(' ', $options['order']);
26 26
             usort($filteredArray, $this->getOrderFunction($order, $columns));
27 27
         }
28
-        if (isset($options['offset'])) $filteredArray = array_slice($filteredArray, $options['offset']);
29
-        if (isset($options['limit'])) $filteredArray = array_slice($filteredArray, 0, $options['limit']);
28
+        if (isset($options['offset'])) {
29
+        	$filteredArray = array_slice($filteredArray, $options['offset']);
30
+        }
31
+        if (isset($options['limit'])) {
32
+        	$filteredArray = array_slice($filteredArray, 0, $options['limit']);
33
+        }
30 34
         return $filteredArray;
31 35
     }
32 36
 
@@ -39,14 +43,15 @@  discard block
 block discarded – undo
39 43
     }
40 44
 
41 45
   	public function deleteByField(array $fields, array $options) {
42
-        foreach ($this->findByField($fields, $options) as $val) unset($this->data[$val->{$this->id[0]}]);
46
+        foreach ($this->findByField($fields, $options) as $val) {
47
+        	unset($this->data[$val->{$this->id[0]}]);
48
+        }
43 49
     }
44 50
 
45 51
     public function save($data) {
46 52
         if (isset($data->{$this->id[0]})) {
47 53
             $id = $data->{$this->id[0]};
48
-        }
49
-        else {
54
+        } else {
50 55
             $id = count($this->data);
51 56
             $data->{$this->id[0]} = $id;
52 57
         }
@@ -66,12 +71,16 @@  discard block
 block discarded – undo
66 71
             if ($aColumn === $bColumn) {
67 72
               $sortVal = 0;
68 73
               continue;
74
+            } else {
75
+            	$sortVal = ($aColumn < $bColumn) ? -1 : 1;
69 76
             }
70
-            else $sortVal = ($aColumn < $bColumn) ? -1 : 1;
71 77
             break;
72 78
           }
73
-          if ($order === 'desc') return -$sortVal;
74
-          else return $sortVal;
79
+          if ($order === 'desc') {
80
+          	return -$sortVal;
81
+          } else {
82
+          	return $sortVal;
83
+          }
75 84
         };
76 85
     }
77 86
 }
Please login to merge, or discard this patch.
maphper/lib/Sql/WhereConditional.php 1 patch
Indentation   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -2,6 +2,6 @@
 block discarded – undo
2 2
 namespace Maphper\Lib\Sql;
3 3
 
4 4
 interface WhereConditional {
5
-    public function matches($key, $value, $mode);
6
-    public function getSql($key, $value, $mode);
5
+	public function matches($key, $value, $mode);
6
+	public function getSql($key, $value, $mode);
7 7
 }
Please login to merge, or discard this patch.