Completed
Push — master ( 9bd3b4...2d1963 )
by Jean-Christophe
02:37
created

ConditionParser::simple()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
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
	
15
	public function __construct($condition=null,$firstPart=null){
16
		$this->condition=$condition;
17
		$this->firstPart=$firstPart;
18
	}
19
	
20
	public function addKeyValues($keyValues,$classname,$separator=" AND ") {
21
		if(!is_array($keyValues)){
22
			$this->condition=$this->parseKey($keyValues, $classname);
23
		}else{
24 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...
25
				if(isset($classname)){
26
					$keys=OrmUtils::getKeyFields($classname);
27
					$keyValues=\array_combine($keys, $keyValues);
28
				}
29
			}
30
			$retArray=array ();
31 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...
32
				if($this->addParams($value)){
33
					$retArray[]=SqlUtils::$quote . $key . SqlUtils::$quote . " = ?";
34
				}
35
			}
36
			$this->condition=implode($separator, $retArray);
37
		}
38
	}
39
	
40
	private function addParams($value){
41
		if(!isset($this->params[$value])){
42
			return $this->params[$value]=true;
43
		}
44
		return false;
45
	}
46
	
47
	public function addPart($condition,$value){
48
		if($this->addParams($value)){
49
			$this->parts[]=$condition;
50
		}
51
	}
52
	
53
	public function addParts($condition,$values){
54
		foreach ($values as $value){
55
			if($this->addParams($value)){
56
				$this->parts[]=$condition;
57
			}
58
		}
59
	}
60
	
61
	public function compileParts($separator=" OR "){
62
		$this->condition=implode($separator, $this->parts);
63
	}
64
	
65
	private function parseKey($keyValues,$className){
66
		$condition=$keyValues;
67
		if (strrpos($keyValues, "=") === false && strrpos($keyValues, ">") === false && strrpos($keyValues, "<") === false) {
68
			if($this->addParams($keyValues)){
69
				$condition=SqlUtils::$quote. OrmUtils::getFirstKey($className) . SqlUtils::$quote."= ?";
70
			}
71
		}
72
		return $condition;
73
	}
74
	
75
	/**
76
	 * @return string
77
	 */
78
	public function getCondition() {
79
		if(!isset($this->firstPart))
80
			return $this->condition;
81
			$ret=$this->firstPart;
82
			if(isset($this->condition)){
83
				$ret.=" WHERE ".$this->condition;
84
			}
85
			return $ret;
86
	}
87
	
88
	/**
89
	 * @return mixed
90
	 */
91
	public function getParams() {
92
		if(is_array($this->params))
93
			return array_keys($this->params);
94
			return;
95
	}
96
	
97
	/**
98
	 * @param string $condition
99
	 */
100
	public function setCondition($condition) {
101
		$this->condition = $condition;
102
		return $this;
103
	}
104
	
105
	/**
106
	 * @param mixed $params
107
	 */
108
	public function setParams($params) {
109
		$this->params = $params;
110
		return $this;
111
	}
112
	
113
	public function limitOne(){
114
		$limit="";
115
		if(\stripos($this->condition, " limit ")===false)
116
			$limit=" limit 1";
117
			$this->condition.=$limit;
118
	}
119
	
120
	public static function simple($condition,$params){
121
		$cParser=new ConditionParser($condition);
122
		$cParser->addParams($params);
123
		return $cParser;
124
	}
125
	
126
}
127
128