Completed
Push — master ( 32110c...bc25d3 )
by Jean-Christophe
02:12
created

ConditionParser::limitOne()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
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 $condition;
11
	private $parts=[];
12
	private $params;
13
	
14
	public function __construct($condition=null){
15
		$this->condition=$condition;
16
	}
17
	
18
	public function addKeyValues($keyValues,$classname,$separator=" AND ") {
19
		if(!is_array($keyValues)){
20
			$this->condition=$this->parseKey($keyValues, $classname);
21
		}else{
22 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...
23
				if(isset($classname)){
24
					$keys=OrmUtils::getKeyFields($classname);
25
					$keyValues=\array_combine($keys, $keyValues);
26
				}
27
			}
28
			$retArray=array ();
29 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...
30
				$retArray[]=SqlUtils::$quote . $key . SqlUtils::$quote . " = ?";
31
				$this->params[]=$value;
32
			}
33
			$this->condition=implode($separator, $retArray);
34
		}
35
	}
36
	
37
	public function addPart($condition,$value){
38
		$this->parts[]=$condition;
39
		$this->params[]=$value;
40
	}
41
	
42
	public function compileParts($separator=" OR "){
43
		$this->condition=implode($separator, $this->parts);
44
	}
45
	
46
	private function parseKey($keyValues,$className){
47
		$condition=$keyValues;
48
		if (strrpos($keyValues, "=") === false && strrpos($keyValues, ">") === false && strrpos($keyValues, "<") === false) {
49
			$condition=SqlUtils::$quote. OrmUtils::getFirstKey($className) . SqlUtils::$quote."= ?";
50
			$this->params[]=$keyValues;
51
		}
52
		return $condition;
53
	}
54
55
	/**
56
	 * @return string
57
	 */
58
	public function getCondition() {
59
		return $this->condition;
60
	}
61
62
	/**
63
	 * @return mixed
64
	 */
65
	public function getParams() {
66
		return $this->params;
67
	}
68
69
	/**
70
	 * @param string $condition
71
	 */
72
	public function setCondition($condition) {
73
		$this->condition = $condition;
74
	}
75
76
	/**
77
	 * @param mixed $params
78
	 */
79
	public function setParams($params) {
80
		$this->params = $params;
81
	}
82
	
83
	public function limitOne(){
84
		$limit="";
85
		if(\stripos($this->condition, " limit ")===false)
86
			$limit=" limit 1";
87
		$this->condition.=$limit;
88
	}
89
90
}
91
92