Completed
Push — master ( 5afe3b...44ee0c )
by Jean-Christophe
05:18
created

ConditionParser::simple()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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)){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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 ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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
			return true;
55
		}
56
		return false;
57
	}
58
	
59
	public function addParts($condition,$values){
60
		foreach ($values as $value){
61
			if($this->addParams($value)){
62
				$this->parts[]=$condition;
63
			}
64
		}
65
	}
66
	
67
	public function compileParts($separator=" OR "){
68
		if($separator==" OR " && sizeof($this->parts)>3){
69
			$parts=$this->refactorParts();
70
			$conditions=[];
71
			foreach ($parts as $part=>$values){
72
				$values[0]="SELECT ? as _id";
73
				$conditions[]=" INNER JOIN (".implode(" UNION ALL SELECT ", $values).") as _tmp ON ".$part."=_tmp._id";
74
			}
75
			$this->condition=implode(" ", $conditions);
76
		}else{
77
			$this->condition=implode($separator, $this->parts);
78
		}
79
	}
80
	
81
	private function refactorParts(){
82
		$tmp="";
0 ignored issues
show
Unused Code introduced by
$tmp is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
83
		$result=[];
84
		foreach ($this->parts as $part){
85
			$part=str_replace("= ?", "", $part);
86
			$result[$part][]='?';
87
		}
88
		return $result;
89
	}
90
	
91
	private function parseKey($keyValues,$className){
92
		$condition=$keyValues;
93
		if (strrpos($keyValues, "=") === false && strrpos($keyValues, ">") === false && strrpos($keyValues, "<") === false) {
94
			if($this->addParams($keyValues)){
95
				$condition=SqlUtils::$quote. OrmUtils::getFirstKey($className) . SqlUtils::$quote."= ?";
96
			}
97
		}
98
		return $condition;
99
	}
100
	
101
	/**
102
	 * @return string
103
	 */
104
	public function getCondition() {
105
		if(!isset($this->firstPart)|| $this->firstPart==='')
106
			return $this->condition;
107
		$ret=$this->firstPart;
108
		if(isset($this->condition)){
109
			$ret.=" WHERE ".$this->condition;
110
		}
111
		return $ret;
112
	}
113
	
114
	/**
115
	 * @return mixed
116
	 */
117
	public function getParams() {
118
		if(is_array($this->params)){
119
			if($this->invertedParams){
120
				return array_keys($this->params);
121
			}
122
			return $this->params;
123
		}
124
		return;
125
	}
126
	
127
	/**
128
	 * @return mixed
129
	 */
130
	public function hasParam($value) {
131
		if(is_array($this->params)){
132
			if($this->invertedParams){
133
				return isset($this->params[$value]);
134
			}
135
			return array_search($value,$this->params)!==false;
136
		}
137
		return false;
138
	}
139
	
140
	public function countParts(){
141
		if(is_array($this->params))
142
			return sizeof($this->params);
143
		return 0;
144
	}
145
	
146
	/**
147
	 * @param string $condition
148
	 */
149
	public function setCondition($condition) {
150
		$this->condition = $condition;
151
		return $this;
152
	}
153
	
154
	/**
155
	 * @param mixed $params
156
	 */
157
	public function setParams($params) {
158
		$this->params = $params;
159
		$this->invertedParams=false;
160
		return $this;
161
	}
162
	
163
	public function limitOne(){
164
		$limit="";
165
		if(\stripos($this->condition, " limit ")===false){
166
			$limit=" limit 1";
167
		}
168
		$this->condition.=$limit;
169
	}
170
	
171
	public static function simple($condition,$params){
172
		$cParser=new ConditionParser($condition);
173
		$cParser->addParams($params);
174
		return $cParser;
175
	}
176
	
177
}
178
179