http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 19 LOC: 345 Statements: 59

Source file Statements Methods Total coverage
Expression.php 100.0% 100.0% 100.0%
 
1
<?php
2
/**
3
 * Xyster Framework
4
 *
5
 * This source file is subject to the new BSD license that is bundled
6
 * with this package in the file LICENSE.txt.
7
 * It is also available through the world-wide-web at this URL:
8
 * http://www.opensource.org/licenses/bsd-license.php
9
 *
10
 * @category  Xyster
11
 * @package   Xyster_Data
12
 * @copyright Copyright (c) 2007-2008 Irrational Logic (http://irrationallogic.net)
13
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
14
 * @version   $Id$
15
 */
16
/**
17
 * @see Xyster_Data_Criterion
18
 */
19 1
require_once 'Xyster/Data/Criterion.php';
20
/**
21
 * @see Xyster_Data_Operator_Expression
22
 */
23 1
require_once 'Xyster/Data/Operator/Expression.php';
24
/**
25
 * An expression is a boolean evaluation comparing a column against a value
26
 *
27
 * @category  Xyster
28
 * @package   Xyster_Data
29
 * @copyright Copyright (c) 2007-2008 Irrational Logic (http://irrationallogic.net)
30
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
31
 */
32
class Xyster_Data_Expression extends Xyster_Data_Criterion
33
{
34
    /**
35
     * Internal list of valid expression operators
36
     *
37
     * @var array
38
     */
39
    static private $_operators = array(
40
        "eq" => "=",
41
        "neq" => "<>",
42
        "lt" => "<",
43
        "gt" => ">",
44
        "gte" => ">=",
45
        "lte" => "<=",
46
        "like" => "LIKE",
47
        "notLike" => "NOT LIKE",
48
        "between" => "BETWEEN",
49
        "notBetween" => "NOT BETWEEN",
50
        "in" => "IN",
51
        "notIn" => "NOT IN"
52
    );
53
54
    /**
55
     * The left value, always a field
56
     *
57
     * @var Xyster_Data_Field
58
     */
59
    protected $_left;
60
61
    /**
62
     * The operator
63
     *
64
     * @var Xyster_Data_Operator_Expression
65
     */
66
    protected $_operator;
67
68
    /**
69
     * The right value, could be a scalar or a {@link Xyster_Data_Field}
70
     *
71
     * @var mixed
72
     */
73
    protected $_right;
74
75
    /**
76
     * Creates a new expression
77
     *
78
     * @param Xyster_Data_Field|string $field
79
     * @param string $operator
80
     * @param mixed $value
81
     */
82
    protected function __construct( $field, $operator, $value )
83
    {
84 205
        if (! $field instanceof Xyster_Data_Field) {
85 155
            require_once 'Xyster/Data/Field.php';
86 155
            $field = Xyster_Data_Field::named($field);
87 155
        }
88 205
        $this->_left = $field;
89 205
        $this->_operator = Xyster_Enum::valueOf('Xyster_Data_Operator_Expression', $operator);
90 205
        if ( $value == "NULL" ) {
91 17
            $value = null;
92 17
        }
93 205
        $this->_right = $value;
94
    }
95
96
    /**
97
     * Gets the field
98
     *
99
     * @return Xyster_Data_Field
100
     */
101
    public function getLeft()
102
    {
103 112
        return $this->_left;
104
    }
105
106
    /**
107
     * Gets the operator
108
     *
109
     * @return Xyster_Data_Operator_Expression
110
     */
111
    public function getOperator()
112
    {
113 100
        return $this->_operator;
114
    }
115
116
    /**
117
     * Gets the value
118
     *
119
     * @return mixed
120
     */
121
    public function getRight()
122
    {
123 100
        return $this->_right;
124
    }
125
126
    /**
127
     * Returns the syntax for this Expression
128
     *
129
     * @magic
130
     * @return string
131
     */
132
    public function __toString()
133
    {
134 69
        $string = "";
135 69
        $op = $this->_operator->getValue();
136 69
        $val = $this->_right;
137
138 69
        if ( $val === null ) {
139 1
            $string .= 'NULL';
140 69
        } else if ( is_array($val) && strpos($op, 'BETWEEN') !== false ) {
141 2
            $string .= ( preg_match("/^[0-9.]+$/", $val[0]) ) ?
142 2
                $val[0] : "'" . str_replace("'", "''", $val[0]) . "'";
143 2
            $string .= ' AND ';
144 2
            $string .= ( preg_match("/^[0-9.]+$/", $val[1]) ) ?
145 2
                $val[1] : "'" . str_replace("'", "''", $val[1]) . "'";
146 69
        } else if ( is_array($val) && $op == "IN" || $op == "NOT IN" ) {
147 2
            $quoted = array();
148 2
            foreach( $val as $v ) {
149 2
                $quoted[] = ( preg_match("/^[0-9.]+$/", $v) ) ?
150 2
                    $v : "'" . str_replace("'", "''", $v) . "'";
151 2
            }
152 2
            $string .= '(' . implode(',', $quoted) . ')';
153 69
        } else if ( $val instanceof Xyster_Data_Field ) {
154 1
            $string .= (string)$val;
155 68
        } else if ( !is_numeric($val) ) {
156 31
            $string .= "'" . str_replace("'", "''", $val) . "'";
157 31
        } else {
158 49
            $string .= $val;
159
        }
160
161 69
        return (string)$this->_left . " " . $op . " " . $string;
162
    }
163
164
    /**
165
     * Evaluates the Expression for a given array or object
166
     *
167
     * @param mixed $object
168
     * @return boolean
169
     */
170
    public function evaluate( $object )
171
    {
172 58
        $a = $this->_left->evaluate($object);
173 58
        $b = ( $this->_right instanceof Xyster_Data_Field ) ?
174 58
            $this->_right->evaluate($object) : $this->_right;
175 58
        return $this->_operator->evaluate($a, $b);
176
    }
177
178
    /**
179
     * Returns the name of the static method to call for the operator passed
180
     *
181
     * @param string $operator
182
     * @return string
183
     */
184
    static public function getMethodName( $operator )
185
    {
186 121
        $method = array_search($operator, array_flip(Xyster_Enum::values('Xyster_Data_Operator_Expression')));
187 121
        return strlen($method) > 1 ? strtolower($method[0]) . substr($method, 1) : false;
188
    }
189
190
    /**
191
     * Equal To Xyster_Data_Expression ( field = 'value' )
192
     *
193
     * For $value to reference another field, pass a {@link Xyster_Data_Field} object
194
     *
195
     * @param string $field
196
     * @param string $value
197
     * @return Xyster_Data_Expression
198
     */
199
    static public function eq( $field, $value )
200
    {
201 134
        return new Xyster_Data_Expression($field, self::$_operators[__FUNCTION__], $value);
202
    }
203
204
    /**
205
     * Not Equal To Xyster_Data_Expression ( field <> 'value' )
206
     *
207
     * For $value to reference another field, pass a {@link Xyster_Data_Field} object
208
209
     * @param string $field
210
     * @param string $value
211
     * @return Xyster_Data_Expression
212
     */
213
    static public function neq( $field, $value )
214
    {
215 120
        return new Xyster_Data_Expression($field, self::$_operators[__FUNCTION__], $value);
216
    }
217
218
    /**
219
     * Less Than Xyster_Data_Expression ( field < 3 )
220
     *
221
     * For $value to reference another field, pass a {@link Xyster_Data_Field} object
222
     *
223
     * @param string $field
224
     * @param string $value
225
     * @return Xyster_Data_Expression
226
     */
227
    static public function lt( $field, $value )
228
    {
229 4
        return new Xyster_Data_Expression($field, self::$_operators[__FUNCTION__], $value);
230
    }
231
232
    /**
233
     * Less Than or Equal To Xyster_Data_Expression ( field <= 3 )
234
     *
235
     * @param string $field
236
     * @param string $value
237
     * @return Xyster_Data_Expression
238
     */
239
    static public function lte( $field, $value )
240
    {
241 2
        return new Xyster_Data_Expression($field, self::$_operators[__FUNCTION__], $value);
242
    }
243
244
    /**
245
     * Greater Than Xyster_Data_Expression ( field > 2 )
246
     *
247
     * For $value to reference another field, pass a {@link Xyster_Data_Field} object
248
     *
249
     * @param string $field
250
     * @param string $value
251
     * @return Xyster_Data_Expression
252
     */
253
    static public function gt( $field, $value )
254
    {
255 26
        return new Xyster_Data_Expression($field, self::$_operators[__FUNCTION__], $value);
256
    }
257
258
    /**
259
     * Greater Than or Equal To Xyster_Data_Expression ( field >= 2 )
260
     *
261
     * For $value to reference another field, pass a {@link Xyster_Data_Field} object
262
     *
263
     * @param string $field
264
     * @param string $value
265
     * @return Xyster_Data_Expression
266
     */
267
    static public function gte( $field, $value )
268
    {
269 2
        return new Xyster_Data_Expression($field, self::$_operators[__FUNCTION__], $value);
270
    }
271
272
    /**
273
     * LIKE Xyster_Data_Expression ( field LIKE '%value' )
274
     *
275
     * @param string $field
276
     * @param string $value
277
     * @return Xyster_Data_Expression
278
     */
279
    static public function like( $field, $value )
280
    {
281 9
        return new Xyster_Data_Expression($field, self::$_operators[__FUNCTION__], $value);
282
    }
283
284
    /**
285
     * NOT LIKE Xyster_Data_Expression ( field NOT LIKE '%value' )
286
     *
287
     * @param string $field
288
     * @param string $value
289
     * @return Xyster_Data_Expression
290
     */
291
    static public function notLike( $field, $value )
292
    {
293 6
        return new Xyster_Data_Expression($field, self::$_operators[__FUNCTION__], $value);
294
    }
295
296
    /**
297
     * BETWEEN Xyster_Data_Expression ( field BETWEEN 'value' AND 'value' )
298
     *
299
     * @param string $field
300
     * @param string $start
301
     * @param string $end
302
     * @return Xyster_Data_Expression
303
     */
304
    static public function between( $field, $start, $end )
305
    {
306 5
        return new Xyster_Data_Expression($field, self::$_operators[__FUNCTION__], array($start, $end));
307
    }
308
309
    /**
310
     * Equal To Xyster_Data_Expression ( field NOT BETWEEN 'value' AND 'value' )
311
     *
312
     * @param string $field
313
     * @param string $start
314
     * @param string $end
315
     * @return Xyster_Data_Expression
316
     */
317
    static public function notBetween( $field, $start, $end )
318
    {
319 2
        return new Xyster_Data_Expression($field, self::$_operators[__FUNCTION__], array($start, $end));
320
    }
321
322
    /**
323
     * In expression ( field IN ( 1,1,2,3,5,8,13,21,'fibonacci','sequence' ) )
324
     *
325
     * @param string $field
326
     * @param array $choices
327
     * @return Xyster_Data_Expression
328
     */
329
    static public function in( $field, array $choices )
330
    {
331 4
        return new Xyster_Data_Expression($field, self::$_operators[__FUNCTION__], $choices);
332
    }
333
334
    /**
335
     * Not in expression ( field NOT IN ( 1,1,2,3,5,8,13,21,'fibonacci','sequence' ) )
336
     *
337
     * @param string $field
338
     * @param array $choices
339
     * @return Xyster_Data_Expression
340
     */
341
    static public function notIn( $field, array $choices )
342
    {
343 3
        return new Xyster_Data_Expression($field, self::$_operators[__FUNCTION__], $choices);
344
    }
345
}


Report generated at 2008-03-05T18:27:43-05:00