|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\orm\parser; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\utils\base\UArray; |
|
6
|
|
|
use Ubiquity\orm\OrmUtils; |
|
7
|
|
|
use Ubiquity\db\SqlUtils; |
|
8
|
|
|
|
|
9
|
|
|
class ConditionParser { |
|
10
|
|
|
private $firstPart; |
|
11
|
|
|
private $condition; |
|
12
|
|
|
private $parts=[]; |
|
13
|
|
|
private $params; |
|
14
|
|
|
private $invertedParams=true; |
|
15
|
|
|
|
|
16
|
|
|
public function __construct($condition=null,$firstPart=null,$params=null){ |
|
17
|
|
|
$this->condition=$condition; |
|
18
|
|
|
$this->firstPart=$firstPart; |
|
19
|
|
|
if(is_array($params)){ |
|
20
|
|
|
$this->setParams($params); |
|
21
|
|
|
} |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function addKeyValues($keyValues,$classname,$separator=" AND ") { |
|
25
|
|
|
if(!is_array($keyValues)){ |
|
26
|
|
|
$this->condition=$this->parseKey($keyValues, $classname); |
|
27
|
|
|
}else{ |
|
28
|
|
View Code Duplication |
if(!UArray::isAssociative($keyValues)){ |
|
|
|
|
|
|
29
|
|
|
if(isset($classname)){ |
|
30
|
|
|
$keys=OrmUtils::getKeyFields($classname); |
|
31
|
|
|
$keyValues=\array_combine($keys, $keyValues); |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
$retArray=array (); |
|
35
|
|
View Code Duplication |
foreach ( $keyValues as $key => $value ) { |
|
|
|
|
|
|
36
|
|
|
if($this->addParams($value)){ |
|
37
|
|
|
$retArray[]=SqlUtils::$quote . $key . SqlUtils::$quote . " = ?"; |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
$this->condition=implode($separator, $retArray); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
private function addParams($value){ |
|
45
|
|
|
if(!isset($this->params[$value])){ |
|
46
|
|
|
return $this->params[$value]=true; |
|
47
|
|
|
} |
|
48
|
|
|
return false; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function addPart($condition,$value){ |
|
52
|
|
|
if($this->addParams($value)){ |
|
53
|
|
|
$this->parts[]=$condition; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function addParts($condition,$values){ |
|
58
|
|
|
foreach ($values as $value){ |
|
59
|
|
|
if($this->addParams($value)){ |
|
60
|
|
|
$this->parts[]=$condition; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function compileParts($separator=" OR "){ |
|
66
|
|
|
if($separator==" OR " && sizeof($this->parts)>3){ |
|
67
|
|
|
$parts=$this->refactorParts(); |
|
68
|
|
|
$conditions=[]; |
|
69
|
|
|
foreach ($parts as $part=>$values){ |
|
70
|
|
|
$conditions[]=$part." IN (".implode(",", $values).")"; |
|
71
|
|
|
} |
|
72
|
|
|
$this->condition=implode(" OR ", $conditions); |
|
73
|
|
|
}else{ |
|
74
|
|
|
$this->condition=implode($separator, $this->parts); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
private function refactorParts(){ |
|
79
|
|
|
$tmp=""; |
|
|
|
|
|
|
80
|
|
|
$result=[]; |
|
81
|
|
|
foreach ($this->parts as $part){ |
|
82
|
|
|
$part=str_replace("= ?", "", $part); |
|
83
|
|
|
$result[$part][]='?'; |
|
84
|
|
|
} |
|
85
|
|
|
return $result; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
private function parseKey($keyValues,$className){ |
|
89
|
|
|
$condition=$keyValues; |
|
90
|
|
|
if (strrpos($keyValues, "=") === false && strrpos($keyValues, ">") === false && strrpos($keyValues, "<") === false) { |
|
91
|
|
|
if($this->addParams($keyValues)){ |
|
92
|
|
|
$condition=SqlUtils::$quote. OrmUtils::getFirstKey($className) . SqlUtils::$quote."= ?"; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
return $condition; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getCondition() { |
|
102
|
|
|
if(!isset($this->firstPart)|| $this->firstPart==='') |
|
103
|
|
|
return $this->condition; |
|
104
|
|
|
$ret=$this->firstPart; |
|
105
|
|
|
if(isset($this->condition)){ |
|
106
|
|
|
$ret.=" WHERE ".$this->condition; |
|
107
|
|
|
} |
|
108
|
|
|
return $ret; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return mixed |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getParams() { |
|
115
|
|
|
if(is_array($this->params)){ |
|
116
|
|
|
if($this->invertedParams){ |
|
117
|
|
|
return array_keys($this->params); |
|
118
|
|
|
} |
|
119
|
|
|
return $this->params; |
|
120
|
|
|
} |
|
121
|
|
|
return; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param string $condition |
|
126
|
|
|
*/ |
|
127
|
|
|
public function setCondition($condition) { |
|
128
|
|
|
$this->condition = $condition; |
|
129
|
|
|
return $this; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param mixed $params |
|
134
|
|
|
*/ |
|
135
|
|
|
public function setParams($params) { |
|
136
|
|
|
$this->params = $params; |
|
137
|
|
|
$this->invertedParams=false; |
|
138
|
|
|
return $this; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function limitOne(){ |
|
142
|
|
|
$limit=""; |
|
143
|
|
|
if(\stripos($this->condition, " limit ")===false){ |
|
144
|
|
|
$limit=" limit 1"; |
|
145
|
|
|
} |
|
146
|
|
|
$this->condition.=$limit; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
public static function simple($condition,$params){ |
|
150
|
|
|
$cParser=new ConditionParser($condition); |
|
151
|
|
|
$cParser->addParams($params); |
|
152
|
|
|
return $cParser; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.