-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWhereClause.php
76 lines (67 loc) · 1.94 KB
/
WhereClause.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
/*
* Class to manage the where clauses.
* Examples: WHERE(status:eq:a)
* TODO: Where clauses do not support multiple arguments for AND/OR
*/
class WhereClause
{
private $clause;
private $arguments; // Array with column (0), operator (1), value (2)
public function __construct($query)
{
$query = strtolower($query);
$this->clause = WhereClause::getWhereClauseFromQuery($query);
$this->arguments = $this->genArguments($this->clause);
}
public function getClause()
{
return $this->clause;
}
public function getWhereArguments()
{
return $this->arguments;
}
public static function getWhereClauseFromQuery($query)
{
$where_clause = null;
// Use Regex to find the WHERE clause
preg_match("/(where\(([^)]+)\))/", $query, $results);
if (preg_match("/(where\(([^)]+)\))/", $query))
{
// If found return the where clause
$where_clause = $results[1];
}
return $where_clause;
}
private function genArguments($clause)
{
preg_match("/where\(([^)]+)\\)/", $clause, $results);
$arguments = explode(':', $results[1]);
$arguments_array = array(
'column' => $arguments[0],
'operator' => $arguments[1],
'value' => $arguments[2]
);
if (!WhereClause::isValidOperator($arguments_array['operator']))
{
// Check if operator is valid, if not set it to NDF
$arguments_array['operator'] = 'ndf';
}
return $arguments_array;
}
public static function isValidOperator($operator)
{
$operator = strtolower($operator);
$valid_operators = array('eq', 'gt', 'lt', 'gte', 'lte');
foreach ($valid_operators as $valid)
{
if ($operator == $valid)
{
return true;
}
}
return false;
}
}
?>