http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

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


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