|
1
|
|
|
<?php |
|
2
|
|
|
namespace Maphper\Lib\Sql; |
|
3
|
|
|
|
|
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
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function addConditional(WhereConditional $conditional) { |
|
20
|
|
|
$this->conditionals[] = $conditional; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function createSql($fields, $mode = \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND) { |
|
24
|
|
|
$args = []; |
|
25
|
|
|
$sql = []; |
|
26
|
|
|
|
|
27
|
|
|
foreach ($fields as $key => $value) { |
|
28
|
|
|
$value = $this->convertDates($value); |
|
29
|
|
|
|
|
30
|
|
|
if (is_object($value)) continue; |
|
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
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
return ['args' => $args, 'sql' => $this->sqlArrayToString($sql, $mode)]; |
|
38
|
|
|
} |
|
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; |
|
44
|
|
|
|
|
45
|
|
|
foreach ($duplicates as $argKey => $argVal) { |
|
46
|
|
|
$valHash = substr(md5($argVal), 0, 5); |
|
47
|
|
|
$newKey = $argKey . $valHash; |
|
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
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $result; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/* |
|
59
|
|
|
* Either get sql from a conditional or call createSql again because the mode needs to be changed |
|
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
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
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 . ')'; |
|
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
|
|
|
} |
|
89
|
|
|
|