@@ -2,20 +2,20 @@ |
||
2 | 2 | namespace Maphper\Lib\Sql; |
3 | 3 | |
4 | 4 | class Between implements WhereConditional { |
5 | - public function matches($key, $value, $mode) { |
|
6 | - return is_array($value) && \Maphper\Maphper::FIND_BETWEEN & $mode; |
|
7 | - } |
|
5 | + public function matches($key, $value, $mode) { |
|
6 | + return is_array($value) && \Maphper\Maphper::FIND_BETWEEN & $mode; |
|
7 | + } |
|
8 | 8 | |
9 | - public function getSql($key, $value, $mode) { |
|
10 | - return [ |
|
11 | - 'sql' => [ |
|
12 | - $key . '>= :' . $key . 'from', |
|
13 | - $key . ' <= :' . $key . 'to' |
|
14 | - ], |
|
15 | - 'args' => [ |
|
16 | - $key . 'from' => $value[0], |
|
17 | - $key . 'to' => $value[1] |
|
18 | - ] |
|
19 | - ]; |
|
20 | - } |
|
9 | + public function getSql($key, $value, $mode) { |
|
10 | + return [ |
|
11 | + 'sql' => [ |
|
12 | + $key . '>= :' . $key . 'from', |
|
13 | + $key . ' <= :' . $key . 'to' |
|
14 | + ], |
|
15 | + 'args' => [ |
|
16 | + $key . 'from' => $value[0], |
|
17 | + $key . 'to' => $value[1] |
|
18 | + ] |
|
19 | + ]; |
|
20 | + } |
|
21 | 21 | } |
@@ -2,15 +2,15 @@ |
||
2 | 2 | namespace Maphper\Lib\Sql; |
3 | 3 | |
4 | 4 | class NullConditional implements WhereConditional { |
5 | - public function matches($key, $value, $mode) { |
|
6 | - return $value === NULL; |
|
7 | - } |
|
5 | + public function matches($key, $value, $mode) { |
|
6 | + return $value === NULL; |
|
7 | + } |
|
8 | 8 | |
9 | - public function getSql($key, $value, $mode) { |
|
10 | - $nullSql = $key . ' IS '; |
|
11 | - if (\Maphper\Maphper::FIND_NOT & $mode) $nullSql .= 'NOT '; |
|
12 | - $sql = [$nullSql . 'NULL']; |
|
9 | + public function getSql($key, $value, $mode) { |
|
10 | + $nullSql = $key . ' IS '; |
|
11 | + if (\Maphper\Maphper::FIND_NOT & $mode) $nullSql .= 'NOT '; |
|
12 | + $sql = [$nullSql . 'NULL']; |
|
13 | 13 | |
14 | - return ['args' => [], 'sql' => $sql]; |
|
15 | - } |
|
14 | + return ['args' => [], 'sql' => $sql]; |
|
15 | + } |
|
16 | 16 | } |
@@ -8,7 +8,9 @@ |
||
8 | 8 | |
9 | 9 | public function getSql($key, $value, $mode) { |
10 | 10 | $nullSql = $key . ' IS '; |
11 | - if (\Maphper\Maphper::FIND_NOT & $mode) $nullSql .= 'NOT '; |
|
11 | + if (\Maphper\Maphper::FIND_NOT & $mode) { |
|
12 | + $nullSql .= 'NOT '; |
|
13 | + } |
|
12 | 14 | $sql = [$nullSql . 'NULL']; |
13 | 15 | |
14 | 16 | return ['args' => [], 'sql' => $sql]; |
@@ -3,21 +3,21 @@ |
||
3 | 3 | use Maphper\Maphper; |
4 | 4 | |
5 | 5 | class Like implements WhereConditional { |
6 | - public function matches($key, $value, $mode) { |
|
7 | - return (Maphper::FIND_LIKE | Maphper::FIND_STARTS | |
|
8 | - Maphper::FIND_ENDS | Maphper::FIND_NOCASE) & $mode; |
|
9 | - } |
|
6 | + public function matches($key, $value, $mode) { |
|
7 | + return (Maphper::FIND_LIKE | Maphper::FIND_STARTS | |
|
8 | + Maphper::FIND_ENDS | Maphper::FIND_NOCASE) & $mode; |
|
9 | + } |
|
10 | 10 | |
11 | - public function getSql($key, $value, $mode) { |
|
12 | - return [ |
|
13 | - 'sql' => [$key . ' LIKE :' . $key], |
|
14 | - 'args' => [$key => $this->getValue($value, $mode)] |
|
15 | - ]; |
|
16 | - } |
|
11 | + public function getSql($key, $value, $mode) { |
|
12 | + return [ |
|
13 | + 'sql' => [$key . ' LIKE :' . $key], |
|
14 | + 'args' => [$key => $this->getValue($value, $mode)] |
|
15 | + ]; |
|
16 | + } |
|
17 | 17 | |
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 .= '%'; |
|
21 | - return $value; |
|
22 | - } |
|
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 .= '%'; |
|
21 | + return $value; |
|
22 | + } |
|
23 | 23 | } |
@@ -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 | } |
@@ -58,7 +58,7 @@ |
||
58 | 58 | |
59 | 59 | private function convertDates($value) { |
60 | 60 | if ($value instanceof \DateTime) { |
61 | - if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
61 | + if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
62 | 62 | else $value = $value->format('Y-m-d H:i:s'); |
63 | 63 | } |
64 | 64 | return $value; |
@@ -2,68 +2,68 @@ |
||
2 | 2 | namespace Maphper\Lib\Sql; |
3 | 3 | |
4 | 4 | class WhereBuilder { |
5 | - private $conditionals = []; |
|
5 | + private $conditionals = []; |
|
6 | 6 | |
7 | - public function __construct() { |
|
8 | - $defaultConditionals = [ |
|
9 | - 'Maphper\Lib\Sql\Between', |
|
10 | - 'Maphper\Lib\Sql\In', |
|
11 | - 'Maphper\Lib\Sql\NullConditional', |
|
12 | - 'Maphper\Lib\Sql\Like', |
|
13 | - 'Maphper\Lib\Sql\GeneralOperator' |
|
14 | - ]; |
|
7 | + public function __construct() { |
|
8 | + $defaultConditionals = [ |
|
9 | + 'Maphper\Lib\Sql\Between', |
|
10 | + 'Maphper\Lib\Sql\In', |
|
11 | + 'Maphper\Lib\Sql\NullConditional', |
|
12 | + 'Maphper\Lib\Sql\Like', |
|
13 | + 'Maphper\Lib\Sql\GeneralOperator' |
|
14 | + ]; |
|
15 | 15 | |
16 | - foreach ($defaultConditionals as $conditional) $this->addConditional(new $conditional); |
|
17 | - } |
|
16 | + foreach ($defaultConditionals as $conditional) $this->addConditional(new $conditional); |
|
17 | + } |
|
18 | 18 | |
19 | - public function addConditional(WhereConditional $conditional) { |
|
20 | - $this->conditionals[] = $conditional; |
|
21 | - } |
|
19 | + public function addConditional(WhereConditional $conditional) { |
|
20 | + $this->conditionals[] = $conditional; |
|
21 | + } |
|
22 | 22 | |
23 | 23 | public function createSql($fields, $mode = \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND) { |
24 | 24 | $args = []; |
25 | 25 | $sql = []; |
26 | 26 | |
27 | - foreach ($fields as $key => $value) { |
|
28 | - $value = $this->convertDates($value); |
|
27 | + foreach ($fields as $key => $value) { |
|
28 | + $value = $this->convertDates($value); |
|
29 | 29 | |
30 | - if (is_object($value)) continue; |
|
30 | + if (is_object($value)) continue; |
|
31 | 31 | $result = $this->getResult($key, $value, $mode); |
32 | - $sql = array_merge($sql, (array)$result['sql']); |
|
33 | - $args = array_merge($args, $result['args']); |
|
34 | - } |
|
32 | + $sql = array_merge($sql, (array)$result['sql']); |
|
33 | + $args = array_merge($args, $result['args']); |
|
34 | + } |
|
35 | 35 | |
36 | 36 | return ['args' => $args, 'sql' => $this->sqlArrayToString($sql, $mode)]; |
37 | 37 | } |
38 | 38 | |
39 | - /* |
|
39 | + /* |
|
40 | 40 | * Either get sql from a conditional or call createSql again because the mode needs to be changed |
41 | 41 | */ |
42 | - private function getResult($key, $value, $mode) { |
|
43 | - if (is_numeric($key) && is_array($value)) return $this->createSql($value, $key); |
|
44 | - return $this->getConditional($key, $value, $mode); |
|
45 | - } |
|
42 | + private function getResult($key, $value, $mode) { |
|
43 | + if (is_numeric($key) && is_array($value)) return $this->createSql($value, $key); |
|
44 | + return $this->getConditional($key, $value, $mode); |
|
45 | + } |
|
46 | 46 | |
47 | - private function sqlArrayToString($sql, $mode) { |
|
48 | - if (\Maphper\Maphper::FIND_OR & $mode) $query = implode(' OR ', $sql); |
|
47 | + private function sqlArrayToString($sql, $mode) { |
|
48 | + if (\Maphper\Maphper::FIND_OR & $mode) $query = implode(' OR ', $sql); |
|
49 | 49 | else $query = implode(' AND ', $sql); |
50 | 50 | if (!empty($query)) $query = '(' . $query . ')'; |
51 | - return $query; |
|
52 | - } |
|
51 | + return $query; |
|
52 | + } |
|
53 | 53 | |
54 | - private function getConditional($key, $value, $mode) { |
|
55 | - foreach ($this->conditionals as $conditional) { |
|
56 | - if ($conditional->matches($key, $value, $mode)) |
|
57 | - return $conditional->getSql($key, $value, $mode); |
|
58 | - } |
|
59 | - throw new \Exception("Invalid WHERE query"); |
|
60 | - } |
|
54 | + private function getConditional($key, $value, $mode) { |
|
55 | + foreach ($this->conditionals as $conditional) { |
|
56 | + if ($conditional->matches($key, $value, $mode)) |
|
57 | + return $conditional->getSql($key, $value, $mode); |
|
58 | + } |
|
59 | + throw new \Exception("Invalid WHERE query"); |
|
60 | + } |
|
61 | 61 | |
62 | - private function convertDates($value) { |
|
63 | - if ($value instanceof \DateTime) { |
|
64 | - if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
65 | - else $value = $value->format('Y-m-d H:i:s'); |
|
66 | - } |
|
67 | - return $value; |
|
68 | - } |
|
62 | + private function convertDates($value) { |
|
63 | + if ($value instanceof \DateTime) { |
|
64 | + if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
65 | + else $value = $value->format('Y-m-d H:i:s'); |
|
66 | + } |
|
67 | + return $value; |
|
68 | + } |
|
69 | 69 | } |
@@ -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 | $sql = array_merge($sql, (array)$result['sql']); |
33 | 37 | $args = array_merge($args, $result['args']); |
@@ -40,29 +44,40 @@ discard block |
||
40 | 44 | * Either get sql from a conditional or call createSql again because the mode needs to be changed |
41 | 45 | */ |
42 | 46 | private function getResult($key, $value, $mode) { |
43 | - if (is_numeric($key) && is_array($value)) return $this->createSql($value, $key); |
|
47 | + if (is_numeric($key) && is_array($value)) { |
|
48 | + return $this->createSql($value, $key); |
|
49 | + } |
|
44 | 50 | return $this->getConditional($key, $value, $mode); |
45 | 51 | } |
46 | 52 | |
47 | 53 | private function sqlArrayToString($sql, $mode) { |
48 | - if (\Maphper\Maphper::FIND_OR & $mode) $query = implode(' OR ', $sql); |
|
49 | - else $query = implode(' AND ', $sql); |
|
50 | - if (!empty($query)) $query = '(' . $query . ')'; |
|
54 | + if (\Maphper\Maphper::FIND_OR & $mode) { |
|
55 | + $query = implode(' OR ', $sql); |
|
56 | + } else { |
|
57 | + $query = implode(' AND ', $sql); |
|
58 | + } |
|
59 | + if (!empty($query)) { |
|
60 | + $query = '(' . $query . ')'; |
|
61 | + } |
|
51 | 62 | return $query; |
52 | 63 | } |
53 | 64 | |
54 | 65 | private function getConditional($key, $value, $mode) { |
55 | 66 | foreach ($this->conditionals as $conditional) { |
56 | - if ($conditional->matches($key, $value, $mode)) |
|
57 | - return $conditional->getSql($key, $value, $mode); |
|
67 | + if ($conditional->matches($key, $value, $mode)) { |
|
68 | + return $conditional->getSql($key, $value, $mode); |
|
69 | + } |
|
58 | 70 | } |
59 | 71 | throw new \Exception("Invalid WHERE query"); |
60 | 72 | } |
61 | 73 | |
62 | 74 | private function convertDates($value) { |
63 | 75 | if ($value instanceof \DateTime) { |
64 | - if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
65 | - else $value = $value->format('Y-m-d H:i:s'); |
|
76 | + if ($value->format('H:i:s') == '00:00:00') { |
|
77 | + $value = $value->format('Y-m-d'); |
|
78 | + } else { |
|
79 | + $value = $value->format('Y-m-d H:i:s'); |
|
80 | + } |
|
66 | 81 | } |
67 | 82 | return $value; |
68 | 83 | } |
@@ -3,33 +3,33 @@ |
||
3 | 3 | use Maphper\Maphper; |
4 | 4 | |
5 | 5 | class GeneralOperator implements WhereConditional { |
6 | - public function matches($key, $value, $mode) { |
|
7 | - return (Maphper::FIND_BIT ^ Maphper::FIND_GREATER ^ Maphper::FIND_LESS ^ Maphper::FIND_NOT & $mode) |
|
8 | - || Maphper::FIND_EXACT & $mode; |
|
9 | - } |
|
6 | + public function matches($key, $value, $mode) { |
|
7 | + return (Maphper::FIND_BIT ^ Maphper::FIND_GREATER ^ Maphper::FIND_LESS ^ Maphper::FIND_NOT & $mode) |
|
8 | + || Maphper::FIND_EXACT & $mode; |
|
9 | + } |
|
10 | 10 | |
11 | - public function getSql($key, $value, $mode) { |
|
12 | - return [ |
|
13 | - 'sql' => [$key . ' ' . $this->getOperator($mode) . ' :' . $key], |
|
14 | - 'args' => [$key => $value] |
|
15 | - ]; |
|
16 | - } |
|
11 | + public function getSql($key, $value, $mode) { |
|
12 | + return [ |
|
13 | + 'sql' => [$key . ' ' . $this->getOperator($mode) . ' :' . $key], |
|
14 | + 'args' => [$key => $value] |
|
15 | + ]; |
|
16 | + } |
|
17 | 17 | |
18 | - private function getOperator($mode) { |
|
19 | - if (\Maphper\Maphper::FIND_BIT & $mode) return '&'; |
|
20 | - else if (\Maphper\Maphper::FIND_NOT & $mode) return '!='; |
|
18 | + private function getOperator($mode) { |
|
19 | + if (\Maphper\Maphper::FIND_BIT & $mode) return '&'; |
|
20 | + else if (\Maphper\Maphper::FIND_NOT & $mode) return '!='; |
|
21 | 21 | |
22 | - return $this->getEqualsOperators($mode); |
|
23 | - } |
|
22 | + return $this->getEqualsOperators($mode); |
|
23 | + } |
|
24 | 24 | |
25 | - private function getEqualsOperators($mode) { |
|
26 | - $operator = ""; |
|
25 | + private function getEqualsOperators($mode) { |
|
26 | + $operator = ""; |
|
27 | 27 | |
28 | - if (\Maphper\Maphper::FIND_GREATER & $mode) $operator = '>'; |
|
29 | - else if (\Maphper\Maphper::FIND_LESS & $mode) $operator = '<'; |
|
28 | + if (\Maphper\Maphper::FIND_GREATER & $mode) $operator = '>'; |
|
29 | + else if (\Maphper\Maphper::FIND_LESS & $mode) $operator = '<'; |
|
30 | 30 | |
31 | - if (\Maphper\Maphper::FIND_EXACT & $mode) $operator .= '='; |
|
31 | + if (\Maphper\Maphper::FIND_EXACT & $mode) $operator .= '='; |
|
32 | 32 | |
33 | - return $operator; |
|
34 | - } |
|
33 | + return $operator; |
|
34 | + } |
|
35 | 35 | } |
@@ -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,27 +13,27 @@ |
||
13 | 13 | return $obj; |
14 | 14 | } |
15 | 15 | |
16 | - private function tryToGetDateObjFromString($obj) { |
|
17 | - try { |
|
18 | - $date = new \DateTime($obj); |
|
19 | - if ($date->format('Y-m-d H:i:s') == substr($obj, 0, 20) || $date->format('Y-m-d') == substr($obj, 0, 10)) $obj = $date; |
|
20 | - } |
|
21 | - catch (\Exception $e) { //Doesn't need to do anything as the try/catch is working out whether $obj is a date |
|
22 | - } |
|
23 | - return $obj; |
|
24 | - } |
|
25 | - |
|
26 | - private function isIterable($obj) { |
|
27 | - return is_array($obj) || (is_object($obj) && ($obj instanceof \Iterator)); |
|
28 | - } |
|
29 | - |
|
30 | - private function isPossiblyDateString($obj) { |
|
31 | - return is_string($obj) && isset($obj[0]) && is_numeric($obj[0]) && strlen($obj) <= 20; |
|
32 | - } |
|
16 | + private function tryToGetDateObjFromString($obj) { |
|
17 | + try { |
|
18 | + $date = new \DateTime($obj); |
|
19 | + if ($date->format('Y-m-d H:i:s') == substr($obj, 0, 20) || $date->format('Y-m-d') == substr($obj, 0, 10)) $obj = $date; |
|
20 | + } |
|
21 | + catch (\Exception $e) { //Doesn't need to do anything as the try/catch is working out whether $obj is a date |
|
22 | + } |
|
23 | + return $obj; |
|
24 | + } |
|
25 | + |
|
26 | + private function isIterable($obj) { |
|
27 | + return is_array($obj) || (is_object($obj) && ($obj instanceof \Iterator)); |
|
28 | + } |
|
29 | + |
|
30 | + private function isPossiblyDateString($obj) { |
|
31 | + return is_string($obj) && isset($obj[0]) && is_numeric($obj[0]) && strlen($obj) <= 20; |
|
32 | + } |
|
33 | 33 | |
34 | 34 | private function checkCache($obj, $reset) { |
35 | 35 | if ($reset) $this->processCache = new \SplObjectStorage(); |
36 | - if (!is_object($obj)) return false; |
|
36 | + if (!is_object($obj)) return false; |
|
37 | 37 | |
38 | 38 | if ($this->processCache->contains($obj)) return $obj; |
39 | 39 | else $this->processCache->attach($obj, true); |
@@ -6,19 +6,26 @@ discard block |
||
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; |
|
10 | - |
|
11 | - if ($this->isIterable($obj)) foreach ($obj as &$o) $o = $this->replaceDates($o, false); |
|
12 | - if ($this->isPossiblyDateString($obj)) $obj = $this->tryToGetDateObjFromString($obj); |
|
9 | + if ($this->checkCache($obj, $reset)) { |
|
10 | + return $obj; |
|
11 | + } |
|
12 | + |
|
13 | + if ($this->isIterable($obj)) { |
|
14 | + foreach ($obj as &$o) $o = $this->replaceDates($o, false); |
|
15 | + } |
|
16 | + if ($this->isPossiblyDateString($obj)) { |
|
17 | + $obj = $this->tryToGetDateObjFromString($obj); |
|
18 | + } |
|
13 | 19 | return $obj; |
14 | 20 | } |
15 | 21 | |
16 | 22 | private function tryToGetDateObjFromString($obj) { |
17 | 23 | try { |
18 | 24 | $date = new \DateTime($obj); |
19 | - if ($date->format('Y-m-d H:i:s') == substr($obj, 0, 20) || $date->format('Y-m-d') == substr($obj, 0, 10)) $obj = $date; |
|
20 | - } |
|
21 | - catch (\Exception $e) { //Doesn't need to do anything as the try/catch is working out whether $obj is a date |
|
25 | + if ($date->format('Y-m-d H:i:s') == substr($obj, 0, 20) || $date->format('Y-m-d') == substr($obj, 0, 10)) { |
|
26 | + $obj = $date; |
|
27 | + } |
|
28 | + } catch (\Exception $e) { //Doesn't need to do anything as the try/catch is working out whether $obj is a date |
|
22 | 29 | } |
23 | 30 | return $obj; |
24 | 31 | } |
@@ -32,11 +39,18 @@ discard block |
||
32 | 39 | } |
33 | 40 | |
34 | 41 | private function checkCache($obj, $reset) { |
35 | - if ($reset) $this->processCache = new \SplObjectStorage(); |
|
36 | - if (!is_object($obj)) return false; |
|
42 | + if ($reset) { |
|
43 | + $this->processCache = new \SplObjectStorage(); |
|
44 | + } |
|
45 | + if (!is_object($obj)) { |
|
46 | + return false; |
|
47 | + } |
|
37 | 48 | |
38 | - if ($this->processCache->contains($obj)) return $obj; |
|
39 | - else $this->processCache->attach($obj, true); |
|
49 | + if ($this->processCache->contains($obj)) { |
|
50 | + return $obj; |
|
51 | + } else { |
|
52 | + $this->processCache->attach($obj, true); |
|
53 | + } |
|
40 | 54 | |
41 | 55 | return false; |
42 | 56 | } |
@@ -11,9 +11,9 @@ discard block |
||
11 | 11 | $this->writeClosure = function ($field, $value) use ($object) { $object->$field = $value; }; |
12 | 12 | } |
13 | 13 | else { |
14 | - $visOverride = $this; |
|
14 | + $visOverride = $this; |
|
15 | 15 | $this->readClosure = function() use ($visOverride) { |
16 | - return (object) array_filter(get_object_vars($this), [$visOverride, 'isReturnableDataType']); |
|
16 | + return (object) array_filter(get_object_vars($this), [$visOverride, 'isReturnableDataType']); |
|
17 | 17 | }; |
18 | 18 | $this->readClosure = $this->readClosure->bindTo($object, $object); |
19 | 19 | |
@@ -23,9 +23,9 @@ discard block |
||
23 | 23 | |
24 | 24 | } |
25 | 25 | |
26 | - public function isReturnableDataType($v) { |
|
27 | - return is_scalar($v) || is_null($v) || (is_object($v) && $v instanceof \DateTime); |
|
28 | - } |
|
26 | + public function isReturnableDataType($v) { |
|
27 | + return is_scalar($v) || is_null($v) || (is_object($v) && $v instanceof \DateTime); |
|
28 | + } |
|
29 | 29 | |
30 | 30 | public function getProperties() { |
31 | 31 | return ($this->readClosure)(); |
@@ -7,17 +7,17 @@ discard block |
||
7 | 7 | |
8 | 8 | public function __construct($object) { |
9 | 9 | if ($object instanceof \stdclass) { |
10 | - $this->readClosure = function() use ($object) { return $object; }; |
|
11 | - $this->writeClosure = function ($field, $value) use ($object) { $object->$field = $value; }; |
|
10 | + $this->readClosure = function() use ($object) { return $object; }; |
|
11 | + $this->writeClosure = function($field, $value) use ($object) { $object->$field = $value; }; |
|
12 | 12 | } |
13 | 13 | else { |
14 | 14 | $visOverride = $this; |
15 | 15 | $this->readClosure = function() use ($visOverride) { |
16 | - return (object) array_filter(get_object_vars($this), [$visOverride, 'isReturnableDataType']); |
|
16 | + return (object)array_filter(get_object_vars($this), [$visOverride, 'isReturnableDataType']); |
|
17 | 17 | }; |
18 | 18 | $this->readClosure = $this->readClosure->bindTo($object, $object); |
19 | 19 | |
20 | - $this->writeClosure = function ($field, $value) { $this->$field = $value; }; |
|
20 | + $this->writeClosure = function($field, $value) { $this->$field = $value; }; |
|
21 | 21 | $this->writeClosure = $this->writeClosure->bindTo($object, $object); |
22 | 22 | } |
23 | 23 | |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | public function write($data) { |
35 | 35 | if ($data != null) { |
36 | 36 | foreach ($data as $key => $value) { |
37 | - ($this->writeClosure)($key, $this->processDates($value)); |
|
37 | + ($this->writeClosure)($key, $this->processDates($value)); |
|
38 | 38 | } |
39 | 39 | } |
40 | 40 | } |
@@ -9,8 +9,7 @@ |
||
9 | 9 | if ($object instanceof \stdclass) { |
10 | 10 | $this->readClosure = function() use ($object) { return $object; }; |
11 | 11 | $this->writeClosure = function ($field, $value) use ($object) { $object->$field = $value; }; |
12 | - } |
|
13 | - else { |
|
12 | + } else { |
|
14 | 13 | $visOverride = $this; |
15 | 14 | $this->readClosure = function() use ($visOverride) { |
16 | 15 | return (object) array_filter(get_object_vars($this), [$visOverride, 'isReturnableDataType']); |
@@ -8,7 +8,7 @@ discard block |
||
8 | 8 | private $parent; |
9 | 9 | |
10 | 10 | public function __construct(\Maphper\Maphper $parent, $className = null) { |
11 | - $this->parent = $parent; |
|
11 | + $this->parent = $parent; |
|
12 | 12 | $this->className = $className; |
13 | 13 | } |
14 | 14 | |
@@ -25,12 +25,12 @@ discard block |
||
25 | 25 | return $object; |
26 | 26 | } |
27 | 27 | |
28 | - private function addRelationData($object, $name, $relation, $siblings) { |
|
29 | - if (isset($object->$name) && !($object->$name instanceof \Maphper\Relation) ) { |
|
30 | - //After overwriting the relation, does the parent object ($object) need overwriting as well? |
|
31 | - if ($relation->overwrite($object, $object->$name)) $this->parent[] = $object; |
|
32 | - } |
|
28 | + private function addRelationData($object, $name, $relation, $siblings) { |
|
29 | + if (isset($object->$name) && !($object->$name instanceof \Maphper\Relation) ) { |
|
30 | + //After overwriting the relation, does the parent object ($object) need overwriting as well? |
|
31 | + if ($relation->overwrite($object, $object->$name)) $this->parent[] = $object; |
|
32 | + } |
|
33 | 33 | |
34 | - $object->$name = $relation->getData($object, $siblings); |
|
35 | - } |
|
34 | + $object->$name = $relation->getData($object, $siblings); |
|
35 | + } |
|
36 | 36 | } |
@@ -26,7 +26,7 @@ |
||
26 | 26 | } |
27 | 27 | |
28 | 28 | private function addRelationData($object, $name, $relation, $siblings) { |
29 | - if (isset($object->$name) && !($object->$name instanceof \Maphper\Relation) ) { |
|
29 | + if (isset($object->$name) && !($object->$name instanceof \Maphper\Relation)) { |
|
30 | 30 | //After overwriting the relation, does the parent object ($object) need overwriting as well? |
31 | 31 | if ($relation->overwrite($object, $object->$name)) $this->parent[] = $object; |
32 | 32 | } |
@@ -21,14 +21,18 @@ |
||
21 | 21 | |
22 | 22 | public function wrap($relations, $object, $siblings = []) { |
23 | 23 | //see if any relations need overwriting |
24 | - foreach ($relations as $name => $relation) $this->addRelationData($object, $name, $relation, $siblings); |
|
24 | + foreach ($relations as $name => $relation) { |
|
25 | + $this->addRelationData($object, $name, $relation, $siblings); |
|
26 | + } |
|
25 | 27 | return $object; |
26 | 28 | } |
27 | 29 | |
28 | 30 | private function addRelationData($object, $name, $relation, $siblings) { |
29 | 31 | if (isset($object->$name) && !($object->$name instanceof \Maphper\Relation) ) { |
30 | 32 | //After overwriting the relation, does the parent object ($object) need overwriting as well? |
31 | - if ($relation->overwrite($object, $object->$name)) $this->parent[] = $object; |
|
33 | + if ($relation->overwrite($object, $object->$name)) { |
|
34 | + $this->parent[] = $object; |
|
35 | + } |
|
32 | 36 | } |
33 | 37 | |
34 | 38 | $object->$name = $relation->getData($object, $siblings); |
@@ -3,14 +3,14 @@ discard block |
||
3 | 3 | class MysqlAdapter implements DatabaseAdapter { |
4 | 4 | private $pdo; |
5 | 5 | private $stmtCache; |
6 | - private $generalEditor; |
|
6 | + private $generalEditor; |
|
7 | 7 | |
8 | 8 | public function __construct(\PDO $pdo) { |
9 | 9 | $this->pdo = $pdo; |
10 | 10 | //Set to strict mode to detect 'out of range' errors, action at a distance but it needs to be set for all INSERT queries |
11 | 11 | $this->pdo->query('SET sql_mode = STRICT_ALL_TABLES'); |
12 | - $this->stmtCache = new StmtCache($pdo); |
|
13 | - $this->generalEditor = new GeneralEditDatabase($this->pdo, ['short_string_max_len' => 191]); |
|
12 | + $this->stmtCache = new StmtCache($pdo); |
|
13 | + $this->generalEditor = new GeneralEditDatabase($this->pdo, ['short_string_max_len' => 191]); |
|
14 | 14 | } |
15 | 15 | |
16 | 16 | public function quote($str) { |
@@ -20,13 +20,13 @@ discard block |
||
20 | 20 | public function query(\Maphper\Lib\Query $query) { |
21 | 21 | $stmt = $this->stmtCache->getCachedStmt($query->getSql()); |
22 | 22 | $args = $query->getArgs(); |
23 | - $stmt->execute($args); |
|
23 | + $stmt->execute($args); |
|
24 | 24 | |
25 | 25 | return $stmt; |
26 | 26 | } |
27 | 27 | |
28 | - private function alterColumns($table, array $primaryKey, $data) { |
|
29 | - foreach ($data as $key => $value) { |
|
28 | + private function alterColumns($table, array $primaryKey, $data) { |
|
29 | + foreach ($data as $key => $value) { |
|
30 | 30 | if ($this->generalEditor->isNotSavableType($value, $key, $primaryKey)) continue; |
31 | 31 | |
32 | 32 | $type = $this->generalEditor->getType($value); |
@@ -38,11 +38,11 @@ discard block |
||
38 | 38 | $this->pdo->query('ALTER TABLE ' . $table . ' MODIFY ' . $this->quote($key) . ' ' . $type); |
39 | 39 | } |
40 | 40 | } |
41 | - } |
|
41 | + } |
|
42 | 42 | |
43 | 43 | public function alterDatabase($table, array $primaryKey, $data) { |
44 | 44 | $this->generalEditor->createTable($table, $primaryKey, $data); |
45 | - $this->alterColumns($table, $primaryKey, $data); |
|
45 | + $this->alterColumns($table, $primaryKey, $data); |
|
46 | 46 | } |
47 | 47 | |
48 | 48 | public function lastInsertId() { |
@@ -27,14 +27,17 @@ 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 | |
34 | 36 | try { |
35 | - if (!$this->pdo->query('ALTER TABLE ' . $table . ' ADD ' . $this->quote($key) . ' ' . $type)) throw new \Exception('Could not alter table'); |
|
36 | - } |
|
37 | - catch (\Exception $e) { |
|
37 | + if (!$this->pdo->query('ALTER TABLE ' . $table . ' ADD ' . $this->quote($key) . ' ' . $type)) { |
|
38 | + throw new \Exception('Could not alter table'); |
|
39 | + } |
|
40 | + } catch (\Exception $e) { |
|
38 | 41 | $this->pdo->query('ALTER TABLE ' . $table . ' MODIFY ' . $this->quote($key) . ' ' . $type); |
39 | 42 | } |
40 | 43 | } |
@@ -57,7 +60,9 @@ discard block |
||
57 | 60 | $keyName = $this->quote(implode('_', $fields)); |
58 | 61 | |
59 | 62 | $results = $this->pdo->query('SHOW INDEX FROM ' . $this->quote($table) . ' WHERE Key_Name = "' . $keyName . '"'); |
60 | - if ($results && count($results->fetchAll()) == 0) $this->pdo->query('CREATE INDEX ' . $keyName . ' ON ' . $this->quote($table) . ' (' . implode(', ', $fields) . ')'); |
|
63 | + if ($results && count($results->fetchAll()) == 0) { |
|
64 | + $this->pdo->query('CREATE INDEX ' . $keyName . ' ON ' . $this->quote($table) . ' (' . implode(', ', $fields) . ')'); |
|
65 | + } |
|
61 | 66 | } |
62 | 67 | |
63 | 68 | public function optimiseColumns($table) { |