Passed
Push — master ( c0dc60...96b474 )
by Jean-Christophe
08:33
created

ConditionParser::setKeyValues()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 10
cc 3
nc 3
nop 1
crap 3
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
/**
10
 * Represents a query condition.
11
 *
12
 * Ubiquity\orm\parser$ConditionParser
13
 * This class is part of Ubiquity
14
 *
15
 * @author jcheron <[email protected]>
16
 * @version 1.0.1
17
 *
18
 */
19
class ConditionParser {
20
	private $firstPart;
21
	private $condition;
22
	private $parts = [ ];
23
	private $params;
24
	private $invertedParams = true;
25
26 58
	public function __construct($condition = null, $firstPart = null, $params = null) {
27 58
		$this->condition = $condition;
28 58
		$this->firstPart = $firstPart;
29 58
		if (is_array ( $params )) {
30 6
			$this->setParams ( $params );
31
		}
32 58
	}
33
34 31
	public function addKeyValues($keyValues, $classname, $separator = " AND ") {
35 31
		if (! is_array ( $keyValues )) {
36 25
			$this->condition = $this->parseKey ( $keyValues, $classname );
37
		} else {
38 10
			if (! UArray::isAssociative ( $keyValues )) {
39 6
				if (isset ( $classname )) {
40 6
					$keys = OrmUtils::getKeyFields ( $classname );
41 6
					if (is_array ( $keys )) {
42 6
						$keyValues = \array_combine ( $keys, $keyValues );
43
					}
44
				}
45
			}
46 10
			$retArray = array ();
47 10
			foreach ( $keyValues as $key => $value ) {
48 10
				if ($this->addParams ( $value )) {
49 10
					$retArray [] = SqlUtils::$quote . $key . SqlUtils::$quote . " = ?";
50
				}
51
			}
52 10
			$this->condition = implode ( $separator, $retArray );
53
		}
54 31
	}
55
56 5
	public function setKeyValues($values) {
57 5
		if (! is_array ( $values )) {
58 4
			$this->params = [ $values => true ];
59
		} else {
60 1
			$this->params = [ ];
61 1
			foreach ( $values as $val ) {
62 1
				$this->addParams ( $val );
63
			}
64
		}
65 5
	}
66
67 49
	private function addParams($value) {
68 49
		if (! isset ( $this->params [$value] )) {
69 49
			return $this->params [$value] = true;
70
		}
71 13
		return false;
72
	}
73
74 38
	public function addPart($condition, $value) {
75 38
		if ($this->addParams ( $value )) {
76 38
			$this->parts [] = $condition;
77 38
			return true;
78
		}
79
		return false;
80
	}
81
82 23
	public function addParts($condition, $values) {
83 23
		foreach ( $values as $value ) {
84 23
			if ($this->addParams ( $value )) {
85 23
				$this->parts [] = $condition;
86
			}
87
		}
88 23
	}
89
90 41
	public function compileParts($separator = " OR ") {
91 41
		if ($separator == " OR " && sizeof ( $this->parts ) > 3) {
92 23
			$parts = $this->refactorParts ();
93 23
			$conditions = [ ];
94 23
			foreach ( $parts as $part => $values ) {
95 23
				$values [0] = "SELECT ? as _id";
96 23
				$conditions [] = " INNER JOIN (" . implode ( " UNION ALL SELECT ", $values ) . ") as _tmp ON " . $part . "=_tmp._id";
97
			}
98 23
			$this->condition = implode ( " ", $conditions );
99
		} else {
100 36
			$this->condition = implode ( $separator, $this->parts );
101
		}
102 41
	}
103
104 23
	private function refactorParts() {
105 23
		$result = [ ];
106 23
		foreach ( $this->parts as $part ) {
107 23
			$part = str_replace ( "= ?", "", $part );
108 23
			$result [$part] [] = '?';
109
		}
110 23
		return $result;
111
	}
112
113 25
	private function parseKey($keyValues, $className) {
114 25
		$condition = $keyValues;
115 25
		if (strrpos ( $keyValues, "=" ) === false && strrpos ( $keyValues, ">" ) === false && strrpos ( $keyValues, "<" ) === false) {
116 12
			if ($this->addParams ( $keyValues )) {
117 12
				$condition = SqlUtils::$quote . OrmUtils::getFirstKey ( $className ) . SqlUtils::$quote . "= ?";
118
			}
119
		}
120 25
		return $condition;
121
	}
122
123
	/**
124
	 *
125
	 * @return string
126
	 */
127 58
	public function getCondition() {
128 58
		if ($this->firstPart == null)
129 58
			return $this->condition;
130 3
		$ret = $this->firstPart;
131 3
		if (isset ( $this->condition )) {
132 3
			$ret .= " WHERE " . $this->condition;
133
		}
134 3
		return $ret;
135
	}
136
137
	/**
138
	 *
139
	 * @return mixed
140
	 */
141 58
	public function getParams() {
142 58
		if (is_array ( $this->params )) {
143 53
			if ($this->invertedParams) {
144 49
				return array_keys ( $this->params );
145
			}
146 7
			return $this->params;
147
		}
148 36
		return;
149
	}
150
151
	/**
152
	 *
153
	 * @return mixed
154
	 */
155 38
	public function hasParam($value) {
156 38
		if (is_array ( $this->params )) {
157 19
			if ($this->invertedParams) {
158 19
				return isset ( $this->params [$value] );
159
			}
160
			return array_search ( $value, $this->params ) !== false;
161
		}
162 38
		return false;
163
	}
164
165 38
	public function countParts() {
166 38
		if (is_array ( $this->params ))
167 19
			return sizeof ( $this->params );
168 38
		return 0;
169
	}
170
171
	/**
172
	 *
173
	 * @param string $condition
174
	 */
175
	public function setCondition($condition) {
176
		$this->condition = $condition;
177
		return $this;
178
	}
179
180
	/**
181
	 *
182
	 * @param mixed $params
183
	 */
184 7
	public function setParams($params) {
185 7
		$this->params = $params;
186 7
		$this->invertedParams = false;
187 7
		return $this;
188
	}
189
190 35
	public function limitOne() {
191 35
		$limit = "";
192 35
		if (\stripos ( $this->condition, " limit " ) === false) {
193 32
			$limit = " limit 1";
194
		}
195 35
		$this->condition .= $limit;
196 35
	}
197
198 11
	public static function simple($condition, $params) {
199 11
		$cParser = new ConditionParser ( $condition );
200 11
		$cParser->addParams ( $params );
201 11
		return $cParser;
202
	}
203
}
204
205