@@ -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 | } |
@@ -25,23 +25,36 @@ discard block |
||
25 | 25 | } |
26 | 26 | |
27 | 27 | public function getType($val) { |
28 | - if ($val instanceof \DateTime) return $this->dataTypes['datetime']; |
|
29 | - else if ($result = $this->doNumberTypes($val)) return $result; |
|
30 | - else if ($result = $this->doStringTypes($val)) return $result; |
|
31 | - else return $this->dataTypes['other']; |
|
28 | + if ($val instanceof \DateTime) { |
|
29 | + return $this->dataTypes['datetime']; |
|
30 | + } else if ($result = $this->doNumberTypes($val)) { |
|
31 | + return $result; |
|
32 | + } else if ($result = $this->doStringTypes($val)) { |
|
33 | + return $result; |
|
34 | + } else { |
|
35 | + return $this->dataTypes['other']; |
|
36 | + } |
|
32 | 37 | } |
33 | 38 | |
34 | 39 | private function doNumberTypes($val) { |
35 | - if (is_int($val)) return $this->dataTypes['int']; |
|
36 | - else if (is_double($val)) return $this->dataTypes['decimal'] . '(9,' . strlen($val) - strrpos($val, '.') - 1 . ')'; |
|
37 | - else return false; |
|
40 | + if (is_int($val)) { |
|
41 | + return $this->dataTypes['int']; |
|
42 | + } else if (is_double($val)) { |
|
43 | + return $this->dataTypes['decimal'] . '(9,' . strlen($val) - strrpos($val, '.') - 1 . ')'; |
|
44 | + } else { |
|
45 | + return false; |
|
46 | + } |
|
38 | 47 | } |
39 | 48 | |
40 | 49 | private function doStringTypes($val) { |
41 | - if (!is_string($val)) return false; |
|
42 | - if (strlen($val) <= $this->dataTypes['short_string_max_len']) |
|
43 | - return $this->dataTypes['short_string'] . '(' . $this->dataTypes['short_string_max_len'] . ')'; |
|
44 | - else return $this->dataTypes['long_string']; |
|
50 | + if (!is_string($val)) { |
|
51 | + return false; |
|
52 | + } |
|
53 | + if (strlen($val) <= $this->dataTypes['short_string_max_len']) { |
|
54 | + return $this->dataTypes['short_string'] . '(' . $this->dataTypes['short_string_max_len'] . ')'; |
|
55 | + } else { |
|
56 | + return $this->dataTypes['long_string']; |
|
57 | + } |
|
45 | 58 | } |
46 | 59 | |
47 | 60 | public function isNotSavableType($value, $key, $primaryKey) { |
@@ -54,8 +67,11 @@ discard block |
||
54 | 67 | $parts = []; |
55 | 68 | foreach ($primaryKey as $key) { |
56 | 69 | $pk = $data->$key; |
57 | - if ($pk == null) $parts[] = $key . ' ' . $this->dataTypes['pk_default']; |
|
58 | - else $parts[] = $key . ' ' . $this->getType($pk) . ' NOT NULL'; |
|
70 | + if ($pk == null) { |
|
71 | + $parts[] = $key . ' ' . $this->dataTypes['pk_default']; |
|
72 | + } else { |
|
73 | + $parts[] = $key . ' ' . $this->getType($pk) . ' NOT NULL'; |
|
74 | + } |
|
59 | 75 | } |
60 | 76 | |
61 | 77 | $pkField = implode(', ', $parts) . ', PRIMARY KEY(' . implode(', ', $primaryKey) . ')'; |
@@ -24,7 +24,9 @@ discard block |
||
24 | 24 | $args = $query->getArgs(); |
25 | 25 | |
26 | 26 | //Handle SQLite when PDO_ERRMODE is set to SILENT |
27 | - if ($stmt === false) throw new \Exception('Invalid query'); |
|
27 | + if ($stmt === false) { |
|
28 | + throw new \Exception('Invalid query'); |
|
29 | + } |
|
28 | 30 | |
29 | 31 | $stmt->execute($args); |
30 | 32 | if ($stmt->errorCode() !== '00000' && $stmt->errorInfo()[2] == 'database schema has changed') { |
@@ -78,8 +80,7 @@ discard block |
||
78 | 80 | |
79 | 81 | $this->pdo->query('INSERT INTO ' . $this->quote($tableTo) . '(' . $columns . ') SELECT ' . $columns . ' FROM ' . $this->quote($tableFrom)); |
80 | 82 | } |
81 | - } |
|
82 | - catch (\PDOException $e) { |
|
83 | + } catch (\PDOException $e) { |
|
83 | 84 | // No data to copy |
84 | 85 | echo $e->getMessage(); |
85 | 86 | } |
@@ -87,7 +88,9 @@ discard block |
||
87 | 88 | |
88 | 89 | private function alterColumns($table, array $primaryKey, $data) { |
89 | 90 | foreach ($data as $key => $value) { |
90 | - if ($this->generalEditor->isNotSavableType($value, $key, $primaryKey)) continue; |
|
91 | + if ($this->generalEditor->isNotSavableType($value, $key, $primaryKey)) { |
|
92 | + continue; |
|
93 | + } |
|
91 | 94 | |
92 | 95 | $type = $this->generalEditor->getType($value); |
93 | 96 | |
@@ -96,10 +99,14 @@ discard block |
||
96 | 99 | } |
97 | 100 | |
98 | 101 | public function addIndex($table, array $fields) { |
99 | - if (empty($fields)) return false; |
|
102 | + if (empty($fields)) { |
|
103 | + return false; |
|
104 | + } |
|
100 | 105 | |
101 | 106 | //SQLite doesn't support ASC/DESC indexes, remove the keywords |
102 | - foreach ($fields as &$field) $field = str_ireplace([' desc', ' asc'], '', $field); |
|
107 | + foreach ($fields as &$field) { |
|
108 | + $field = str_ireplace([' desc', ' asc'], '', $field); |
|
109 | + } |
|
103 | 110 | sort($fields); |
104 | 111 | $fields = array_map('strtolower', $fields); |
105 | 112 | $fields = array_map('trim', $fields); |
@@ -108,8 +115,7 @@ discard block |
||
108 | 115 | |
109 | 116 | try { |
110 | 117 | $this->pdo->query('CREATE INDEX IF NOT EXISTS ' . $keyName . ' ON ' . $table . ' (' . implode(', ', $fields) . ')'); |
111 | - } |
|
112 | - catch (\Exception $e) { |
|
118 | + } catch (\Exception $e) { |
|
113 | 119 | |
114 | 120 | } |
115 | 121 | } |
@@ -31,7 +31,9 @@ discard block |
||
31 | 31 | public function overwrite($parentObject, &$data) { |
32 | 32 | $this->results = $data; |
33 | 33 | $this->object = $parentObject; |
34 | - foreach ($data as $dt) $this[] = $dt; |
|
34 | + foreach ($data as $dt) { |
|
35 | + $this[] = $dt; |
|
36 | + } |
|
35 | 37 | } |
36 | 38 | |
37 | 39 | //bit hacky, breaking encapsulation, but simplest way to work out the info for the other side of the many:many relationship. |
@@ -86,8 +88,9 @@ discard block |
||
86 | 88 | |
87 | 89 | public function offsetSet($name, $value) { |
88 | 90 | list($relatedField, $valueField, $mapper) = $this->getOtherFieldNameInfo(); |
89 | - if ($this->autoTraverse) $this->offsetSetAutotraverse($value, $relatedField, $valueField); |
|
90 | - else if ($this->doUpdateInterMapper($value, $relatedField, $valueField)) { |
|
91 | + if ($this->autoTraverse) { |
|
92 | + $this->offsetSetAutotraverse($value, $relatedField, $valueField); |
|
93 | + } else if ($this->doUpdateInterMapper($value, $relatedField, $valueField)) { |
|
91 | 94 | $record = $value; |
92 | 95 | $record->{$this->parentField} = $value->{$this->intermediateName}->{$this->localField}; |
93 | 96 | $record->$valueField = $this->object->{$relatedField}; |
@@ -9,7 +9,9 @@ discard block |
||
9 | 9 | private $siblings = []; |
10 | 10 | |
11 | 11 | public function __construct(\Maphper\Maphper $mapper, $parentField, $localField, array $criteria = []) { |
12 | - if ($criteria) $mapper = $mapper->filter($this->criteira); |
|
12 | + if ($criteria) { |
|
13 | + $mapper = $mapper->filter($this->criteira); |
|
14 | + } |
|
13 | 15 | $this->mapper = $mapper; |
14 | 16 | $this->parentField = $parentField; |
15 | 17 | $this->localField = $localField; |
@@ -29,7 +31,9 @@ discard block |
||
29 | 31 | private function lazyLoad() { |
30 | 32 | if (!isset($this->data)) { |
31 | 33 | |
32 | - if ($this->parentObject == null) throw new \Exception('Error, no object set'); |
|
34 | + if ($this->parentObject == null) { |
|
35 | + throw new \Exception('Error, no object set'); |
|
36 | + } |
|
33 | 37 | |
34 | 38 | $this->eagerLoad(); |
35 | 39 | |
@@ -69,13 +73,18 @@ discard block |
||
69 | 73 | } |
70 | 74 | |
71 | 75 | public function __call($func, array $args = []) { |
72 | - if ($this->lazyLoad() == null) return ''; |
|
76 | + if ($this->lazyLoad() == null) { |
|
77 | + return ''; |
|
78 | + } |
|
73 | 79 | return call_user_func_array([$this->lazyLoad(), $func], $args); |
74 | 80 | } |
75 | 81 | |
76 | 82 | public function __get($name) { |
77 | - if ($this->lazyLoad()) return $this->lazyLoad()->$name; |
|
78 | - else return null; |
|
83 | + if ($this->lazyLoad()) { |
|
84 | + return $this->lazyLoad()->$name; |
|
85 | + } else { |
|
86 | + return null; |
|
87 | + } |
|
79 | 88 | } |
80 | 89 | |
81 | 90 | public function __isset($name) { |
@@ -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) { |
@@ -13,7 +13,9 @@ discard block |
||
13 | 13 | 'Maphper\Lib\Sql\GeneralOperator' |
14 | 14 | ]; |
15 | 15 | |
16 | - foreach ($defaultConditionals as $conditional) $this->addConditional(new $conditional); |
|
16 | + foreach ($defaultConditionals as $conditional) { |
|
17 | + $this->addConditional(new $conditional); |
|
18 | + } |
|
17 | 19 | } |
18 | 20 | |
19 | 21 | public function addConditional(WhereConditional $conditional) { |
@@ -27,7 +29,9 @@ discard block |
||
27 | 29 | foreach ($fields as $key => $value) { |
28 | 30 | $value = $this->convertDates($value); |
29 | 31 | |
30 | - if (is_object($value)) continue; |
|
32 | + if (is_object($value)) { |
|
33 | + continue; |
|
34 | + } |
|
31 | 35 | $result = $this->getResult($key, $value, $mode); |
32 | 36 | $result = $this->fixDuplicateArgs($args, $result); |
33 | 37 | $sql = array_merge($sql, (array)$result['sql']); |
@@ -40,7 +44,9 @@ discard block |
||
40 | 44 | // Returns result with duplicate issues removed |
41 | 45 | private function fixDuplicateArgs($origArgs, $result) { |
42 | 46 | $duplicates = array_intersect_key($result['args'], $origArgs); // Holds all keys in results already in the args |
43 | - if (count($duplicates) === 0) return $result; |
|
47 | + if (count($duplicates) === 0) { |
|
48 | + return $result; |
|
49 | + } |
|
44 | 50 | |
45 | 51 | foreach ($duplicates as $argKey => $argVal) { |
46 | 52 | $valHash = substr(md5($argVal), 0, 5); |
@@ -59,29 +65,40 @@ discard block |
||
59 | 65 | * Either get sql from a conditional or call createSql again because the mode needs to be changed |
60 | 66 | */ |
61 | 67 | private function getResult($key, $value, $mode) { |
62 | - if (is_numeric($key) && is_array($value)) return $this->createSql($value, $key); |
|
68 | + if (is_numeric($key) && is_array($value)) { |
|
69 | + return $this->createSql($value, $key); |
|
70 | + } |
|
63 | 71 | return $this->getConditional($key, $value, $mode); |
64 | 72 | } |
65 | 73 | |
66 | 74 | private function sqlArrayToString($sql, $mode) { |
67 | - if (\Maphper\Maphper::FIND_OR & $mode) $query = implode(' OR ', $sql); |
|
68 | - else $query = implode(' AND ', $sql); |
|
69 | - if (!empty($query)) $query = '(' . $query . ')'; |
|
75 | + if (\Maphper\Maphper::FIND_OR & $mode) { |
|
76 | + $query = implode(' OR ', $sql); |
|
77 | + } else { |
|
78 | + $query = implode(' AND ', $sql); |
|
79 | + } |
|
80 | + if (!empty($query)) { |
|
81 | + $query = '(' . $query . ')'; |
|
82 | + } |
|
70 | 83 | return $query; |
71 | 84 | } |
72 | 85 | |
73 | 86 | private function getConditional($key, $value, $mode) { |
74 | 87 | foreach ($this->conditionals as $conditional) { |
75 | - if ($conditional->matches($key, $value, $mode)) |
|
76 | - return $conditional->getSql($key, $value, $mode); |
|
88 | + if ($conditional->matches($key, $value, $mode)) { |
|
89 | + return $conditional->getSql($key, $value, $mode); |
|
90 | + } |
|
77 | 91 | } |
78 | 92 | throw new \Exception("Invalid WHERE query"); |
79 | 93 | } |
80 | 94 | |
81 | 95 | private function convertDates($value) { |
82 | 96 | if ($value instanceof \DateTimeInterface) { |
83 | - if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
84 | - else $value = $value->format('Y-m-d H:i:s'); |
|
97 | + if ($value->format('H:i:s') == '00:00:00') { |
|
98 | + $value = $value->format('Y-m-d'); |
|
99 | + } else { |
|
100 | + $value = $value->format('Y-m-d H:i:s'); |
|
101 | + } |
|
85 | 102 | } |
86 | 103 | return $value; |
87 | 104 | } |
@@ -11,26 +11,35 @@ discard block |
||
11 | 11 | |
12 | 12 | public function replaceDates($obj, $reset = true) { |
13 | 13 | //prevent infinite recursion, only process each object once |
14 | - if ($this->checkCache($obj, $reset)) return $obj; |
|
14 | + if ($this->checkCache($obj, $reset)) { |
|
15 | + return $obj; |
|
16 | + } |
|
15 | 17 | |
16 | - if ($this->isIterable($obj)) foreach ($obj as &$o) $o = $this->replaceDates($o, false); |
|
17 | - if ($this->isPossiblyDateString($obj)) $obj = $this->tryToGetDateObjFromString($obj); |
|
18 | + if ($this->isIterable($obj)) { |
|
19 | + foreach ($obj as &$o) $o = $this->replaceDates($o, false); |
|
20 | + } |
|
21 | + if ($this->isPossiblyDateString($obj)) { |
|
22 | + $obj = $this->tryToGetDateObjFromString($obj); |
|
23 | + } |
|
18 | 24 | return $obj; |
19 | 25 | } |
20 | 26 | |
21 | 27 | private function tryToGetDateObjFromString($obj) { |
22 | 28 | try { |
23 | 29 | $date = new \DateTime($obj); |
24 | - if ($this->dateMatchesFormats($date, $obj)) $obj = $date; |
|
25 | - } |
|
26 | - catch (\Exception $e) { //Doesn't need to do anything as the try/catch is working out whether $obj is a date |
|
30 | + if ($this->dateMatchesFormats($date, $obj)) { |
|
31 | + $obj = $date; |
|
32 | + } |
|
33 | + } catch (\Exception $e) { //Doesn't need to do anything as the try/catch is working out whether $obj is a date |
|
27 | 34 | } |
28 | 35 | return $obj; |
29 | 36 | } |
30 | 37 | |
31 | 38 | private function dateMatchesFormats($date, $str) { |
32 | 39 | foreach ($this->dateFormats as list($format, $len)) { |
33 | - if ($date->format($format) == substr($str, 0, $len)) return true; |
|
40 | + if ($date->format($format) == substr($str, 0, $len)) { |
|
41 | + return true; |
|
42 | + } |
|
34 | 43 | } |
35 | 44 | return false; |
36 | 45 | } |
@@ -44,11 +53,18 @@ discard block |
||
44 | 53 | } |
45 | 54 | |
46 | 55 | private function checkCache($obj, $reset) { |
47 | - if ($reset) $this->processCache = new \SplObjectStorage(); |
|
48 | - if (!is_object($obj)) return false; |
|
56 | + if ($reset) { |
|
57 | + $this->processCache = new \SplObjectStorage(); |
|
58 | + } |
|
59 | + if (!is_object($obj)) { |
|
60 | + return false; |
|
61 | + } |
|
49 | 62 | |
50 | - if ($this->processCache->contains($obj)) return $obj; |
|
51 | - else $this->processCache->attach($obj, true); |
|
63 | + if ($this->processCache->contains($obj)) { |
|
64 | + return $obj; |
|
65 | + } else { |
|
66 | + $this->processCache->attach($obj, true); |
|
67 | + } |
|
52 | 68 | |
53 | 69 | return false; |
54 | 70 | } |