@@ -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 | } |
@@ -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 | } |
@@ -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 | } |
@@ -2,87 +2,87 @@ |
||
2 | 2 | namespace Maphper\Lib\Sql; |
3 | 3 | |
4 | 4 | class WhereBuilder { |
5 | - private $conditionals = []; |
|
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 | - ]; |
|
15 | - |
|
16 | - foreach ($defaultConditionals as $conditional) $this->addConditional(new $conditional); |
|
17 | - } |
|
5 | + private $conditionals = []; |
|
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 | + ]; |
|
15 | + |
|
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 | - $result = $this->fixDuplicateArgs($args, $result); |
|
33 | - $sql = array_merge($sql, (array)$result['sql']); |
|
34 | - $args = array_merge($args, $result['args']); |
|
35 | - } |
|
32 | + $result = $this->fixDuplicateArgs($args, $result); |
|
33 | + $sql = array_merge($sql, (array)$result['sql']); |
|
34 | + $args = array_merge($args, $result['args']); |
|
35 | + } |
|
36 | 36 | |
37 | 37 | return ['args' => $args, 'sql' => $this->sqlArrayToString($sql, $mode)]; |
38 | 38 | } |
39 | 39 | |
40 | - // Returns result with duplicate issues removed |
|
41 | - private function fixDuplicateArgs($origArgs, $result) { |
|
42 | - $duplicates = array_intersect_key($result['args'], $origArgs); // Holds all keys in results already in the args |
|
43 | - if (count($duplicates) === 0) return $result; |
|
40 | + // Returns result with duplicate issues removed |
|
41 | + private function fixDuplicateArgs($origArgs, $result) { |
|
42 | + $duplicates = array_intersect_key($result['args'], $origArgs); // Holds all keys in results already in the args |
|
43 | + if (count($duplicates) === 0) return $result; |
|
44 | 44 | |
45 | - foreach ($duplicates as $argKey => $argVal) { |
|
46 | - $valHash = substr(md5($argVal), 0, 5); |
|
47 | - $newKey = $argKey . $valHash; |
|
45 | + foreach ($duplicates as $argKey => $argVal) { |
|
46 | + $valHash = substr(md5($argVal), 0, 5); |
|
47 | + $newKey = $argKey . $valHash; |
|
48 | 48 | |
49 | - // Replace occurences of duplicate key with key + hash as arg |
|
50 | - $result['sql'] = str_replace(':' . $argKey, ':' . $newKey, $result['sql']); |
|
51 | - unset($result['args'][$argKey]); |
|
52 | - $result['args'][$newKey] = $argVal; |
|
53 | - } |
|
49 | + // Replace occurences of duplicate key with key + hash as arg |
|
50 | + $result['sql'] = str_replace(':' . $argKey, ':' . $newKey, $result['sql']); |
|
51 | + unset($result['args'][$argKey]); |
|
52 | + $result['args'][$newKey] = $argVal; |
|
53 | + } |
|
54 | 54 | |
55 | - return $result; |
|
56 | - } |
|
55 | + return $result; |
|
56 | + } |
|
57 | 57 | |
58 | - /* |
|
58 | + /* |
|
59 | 59 | * Either get sql from a conditional or call createSql again because the mode needs to be changed |
60 | 60 | */ |
61 | - private function getResult($key, $value, $mode) { |
|
62 | - if (is_numeric($key) && is_array($value)) return $this->createSql($value, $key); |
|
63 | - return $this->getConditional($key, $value, $mode); |
|
64 | - } |
|
61 | + private function getResult($key, $value, $mode) { |
|
62 | + if (is_numeric($key) && is_array($value)) return $this->createSql($value, $key); |
|
63 | + return $this->getConditional($key, $value, $mode); |
|
64 | + } |
|
65 | 65 | |
66 | - private function sqlArrayToString($sql, $mode) { |
|
67 | - if (\Maphper\Maphper::FIND_OR & $mode) $query = implode(' OR ', $sql); |
|
66 | + private function sqlArrayToString($sql, $mode) { |
|
67 | + if (\Maphper\Maphper::FIND_OR & $mode) $query = implode(' OR ', $sql); |
|
68 | 68 | else $query = implode(' AND ', $sql); |
69 | 69 | if (!empty($query)) $query = '(' . $query . ')'; |
70 | - return $query; |
|
71 | - } |
|
72 | - |
|
73 | - private function getConditional($key, $value, $mode) { |
|
74 | - foreach ($this->conditionals as $conditional) { |
|
75 | - if ($conditional->matches($key, $value, $mode)) |
|
76 | - return $conditional->getSql($key, $value, $mode); |
|
77 | - } |
|
78 | - throw new \Exception("Invalid WHERE query"); |
|
79 | - } |
|
80 | - |
|
81 | - private function convertDates($value) { |
|
82 | - 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'); |
|
85 | - } |
|
86 | - return $value; |
|
87 | - } |
|
70 | + return $query; |
|
71 | + } |
|
72 | + |
|
73 | + private function getConditional($key, $value, $mode) { |
|
74 | + foreach ($this->conditionals as $conditional) { |
|
75 | + if ($conditional->matches($key, $value, $mode)) |
|
76 | + return $conditional->getSql($key, $value, $mode); |
|
77 | + } |
|
78 | + throw new \Exception("Invalid WHERE query"); |
|
79 | + } |
|
80 | + |
|
81 | + private function convertDates($value) { |
|
82 | + 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'); |
|
85 | + } |
|
86 | + return $value; |
|
87 | + } |
|
88 | 88 | } |
@@ -80,7 +80,7 @@ |
||
80 | 80 | |
81 | 81 | private function convertDates($value) { |
82 | 82 | if ($value instanceof \DateTimeInterface) { |
83 | - if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
83 | + if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
84 | 84 | else $value = $value->format('Y-m-d H:i:s'); |
85 | 85 | } |
86 | 86 | return $value; |
@@ -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 | } |
@@ -18,15 +18,15 @@ discard block |
||
18 | 18 | return $obj; |
19 | 19 | } |
20 | 20 | |
21 | - private function tryToGetDateObjFromString($obj) { |
|
22 | - try { |
|
23 | - $date = new \DateTime($obj); |
|
21 | + private function tryToGetDateObjFromString($obj) { |
|
22 | + try { |
|
23 | + $date = new \DateTime($obj); |
|
24 | 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 |
|
27 | - } |
|
28 | - return $obj; |
|
29 | - } |
|
25 | + } |
|
26 | + catch (\Exception $e) { //Doesn't need to do anything as the try/catch is working out whether $obj is a date |
|
27 | + } |
|
28 | + return $obj; |
|
29 | + } |
|
30 | 30 | |
31 | 31 | private function dateMatchesFormats($date, $str) { |
32 | 32 | foreach ($this->dateFormats as list($format, $len)) { |
@@ -35,17 +35,17 @@ discard block |
||
35 | 35 | return false; |
36 | 36 | } |
37 | 37 | |
38 | - private function isIterable($obj) { |
|
39 | - return is_array($obj) || (is_object($obj) && ($obj instanceof \Iterator)); |
|
40 | - } |
|
38 | + private function isIterable($obj) { |
|
39 | + return is_array($obj) || (is_object($obj) && ($obj instanceof \Iterator)); |
|
40 | + } |
|
41 | 41 | |
42 | - private function isPossiblyDateString($obj) { |
|
43 | - return is_string($obj) && isset($obj[0]) && is_numeric($obj[0]) && strlen($obj) <= 20; |
|
44 | - } |
|
42 | + private function isPossiblyDateString($obj) { |
|
43 | + return is_string($obj) && isset($obj[0]) && is_numeric($obj[0]) && strlen($obj) <= 20; |
|
44 | + } |
|
45 | 45 | |
46 | 46 | private function checkCache($obj, $reset) { |
47 | 47 | if ($reset) $this->processCache = new \SplObjectStorage(); |
48 | - if (!is_object($obj)) return false; |
|
48 | + if (!is_object($obj)) return false; |
|
49 | 49 | |
50 | 50 | if ($this->processCache->contains($obj)) return $obj; |
51 | 51 | else $this->processCache->attach($obj, true); |
@@ -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 | } |
@@ -96,7 +96,7 @@ discard block |
||
96 | 96 | return; |
97 | 97 | |
98 | 98 | $runAgain = false; |
99 | - $columns = $this->pdo->query('SELECT * FROM '. $this->quote($table) . ' PROCEDURE ANALYSE(1,1)')->fetchAll(\PDO::FETCH_OBJ); |
|
99 | + $columns = $this->pdo->query('SELECT * FROM ' . $this->quote($table) . ' PROCEDURE ANALYSE(1,1)')->fetchAll(\PDO::FETCH_OBJ); |
|
100 | 100 | foreach ($columns as $column) { |
101 | 101 | $parts = explode('.', $column->Field_name); |
102 | 102 | $name = $this->quote(end($parts)); |
@@ -130,7 +130,7 @@ discard block |
||
130 | 130 | } |
131 | 131 | } |
132 | 132 | |
133 | - $this->pdo->query('ALTER TABLE ' . $this->quote($table) . ' MODIFY '. $name . ' ' . $type); |
|
133 | + $this->pdo->query('ALTER TABLE ' . $this->quote($table) . ' MODIFY ' . $name . ' ' . $type); |
|
134 | 134 | } |
135 | 135 | } |
136 | 136 | //Sometimes a second pass is needed, if a column has gone from varchar -> int(11) a better int type may be needed |
@@ -11,16 +11,21 @@ discard block |
||
11 | 11 | foreach ($columns as $column) { |
12 | 12 | $parts = explode('.', $column->Field_name); |
13 | 13 | $name = $this->quote(end($parts)); |
14 | - if ($column->Min_value === null && $column->Max_value === null) $this->pdo->query('ALTER TABLE ' . $this->quote($table) . ' DROP COLUMN ' . $name); |
|
15 | - else { |
|
14 | + if ($column->Min_value === null && $column->Max_value === null) { |
|
15 | + $this->pdo->query('ALTER TABLE ' . $this->quote($table) . ' DROP COLUMN ' . $name); |
|
16 | + } else { |
|
16 | 17 | $type = $column->Optimal_fieldtype; |
17 | 18 | if ($column->Max_length < 11) { |
18 | 19 | //Check for dates |
19 | 20 | $count = $this->pdo->query('SELECT count(*) as `count` FROM ' . $this->quote($table) . ' WHERE STR_TO_DATE(' . $name . ',\'%Y-%m-%d %H:%i:s\') IS NULL OR STR_TO_DATE(' . $name . ',\'%Y-%m-%d %H:%i:s\') != ' . $name . ' LIMIT 1')->fetch(\PDO::FETCH_OBJ)->count; |
20 | - if ($count == 0) $type = 'DATETIME'; |
|
21 | + if ($count == 0) { |
|
22 | + $type = 'DATETIME'; |
|
23 | + } |
|
21 | 24 | |
22 | 25 | $count = $this->pdo->query('SELECT count(*) as `count` FROM ' . $this->quote($table) . ' WHERE STR_TO_DATE(' . $name . ',\'%Y-%m-%d\') IS NULL OR STR_TO_DATE(' . $name . ',\'%Y-%m-%d\') != ' . $name . ' LIMIT 1')->fetch(\PDO::FETCH_OBJ)->count; |
23 | - if ($count == 0) $type = 'DATE'; |
|
26 | + if ($count == 0) { |
|
27 | + $type = 'DATE'; |
|
28 | + } |
|
24 | 29 | } |
25 | 30 | |
26 | 31 | //If it's text, work out if it would be better to be something else |
@@ -30,8 +35,7 @@ discard block |
||
30 | 35 | if ($count == 0) { |
31 | 36 | $type = 'INT(11)'; |
32 | 37 | $runAgain = true; |
33 | - } |
|
34 | - else { |
|
38 | + } else { |
|
35 | 39 | //See if it's decimal |
36 | 40 | $count = $this->pdo->query('SELECT count(*) FROM ' . $table . ' WHERE concat(\'\', ' . $name . ' * 1) != ' . $name . ')')->fetch(\PDO::FETCH_OBJ)->count; |
37 | 41 | if ($count == 0) { |
@@ -45,6 +49,8 @@ discard block |
||
45 | 49 | } |
46 | 50 | } |
47 | 51 | //Sometimes a second pass is needed, if a column has gone from varchar -> int(11) a better int type may be needed |
48 | - if ($runAgain) $this->optimiseColumns($table); |
|
52 | + if ($runAgain) { |
|
53 | + $this->optimiseColumns($table); |
|
54 | + } |
|
49 | 55 | } |
50 | 56 | } |
51 | 57 | \ No newline at end of file |
@@ -9,7 +9,7 @@ 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) $mapper = $mapper->filter($this->criteira); |
|
13 | 13 | $this->mapper = $mapper; |
14 | 14 | $this->parentField = $parentField; |
15 | 15 | $this->localField = $localField; |
@@ -48,17 +48,17 @@ discard block |
||
48 | 48 | //Fetch the results so they're in the cache for the corresponding maphper object |
49 | 49 | $results = $this->mapper->filter([$this->localField => $recordsToLoad]); |
50 | 50 | |
51 | - $this->loadDataIntoSiblings($results); |
|
51 | + $this->loadDataIntoSiblings($results); |
|
52 | 52 | } |
53 | 53 | |
54 | - private function loadDataIntoSiblings($results) { |
|
55 | - $cache = []; |
|
54 | + private function loadDataIntoSiblings($results) { |
|
55 | + $cache = []; |
|
56 | 56 | foreach ($results as $result) { |
57 | 57 | $cache[$result->{$this->localField}] = $result; |
58 | 58 | } |
59 | 59 | |
60 | 60 | foreach ($this->siblings as $sibling) { |
61 | - if (isset($cache[$sibling->parentObject->{$this->parentField}])) $sibling->data = $cache[$sibling->parentObject->{$this->parentField}]; |
|
61 | + if (isset($cache[$sibling->parentObject->{$this->parentField}])) $sibling->data = $cache[$sibling->parentObject->{$this->parentField}]; |
|
62 | 62 | } |
63 | 63 | /* |
64 | 64 | foreach ($this->siblings as $sibling) { |
@@ -66,7 +66,7 @@ discard block |
||
66 | 66 | else $sibling->data = $sibling->mapper->filter([$sibling->localField => $sibling->parentObject->{$this->parentField}])->item(0); |
67 | 67 | } |
68 | 68 | */ |
69 | - } |
|
69 | + } |
|
70 | 70 | |
71 | 71 | public function __call($func, array $args = []) { |
72 | 72 | if ($this->lazyLoad() == null) return ''; |
@@ -75,7 +75,7 @@ discard block |
||
75 | 75 | |
76 | 76 | public function __get($name) { |
77 | 77 | if ($this->lazyLoad()) return $this->lazyLoad()->$name; |
78 | - else return null; |
|
78 | + else return null; |
|
79 | 79 | } |
80 | 80 | |
81 | 81 | public function __isset($name) { |
@@ -83,9 +83,9 @@ discard block |
||
83 | 83 | } |
84 | 84 | |
85 | 85 | public function overwrite($parentObject, &$data) { |
86 | - $this->mapper[] = $data; |
|
86 | + $this->mapper[] = $data; |
|
87 | 87 | |
88 | - if (!isset($parentObject->{$this->parentField}) || $parentObject->{$this->parentField} != $data->{$this->localField}) { |
|
88 | + if (!isset($parentObject->{$this->parentField}) || $parentObject->{$this->parentField} != $data->{$this->localField}) { |
|
89 | 89 | $parentObject->{$this->parentField} = $data->{$this->localField}; |
90 | 90 | //Trigger an update of the parent object |
91 | 91 | return true; |
@@ -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) { |
@@ -7,17 +7,17 @@ |
||
7 | 7 | private $intermediateName; |
8 | 8 | |
9 | 9 | public function __construct(\Maphper\Iterator $iterator, $intermediateName = null) { |
10 | - $this->iterator = $iterator; |
|
11 | - $this->intermediateName = $intermediateName; |
|
10 | + $this->iterator = $iterator; |
|
11 | + $this->intermediateName = $intermediateName; |
|
12 | 12 | } |
13 | 13 | |
14 | 14 | public function current() { |
15 | - if ($this->intermediateName) return $this->iterator->current()->{$this->intermediateName}; |
|
15 | + if ($this->intermediateName) return $this->iterator->current()->{$this->intermediateName}; |
|
16 | 16 | return $this->iterator->current(); |
17 | 17 | } |
18 | 18 | |
19 | 19 | public function key() { |
20 | - return $this->iterator->key(); |
|
20 | + return $this->iterator->key(); |
|
21 | 21 | } |
22 | 22 | |
23 | 23 | public function next() { |
@@ -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 |
@@ -37,7 +37,7 @@ discard block |
||
37 | 37 | //bit hacky, breaking encapsulation, but simplest way to work out the info for the other side of the many:many relationship. |
38 | 38 | private function getOtherFieldNameInfo() { |
39 | 39 | if ($this->otherInfo == null) { |
40 | - $propertyReader = function($name) {return $this->$name; }; |
|
40 | + $propertyReader = function($name) {return $this->$name; }; |
|
41 | 41 | |
42 | 42 | $reader = $propertyReader->bindTo($this->intermediateMapper, $this->intermediateMapper); |
43 | 43 | |
@@ -45,7 +45,7 @@ discard block |
||
45 | 45 | $propertyReader = $propertyReader->bindTo($relation, $relation); |
46 | 46 | if ($propertyReader('parentField') != $this->parentField) { |
47 | 47 | $relation = $relation->getData($this->object); |
48 | - $this->otherInfo = [$propertyReader('localField'), $propertyReader('parentField'), $propertyReader('mapper')]; |
|
48 | + $this->otherInfo = [$propertyReader('localField'), $propertyReader('parentField'), $propertyReader('mapper')]; |
|
49 | 49 | } |
50 | 50 | } |
51 | 51 | } |
@@ -103,7 +103,7 @@ discard block |
||
103 | 103 | |
104 | 104 | private function offsetSetAutotraverse($value, $relatedField, $valueField) { |
105 | 105 | $record = new \stdClass; |
106 | - $record->{$this->parentField} = $value->{$this->localField}; |
|
106 | + $record->{$this->parentField} = $value->{$this->localField}; |
|
107 | 107 | $record->$valueField = $this->object->{$relatedField}; |
108 | 108 | $this->intermediateMapper[] = $record; |
109 | 109 | } |
@@ -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}; |
@@ -90,26 +90,26 @@ |
||
90 | 90 | list($relatedField, $valueField, $mapper) = $this->getOtherFieldNameInfo(); |
91 | 91 | if ($this->autoTraverse) $this->offsetSetAutotraverse($value, $relatedField, $valueField); |
92 | 92 | else if ($this->doUpdateInterMapper($value, $relatedField, $valueField)) { |
93 | - $record = $value; |
|
93 | + $record = $value; |
|
94 | 94 | $record->{$this->parentField} = $value->{$this->intermediateName}->{$this->localField}; |
95 | 95 | $record->$valueField = $this->object->{$relatedField}; |
96 | 96 | $this->intermediateMapper[] = $record; |
97 | 97 | } |
98 | 98 | } |
99 | 99 | |
100 | - private function doUpdateInterMapper($record, $relatedField, $valueField) { |
|
101 | - return !(isset($record->{$this->parentField}) && isset($record->{$this->intermediateName}) && |
|
102 | - isset($record->$valueField) && isset($this->object->{$relatedField}) && |
|
103 | - $record->{$this->parentField} == $record->{$this->intermediateName}->{$this->localField} && |
|
104 | - $record->$valueField == $this->object->{$relatedField}); |
|
105 | - } |
|
106 | - |
|
107 | - private function offsetSetAutotraverse($value, $relatedField, $valueField) { |
|
108 | - $record = new \stdClass; |
|
109 | - $record->{$this->parentField} = $value->{$this->localField}; |
|
110 | - $record->$valueField = $this->object->{$relatedField}; |
|
111 | - $this->intermediateMapper[] = $record; |
|
112 | - } |
|
100 | + private function doUpdateInterMapper($record, $relatedField, $valueField) { |
|
101 | + return !(isset($record->{$this->parentField}) && isset($record->{$this->intermediateName}) && |
|
102 | + isset($record->$valueField) && isset($this->object->{$relatedField}) && |
|
103 | + $record->{$this->parentField} == $record->{$this->intermediateName}->{$this->localField} && |
|
104 | + $record->$valueField == $this->object->{$relatedField}); |
|
105 | + } |
|
106 | + |
|
107 | + private function offsetSetAutotraverse($value, $relatedField, $valueField) { |
|
108 | + $record = new \stdClass; |
|
109 | + $record->{$this->parentField} = $value->{$this->localField}; |
|
110 | + $record->$valueField = $this->object->{$relatedField}; |
|
111 | + $this->intermediateMapper[] = $record; |
|
112 | + } |
|
113 | 113 | |
114 | 114 | public function offsetUnset($id) { |
115 | 115 | //$this->relation->mapper->filter([$relatedField => $this->object->$valueField, $this->relation->parentField => $id])->delete(); |