http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

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


Report generated at 2010-10-18T17:19:49-04:00