http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 13 LOC: 229 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: Expression.php 219 2008-02-09 18:02:48Z doublecompile $
15
 */
16
/**
17
 * @see Xyster_Data_Enum
18
 */
19 1
require_once 'Xyster/Enum.php';
20
/**
21
 * Enumerated type of Expression operators
22
 *
23
 * @category  Xyster
24
 * @package   Xyster_Data
25
 * @copyright Copyright (c) 2007-2008 Irrational Logic (http://irrationallogic.net)
26
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
27
 */
28
class Xyster_Data_Operator_Expression extends Xyster_Enum
29
{
30
	const Eq = '=';
31
	const Neq = '<>';
32
	const Lt = '<';
33
	const Gt = '>';
34
	const Gte = '>=';
35
	const Lte = '<=';
36
	const Like = 'LIKE';
37
	const NotLike = 'NOT LIKE';
38
	const Between = 'BETWEEN';
39
	const NotBetween = 'NOT BETWEEN';
40
	const In = 'IN';
41
	const NotIn = 'NOT IN';
42
43
    /**
44
     * Evaluates the operator for two values
45
     *
46
     * @param mixed $a
47
     * @param mixed $b
48
     * @return boolean
49
     */
50
    public function evaluate( $a, $b )
51
    {
52 59
        $operator = $this->getValue();
53
54 59
        $bool = false;
55 59
        $eval = "\$bool = (bool)( \$a %s \$b );";
56
        switch( $operator ) {
57 59
            case "=":
58 45
                eval(sprintf($eval, '=='));
59 45
                break;
60
61 27
            case "<>":
62 7
                eval(sprintf($eval, '!='));
63 7
                break;
64
65 22
            case "LIKE":
66 22
            case "NOT LIKE":
67 4
                $lookend = ( substr($b, 0, 1) == '%' );
68 4
                $lookbeg = ( substr($b, -1, 1) == '%' );
69 4
                $lookin = ( $lookbeg && $lookend );
70 4
                if ( $lookin ) {
71 4
                    $bool = strpos($a, substr($b, 1, strlen($b)-2)) > -1;
72 4
                } else if ( $lookbeg ) {
73 3
                    $bool = (substr($b, 0, -1) == substr($a, 0, strlen($b)-1));
74 3
                } else if ( $lookend ) {
75 3
                    $match = substr($b, 1);
76 3
                    $bool = $match == substr($a, -strlen($match));
77 3
                }
78 4
                if ( $operator == "NOT LIKE" ) {
79 2
                    $bool = !$bool;
80 2
                }
81 4
                break;
82
83 20
            case "IN":
84 2
                $bool = ( in_array($a, $b) );
85 2
                break;
86
87 19
            case "NOT IN":
88 2
                $bool = ( !in_array($a, $b) );
89 2
                break;
90
91 18
            case "BETWEEN":
92 2
                $bool = ( $a >= $b[0] ) && ( $a <= $b[1] );
93 2
                break;
94
95 17
            case "NOT BETWEEN":
96 2
                $bool = ( $a < $b[0] ) || ( $a > $b[1] );
97 2
                break;
98
99 16
            case ">":
100 16
            case "<":
101 16
            case ">=":
102 16
            case "<=":
103 16
            default:
104 16
                eval(sprintf($eval, $operator));
105 16
        }
106
107 59
        return $bool;
108
    }
109
110
    /**
111
     * Uses the Eq operator
112
     *
113
     * @return Xyster_Data_Operator_Expression
114
     */
115
    public static function Eq()
116
    {
117 3
       return Xyster_Enum::_factory();
118
    }
119
120
    /**
121
     * Uses the Neq operator
122
     *
123
     * @return Xyster_Data_Operator_Expression
124
     */
125
    public static function Neq()
126
    {
127 3
       return Xyster_Enum::_factory();
128
    }
129
130
    /**
131
     * Uses the Lt operator
132
     *
133
     * @return Xyster_Data_Operator_Expression
134
     */
135
    public static function Lt()
136
    {
137 3
       return Xyster_Enum::_factory();
138
    }
139
140
    /**
141
     * Uses the Lte operator
142
     *
143
     * @return Xyster_Data_Operator_Expression
144
     */
145
    public static function Lte()
146
    {
147 3
       return Xyster_Enum::_factory();
148
    }
149
150
    /**
151
     * Uses the Gt operator
152
     *
153
     * @return Xyster_Data_Operator_Expression
154
     */
155
    public static function Gt()
156
    {
157 3
       return Xyster_Enum::_factory();
158
    }
159
160
    /**
161
     * Uses the Gte operator
162
     *
163
     * @return Xyster_Data_Operator_Expression
164
     */
165
    public static function Gte()
166
    {
167 3
       return Xyster_Enum::_factory();
168
    }
169
170
    /**
171
     * Uses the Like operator
172
     *
173
     * @return Xyster_Data_Operator_Expression
174
     */
175
    public static function Like()
176
    {
177 3
       return Xyster_Enum::_factory();
178
    }
179
180
    /**
181
     * Uses the NotLike operator
182
     *
183
     * @return Xyster_Data_Operator_Expression
184
     */
185
    public static function NotLike()
186
    {
187 3
       return Xyster_Enum::_factory();
188
    }
189
190
    /**
191
     * Uses the Between operator
192
     *
193
     * @return Xyster_Data_Operator_Expression
194
     */
195
    public static function Between()
196
    {
197 3
       return Xyster_Enum::_factory();
198
    }
199
200
    /**
201
     * Uses the NotBetween operator
202
     *
203
     * @return Xyster_Data_Operator_Expression
204
     */
205
    public static function NotBetween()
206
    {
207 3
       return Xyster_Enum::_factory();
208
    }
209
210
    /**
211
     * Uses the In operator
212
     *
213
     * @return Xyster_Data_Operator_Expression
214
     */
215
    public static function In()
216
    {
217 3
       return Xyster_Enum::_factory();
218
    }
219
220
    /**
221
     * Uses the NotIn operator
222
     *
223
     * @return Xyster_Data_Operator_Expression
224
     */
225
    public static function NotIn()
226
    {
227 4
       return Xyster_Enum::_factory();
228
    }
229
}


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