Completed
Push — master ( cbe892...9bd3b4 )
by Jean-Christophe
02:05
created

ConditionParser::addParts()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 3
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
			if(!UArray::isAssociative($keyValues)){
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
	}
103
	
104
	/**
105
	 * @param mixed $params
106
	 */
107
	public function setParams($params) {
108
		$this->params = $params;
109
	}
110
	
111
	public function limitOne(){
112
		$limit="";
113
		if(\stripos($this->condition, " limit ")===false)
114
			$limit=" limit 1";
115
			$this->condition.=$limit;
116
	}
117
	
118
}
119
120