/
Comparison.php
185 lines (167 loc) · 4.47 KB
/
Comparison.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Database\Expression;
use Cake\Database\ExpressionInterface;
use Cake\Database\ValueBinder;
/**
* A Comparison is a type of query expression that represents an operation
* involving a field an operator and a value. In its most common form the
* string representation of a comparison is `field = value`
*
* @internal
*/
class Comparison extends QueryExpression {
/**
* The field name or expression to be used in the left hand side of the operator
*
* @var string
*/
protected $_field;
/**
* The value to be used in the right hand side of the operation
*
* @var mixed
*/
protected $_value;
/**
* The type to be used for casting the value to a database representation
*
* @var string
*/
protected $_type;
/**
* Constructor
*
* @param string $field the field name to compare to a value
* @param mixed $value The value to be used in comparison
* @param string $type the type name used to cast the value
* @param string $conjunction the operator used for comparing field and value
*/
public function __construct($field, $value, $type, $conjunction) {
$this->field($field);
$this->value($value);
$this->type($conjunction);
if (is_string($type)) {
$this->_type = $type;
}
if (is_string($field) && isset($types[$this->_field])) {
$this->_type = current($types);
}
}
/**
* Sets the field name
*
* @param string $field
* @return void
*/
public function field($field) {
$this->_field = $field;
}
/**
* Sets the value
*
* @param mixed $value
* @return void
*/
public function value($value) {
$this->_value = $value;
}
/**
* Returns the field name
*
* @return string
*/
public function getField() {
return $this->_field;
}
/**
* Returns the value used for comparison
*
* @return mixed
*/
public function getValue() {
return $this->_value;
}
/**
* Convert the expression into a SQL fragment.
*
* @param \Cake\Database\ValueBinder $generator Placeholder generator object
* @return string
*/
public function sql(ValueBinder $generator) {
if ($this->_value instanceof ExpressionInterface) {
$template = '%s %s (%s)';
$value = $this->_value->sql($generator);
} else {
list($template, $value) = $this->_stringExpression($generator);
}
return sprintf($template, $this->_field, $this->_conjunction, $value);
}
/**
* Returns a template and an placeholder for the value after registering it
* with the placeholder $generator
*
* @param ValueBinder $generator
* @return array First position containing the template and the second a placeholder
*/
protected function _stringExpression($generator) {
if (strpos($this->_type, '[]') !== false) {
$template = '%s %s (%s)';
$type = str_replace('[]', '', $this->_type);
$value = $this->_flattenValue($this->_value, $generator, $type);
} else {
$template = '%s %s %s';
$value = $this->_bindValue($this->_value, $generator, $this->_type);
}
return [$template, $value];
}
/**
* Registers a value in the placeholder generator and returns the generated placeholder
*
* @param mixed $value
* @param ValueBinder $generator
* @param string $type
* @return string generated placeholder
*/
protected function _bindValue($value, $generator, $type) {
$placeholder = $generator->placeholder($this->_field);
$generator->bind($placeholder, $value, $type);
return $placeholder;
}
/**
* Converts a traversable value into a set of placeholders generated by
* $generator and separated by `,`
*
* @param array|\Traversable $value the value to flatten
* @param ValueBinder $generator
* @param string|array $type the type to cast values to
* @return string
*/
protected function _flattenValue($value, $generator, $type = null) {
$parts = [];
foreach ($value as $k => $v) {
$parts[] = $this->_bindValue($v, $generator, $type);
}
return implode(',', $parts);
}
/**
* Returns the number of expression this class represents
*
* @return int
*/
public function count() {
return 1;
}
}