1 | <?php |
||
2 | |||
3 | namespace Nip\Database\Query\Condition; |
||
4 | |||
5 | use Nip\Database\Query\AbstractQuery as Query; |
||
6 | |||
7 | /** |
||
8 | * Class Condition |
||
9 | * @package Nip\Database\Query\Condition |
||
10 | */ |
||
11 | class Condition |
||
12 | { |
||
13 | protected $_string; |
||
14 | protected $_values; |
||
15 | protected $_query; |
||
16 | |||
17 | 13 | /** |
|
18 | * @param string $string |
||
19 | 13 | */ |
|
20 | 13 | public function __construct($string, $values = []) |
|
21 | 13 | { |
|
22 | $this->_string = $string; |
||
23 | 6 | $this->_values = $values; |
|
24 | } |
||
25 | 6 | ||
26 | /** |
||
27 | * @return string |
||
28 | 12 | */ |
|
29 | public function __toString() |
||
30 | 12 | { |
|
31 | return $this->getString(); |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * @return string |
||
36 | */ |
||
37 | public function getString() |
||
38 | { |
||
39 | return $this->parseString($this->_string, $this->_values); |
||
40 | 12 | } |
|
41 | |||
42 | 12 | /** |
|
43 | 12 | * Parses $string and replaces all instances of "?" with corresponding $values. |
|
44 | 12 | * |
|
45 | * @param string $string |
||
46 | 12 | * @param array $values |
|
47 | 3 | * |
|
48 | 3 | * @return string |
|
49 | */ |
||
50 | public function parseString($string, $values) |
||
51 | 12 | { |
|
52 | $positions = []; |
||
53 | 12 | $pos = 0; |
|
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
54 | 3 | $offset = 0; |
|
55 | |||
56 | while (($pos = strpos($string, "?", $offset)) !== false) { |
||
57 | 12 | $positions[] = $pos; |
|
58 | 3 | $offset = $pos + 1; |
|
59 | 3 | } |
|
60 | 1 | ||
61 | 2 | $count = count($positions); |
|
62 | 1 | ||
63 | 1 | if ($count == 1) { |
|
64 | 1 | $values = [$values]; |
|
65 | } |
||
66 | 1 | ||
67 | for ($i = 0; $i < $count; $i++) { |
||
68 | $value = $values[$i]; |
||
69 | 1 | if ($value instanceof Query) { |
|
70 | 1 | $value = $this->parseValueQuery($value); |
|
71 | } elseif (is_array($value)) { |
||
72 | 1 | foreach ($value as $key => $subvalue) { |
|
73 | if (trim($subvalue) != '') { |
||
74 | 3 | $value[$key] = is_numeric($subvalue) ? $subvalue : $this->getQuery()->getManager()->getAdapter()->quote($subvalue); |
|
75 | } else { |
||
76 | unset($value[$key]); |
||
77 | 12 | } |
|
78 | } |
||
79 | $value = '(' . implode(', ', $value) . ')'; |
||
80 | } elseif (is_int($value) || is_float($value)) { |
||
81 | } else { |
||
82 | $value = $this->getQuery()->getManager()->getAdapter()->quote($values[$i]); |
||
83 | 1 | } |
|
84 | $string = substr_replace($string, $value, strpos($string, '?'), 1); |
||
85 | 1 | } |
|
86 | |||
87 | return $string; |
||
0 ignored issues
–
show
|
|||
88 | } |
||
89 | |||
90 | /** |
||
91 | 1 | * @param Query $value |
|
92 | */ |
||
93 | 1 | protected function parseValueQuery($value) |
|
94 | { |
||
95 | return "(" . $value->assemble() . ")"; |
||
0 ignored issues
–
show
Are you sure the usage of
$value->assemble() targeting Nip\Database\Query\AbstractQuery::assemble() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @return Query |
||
100 | 13 | */ |
|
101 | public function getQuery() |
||
102 | 13 | { |
|
103 | return $this->_query; |
||
104 | 13 | } |
|
105 | |||
106 | /** |
||
107 | * @param Query $query |
||
108 | * @return $this |
||
109 | */ |
||
110 | 5 | public function setQuery($query) |
|
111 | { |
||
112 | 5 | $this->_query = $query; |
|
113 | |||
114 | return $this; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | 4 | * @param Condition $condition |
|
119 | */ |
||
120 | 4 | public function and_($condition) |
|
121 | { |
||
122 | return new AndCondition($this, $condition); |
||
123 | 7 | } |
|
124 | |||
125 | 7 | /** |
|
126 | * @param Condition $condition |
||
127 | */ |
||
128 | public function or_($condition) |
||
129 | { |
||
130 | return new OrCondition($this, $condition); |
||
131 | } |
||
132 | |||
133 | public function protectCondition($condition) |
||
134 | { |
||
135 | return strpos($condition, ' AND ') || strpos($condition, ' OR ') ? '(' . $condition . ')' : $condition; |
||
136 | } |
||
137 | } |
||
138 |