Completed
Push — master ( 681f8a...cbe892 )
by Jean-Christophe
02:08
created

ConditionParser::parseKey()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 9
rs 8.8571
c 1
b 0
f 0
cc 5
eloc 6
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 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 compileParts($separator=" OR "){
54
		$this->condition=implode($separator, $this->parts);
55
	}
56
	
57
	private function parseKey($keyValues,$className){
58
		$condition=$keyValues;
59
		if (strrpos($keyValues, "=") === false && strrpos($keyValues, ">") === false && strrpos($keyValues, "<") === false) {
60
			if($this->addParams($keyValues)){
61
				$condition=SqlUtils::$quote. OrmUtils::getFirstKey($className) . SqlUtils::$quote."= ?";
62
			}
63
		}
64
		return $condition;
65
	}
66
	
67
	/**
68
	 * @return string
69
	 */
70
	public function getCondition() {
71
		if(!isset($this->firstPart))
72
			return $this->condition;
73
			$ret=$this->firstPart;
74
			if(isset($this->condition)){
75
				$ret.=" WHERE ".$this->condition;
76
			}
77
			return $ret;
78
	}
79
	
80
	/**
81
	 * @return mixed
82
	 */
83
	public function getParams() {
84
		if(is_array($this->params))
85
			return array_keys($this->params);
86
			return;
87
	}
88
	
89
	/**
90
	 * @param string $condition
91
	 */
92
	public function setCondition($condition) {
93
		$this->condition = $condition;
94
	}
95
	
96
	/**
97
	 * @param mixed $params
98
	 */
99
	public function setParams($params) {
100
		$this->params = $params;
101
	}
102
	
103
	public function limitOne(){
104
		$limit="";
105
		if(\stripos($this->condition, " limit ")===false)
106
			$limit=" limit 1";
107
			$this->condition.=$limit;
108
	}
109
	
110
}
111
112