1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Nip\Database\Query\Condition; |
5
|
|
|
|
6
|
|
|
use Nip\Database\Query\AbstractQuery as Query; |
7
|
|
|
|
8
|
|
|
class Condition |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
protected $_string; |
12
|
|
|
protected $_values; |
13
|
|
|
protected $_query; |
14
|
|
|
|
15
|
13 |
|
public function __construct($string, $values = array()) |
16
|
|
|
{ |
17
|
13 |
|
$this->_string = $string; |
18
|
13 |
|
$this->_values = $values; |
19
|
13 |
|
} |
20
|
|
|
|
21
|
6 |
|
public function __toString() |
22
|
|
|
{ |
23
|
6 |
|
return $this->getString(); |
24
|
|
|
} |
25
|
|
|
|
26
|
12 |
|
public function getString() |
27
|
|
|
{ |
28
|
12 |
|
return $this->parseString($this->_string, $this->_values); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Parses $string and replaces all instances of "?" with corresponding $values |
33
|
|
|
* |
34
|
|
|
* @param string $string |
35
|
|
|
* @param array $values |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
12 |
|
public function parseString($string, $values) |
39
|
|
|
{ |
40
|
12 |
|
$positions = []; |
41
|
12 |
|
$pos = 0; |
|
|
|
|
42
|
12 |
|
$offset = 0; |
43
|
|
|
|
44
|
12 |
|
while (($pos = strpos($string, "?", $offset)) !== false) { |
45
|
4 |
|
$positions[] = $pos; |
46
|
4 |
|
$offset = $pos + 1; |
47
|
|
|
} |
48
|
|
|
|
49
|
12 |
|
$count = count($positions); |
50
|
|
|
|
51
|
12 |
|
if ($count == 1) { |
52
|
4 |
|
$values = array($values); |
53
|
|
|
} |
54
|
|
|
|
55
|
12 |
|
for ($i = 0; $i < $count; $i++) { |
56
|
4 |
|
$value = $values[$i]; |
57
|
4 |
|
if ($value instanceof Query) { |
58
|
1 |
|
$value = $this->parseValueQuery($value); |
59
|
3 |
|
} elseif (is_array($value)) { |
60
|
1 |
|
foreach ($value as $key => $subvalue) { |
61
|
1 |
|
if (trim($subvalue) != '') { |
62
|
1 |
|
$value[$key] = is_numeric($subvalue) ? $subvalue : $this->getQuery()->getManager()->getAdapter()->quote($subvalue); |
63
|
|
|
} else { |
64
|
1 |
|
unset($value[$key]); |
65
|
|
|
} |
66
|
|
|
} |
67
|
1 |
|
$value = "(" . implode(", ", $value) . ")"; |
68
|
2 |
|
} elseif (is_numeric($value)) { |
|
|
|
|
69
|
|
|
} else { |
70
|
2 |
|
$value = $this->getQuery()->getManager()->getAdapter()->quote($values[$i]); |
71
|
|
|
} |
72
|
4 |
|
$string = substr_replace($string, $value, strpos($string, '?'), 1); |
73
|
|
|
} |
74
|
|
|
|
75
|
12 |
|
return $string; |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
protected function parseValueQuery($value) |
79
|
|
|
{ |
80
|
1 |
|
return "(".$value->assemble().")"; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return Query |
85
|
|
|
*/ |
86
|
2 |
|
public function getQuery() |
87
|
|
|
{ |
88
|
2 |
|
return $this->_query; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param Query $query |
93
|
|
|
* @return $this |
94
|
|
|
*/ |
95
|
13 |
|
public function setQuery($query) |
96
|
|
|
{ |
97
|
13 |
|
$this->_query = $query; |
98
|
|
|
|
99
|
13 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
5 |
|
public function and_($condition) |
103
|
|
|
{ |
104
|
5 |
|
return new AndCondition($this, $condition); |
105
|
|
|
} |
106
|
|
|
|
107
|
4 |
|
public function or_($condition) |
108
|
|
|
{ |
109
|
4 |
|
return new OrCondition($this, $condition); |
110
|
|
|
} |
111
|
|
|
|
112
|
7 |
|
public function protectCondition($condition) |
113
|
|
|
{ |
114
|
7 |
|
return strpos($condition, ' AND ') || strpos($condition, ' OR ') ? '('.$condition.')' : $condition; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.