@@ -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 | } |
@@ -8,7 +8,9 @@ |
||
8 | 8 | |
9 | 9 | public function getSql($key, $value, $mode) { |
10 | 10 | $nullSql = $key . ' IS '; |
11 | - if (\Maphper\Maphper::FIND_NOT & $mode) $nullSql .= 'NOT '; |
|
11 | + if (\Maphper\Maphper::FIND_NOT & $mode) { |
|
12 | + $nullSql .= 'NOT '; |
|
13 | + } |
|
12 | 14 | $sql = [$nullSql . 'NULL']; |
13 | 15 | |
14 | 16 | return ['args' => [], 'sql' => $sql]; |
@@ -16,8 +16,12 @@ |
||
16 | 16 | } |
17 | 17 | |
18 | 18 | private function getValue($value, $mode) { |
19 | - if ((Maphper::FIND_LIKE | Maphper::FIND_ENDS) & $mode) $value = '%' . $value; |
|
20 | - if ((Maphper::FIND_LIKE | Maphper::FIND_STARTS) & $mode) $value .= '%'; |
|
19 | + if ((Maphper::FIND_LIKE | Maphper::FIND_ENDS) & $mode) { |
|
20 | + $value = '%' . $value; |
|
21 | + } |
|
22 | + if ((Maphper::FIND_LIKE | Maphper::FIND_STARTS) & $mode) { |
|
23 | + $value .= '%'; |
|
24 | + } |
|
21 | 25 | return $value; |
22 | 26 | } |
23 | 27 | } |
@@ -16,8 +16,11 @@ discard block |
||
16 | 16 | } |
17 | 17 | |
18 | 18 | private function getOperator($mode) { |
19 | - if (\Maphper\Maphper::FIND_BIT & $mode) return '&'; |
|
20 | - else if (\Maphper\Maphper::FIND_NOT & $mode) return '!='; |
|
19 | + if (\Maphper\Maphper::FIND_BIT & $mode) { |
|
20 | + return '&'; |
|
21 | + } else if (\Maphper\Maphper::FIND_NOT & $mode) { |
|
22 | + return '!='; |
|
23 | + } |
|
21 | 24 | |
22 | 25 | return $this->getEqualsOperators($mode); |
23 | 26 | } |
@@ -25,10 +28,15 @@ discard block |
||
25 | 28 | private function getEqualsOperators($mode) { |
26 | 29 | $operator = ""; |
27 | 30 | |
28 | - if (\Maphper\Maphper::FIND_GREATER & $mode) $operator = '>'; |
|
29 | - else if (\Maphper\Maphper::FIND_LESS & $mode) $operator = '<'; |
|
31 | + if (\Maphper\Maphper::FIND_GREATER & $mode) { |
|
32 | + $operator = '>'; |
|
33 | + } else if (\Maphper\Maphper::FIND_LESS & $mode) { |
|
34 | + $operator = '<'; |
|
35 | + } |
|
30 | 36 | |
31 | - if (\Maphper\Maphper::FIND_EXACT & $mode) $operator .= '='; |
|
37 | + if (\Maphper\Maphper::FIND_EXACT & $mode) { |
|
38 | + $operator .= '='; |
|
39 | + } |
|
32 | 40 | |
33 | 41 | return $operator; |
34 | 42 | } |
@@ -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,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 |
@@ -12,7 +12,9 @@ |
||
12 | 12 | } |
13 | 13 | |
14 | 14 | public function current() { |
15 | - if ($this->intermediateName) return $this->iterator->current()->{$this->intermediateName}; |
|
15 | + if ($this->intermediateName) { |
|
16 | + return $this->iterator->current()->{$this->intermediateName}; |
|
17 | + } |
|
16 | 18 | return $this->iterator->current(); |
17 | 19 | } |
18 | 20 |
@@ -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 | |
@@ -41,8 +45,10 @@ discard block |
||
41 | 45 | $recordsToLoad = []; |
42 | 46 | //Get a list of records by FK to eager load |
43 | 47 | foreach ($this->siblings as $sibling) { |
44 | - if ($sibling->parentField === $this->parentField) // Ensure that it is only loading records from the same type of relation |
|
48 | + if ($sibling->parentField === $this->parentField) { |
|
49 | + // Ensure that it is only loading records from the same type of relation |
|
45 | 50 | $recordsToLoad[] = $sibling->parentObject->{$sibling->parentField}; |
51 | + } |
|
46 | 52 | } |
47 | 53 | |
48 | 54 | $recordsToLoad = array_unique($recordsToLoad); |
@@ -59,7 +65,9 @@ discard block |
||
59 | 65 | } |
60 | 66 | |
61 | 67 | foreach ($this->siblings as $sibling) { |
62 | - if (isset($cache[$sibling->parentObject->{$this->parentField}])) $sibling->data = $cache[$sibling->parentObject->{$this->parentField}]; |
|
68 | + if (isset($cache[$sibling->parentObject->{$this->parentField}])) { |
|
69 | + $sibling->data = $cache[$sibling->parentObject->{$this->parentField}]; |
|
70 | + } |
|
63 | 71 | } |
64 | 72 | /* |
65 | 73 | foreach ($this->siblings as $sibling) { |
@@ -70,13 +78,18 @@ discard block |
||
70 | 78 | } |
71 | 79 | |
72 | 80 | public function __call($func, array $args = []) { |
73 | - if ($this->lazyLoad() == null) return ''; |
|
81 | + if ($this->lazyLoad() == null) { |
|
82 | + return ''; |
|
83 | + } |
|
74 | 84 | return call_user_func_array([$this->lazyLoad(), $func], $args); |
75 | 85 | } |
76 | 86 | |
77 | 87 | public function __get($name) { |
78 | - if ($this->lazyLoad()) return $this->lazyLoad()->$name; |
|
79 | - else return null; |
|
88 | + if ($this->lazyLoad()) { |
|
89 | + return $this->lazyLoad()->$name; |
|
90 | + } else { |
|
91 | + return null; |
|
92 | + } |
|
80 | 93 | } |
81 | 94 | |
82 | 95 | public function __isset($name) { |
@@ -32,8 +32,12 @@ discard block |
||
32 | 32 | list($relatedField, $valueField, $mapper) = $this->getOtherFieldNameInfo(); |
33 | 33 | $this->results = $data; |
34 | 34 | $this->object = $parentObject; |
35 | - if (empty($parentObject->{$relatedField})) return; |
|
36 | - foreach ($data as $dt) $this[] = $dt; |
|
35 | + if (empty($parentObject->{$relatedField})) { |
|
36 | + return; |
|
37 | + } |
|
38 | + foreach ($data as $dt) { |
|
39 | + $this[] = $dt; |
|
40 | + } |
|
37 | 41 | } |
38 | 42 | |
39 | 43 | //bit hacky, breaking encapsulation, but simplest way to work out the info for the other side of the many:many relationship. |
@@ -88,8 +92,9 @@ discard block |
||
88 | 92 | |
89 | 93 | public function offsetSet($name, $value) { |
90 | 94 | list($relatedField, $valueField, $mapper) = $this->getOtherFieldNameInfo(); |
91 | - if ($this->autoTraverse) $this->offsetSetAutotraverse($value, $relatedField, $valueField); |
|
92 | - else if ($this->doUpdateInterMapper($value, $relatedField, $valueField)) { |
|
95 | + if ($this->autoTraverse) { |
|
96 | + $this->offsetSetAutotraverse($value, $relatedField, $valueField); |
|
97 | + } else if ($this->doUpdateInterMapper($value, $relatedField, $valueField)) { |
|
93 | 98 | $record = $value; |
94 | 99 | $record->{$this->parentField} = $value->{$this->intermediateName}->{$this->localField}; |
95 | 100 | $record->$valueField = $this->object->{$relatedField}; |