Completed
Push — master ( 787391...fa93b3 )
by Adrian
02:11 queued 10s
created

Helper::notEqualTo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
c 0
b 0
f 0
rs 10
cc 2
nc 2
nop 3
crap 2
1
<?php
2
namespace Sirius\Validation;
3
4
use Sirius\Validation\Util\Arr;
5
6
class Helper
7
{
8
    protected static $methods = array();
9
10 2
    public static function addMethod($ruleName, $callback)
11
    {
12 2
        if (is_callable($callback)) {
13 1
            self::$methods[$ruleName] = $callback;
14
15 1
            return true;
16
        }
17
18 1
        return false;
19
    }
20
21 1
    public static function methodExists($name)
22
    {
23 1
        return method_exists(__CLASS__, $name) || array_key_exists($name, self::$methods);
24
    }
25
26 2
    public static function __callStatic($name, $arguments)
27
    {
28 2
        if (array_key_exists($name, self::$methods)) {
29 1
            return call_user_func_array(self::$methods[$name], $arguments);
30
        }
31 1
        throw new \InvalidArgumentException(sprintf('Validation method "%s" does not exist', $name));
32
    }
33
34 1
    public static function callback($value, $callback, $context = array())
35
    {
36 1
        $validator = new Rule\Callback();
37 1
        $validator->setOption('callback', $callback);
38 1
        $validator->setContext($context);
39
40 1
        return $validator->validate($value);
41
    }
42
43 1
    public static function required($value)
44
    {
45 1
        return $value !== null && trim($value) !== '';
46
    }
47
48 2
    public static function truthy($value)
49
    {
50 2
        return (bool) $value;
51
    }
52
53 1
    public static function falsy($value)
54
    {
55 1
        return ! static::truthy($value);
56
    }
57
58 1
    public static function number($value)
59
    {
60 1
        return $value == '0' || is_numeric($value);
61
    }
62
63 1
    public static function integer($value)
64
    {
65 1
        return $value == '0' || (int) $value == $value;
66
    }
67
68 1
    public static function lessThan($value, $max)
69
    {
70 1
        $validator = new Rule\LessThan(
71
            array(
72
                'max' => $max
73 1
            )
74 1
        );
75
76 1
        return $validator->validate($value);
77
    }
78
79 1
    public static function greaterThan($value, $min)
80
    {
81 1
        $validator = new Rule\GreaterThan(
82
            array(
83
                'min' => $min
84 1
            )
85 1
        );
86
87 1
        return $validator->validate($value);
88
    }
89
90 1
    public static function between($value, $min, $max)
91
    {
92 1
        $validator = new Rule\Between(
93
            array(
94 1
                'min' => $min,
95
                'max' => $max
96 1
            )
97 1
        );
98
99 1
        return $validator->validate($value);
100
    }
101
102 2
    public static function exactly($value, $otherValue)
103
    {
104 2
        return $value == $otherValue;
105
    }
106
107 1
    public static function not($value, $otherValue)
108
    {
109 1
        return ! self::exactly($value, $otherValue);
110
    }
111
112 1
    public static function alpha($value)
113
    {
114 1
        $validator = new Rule\Alpha();
115
116 1
        return $validator->validate($value);
117
    }
118
119 1
    public static function alphanumeric($value)
120
    {
121 1
        $validator = new Rule\AlphaNumeric();
122
123 1
        return $validator->validate($value);
124
    }
125
126 1
    public static function alphanumhyphen($value)
127
    {
128 1
        $validator = new Rule\AlphaNumHyphen();
129
130 1
        return $validator->validate($value);
131
    }
132
133 1
    public static function minLength($value, $min)
134
    {
135 1
        $validator = new Rule\MinLength(
136
            array(
137
                'min' => $min
138 1
            )
139 1
        );
140
141 1
        return $validator->validate($value);
142
    }
143
144 1
    public static function maxLength($value, $max)
145
    {
146 1
        $validator = new Rule\MaxLength(
147
            array(
148
                'max' => $max
149 1
            )
150 1
        );
151
152 1
        return $validator->validate($value);
153
    }
154
155 1
    public static function length($value, $min, $max)
156
    {
157 1
        $validator = new Rule\Length(
158
            array(
159 1
                'min' => $min,
160
                'max' => $max
161 1
            )
162 1
        );
163
164 1
        return $validator->validate($value);
165
    }
166
167 1
    public static function setMinSize($value, $min)
168
    {
169 1
        $validator = new Rule\ArrayMinLength(
170
            array(
171
                'min' => $min
172 1
            )
173 1
        );
174
175 1
        return $validator->validate($value);
176
    }
177
178 1
    public static function setMaxSize($value, $max)
179
    {
180 1
        $validator = new Rule\ArrayMaxLength(
181
            array(
182
                'max' => $max
183 1
            )
184 1
        );
185
186 1
        return $validator->validate($value);
187
    }
188
189 1
    public static function setSize($value, $min, $max)
190
    {
191 1
        $validator = new Rule\ArrayLength(
192
            array(
193 1
                'min' => $min,
194
                'max' => $max
195 1
            )
196 1
        );
197
198 1
        return $validator->validate($value);
199
    }
200
201 1
    public static function inList($value, $values)
202
    {
203 1
        $validator = new Rule\InList(
204
            array(
205
                'list' => $values
206 1
            )
207 1
        );
208
209 1
        return $validator->validate($value);
210
    }
211
212 1
    public static function notInList($value, $values)
213
    {
214 1
        $validator = new Rule\NotInList(
215
            array(
216
                'list' => $values
217 1
            )
218 1
        );
219
220 1
        return $validator->validate($value);
221
    }
222
223 1
    public static function regex($value, $pattern)
224
    {
225 1
        $validator = new Rule\Regex(
226
            array(
227
                'pattern' => $pattern
228 1
            )
229 1
        );
230
231 1
        return $validator->validate($value);
232
    }
233
234 1
    public static function notRegex($value, $pattern)
235
    {
236 1
        $validator = new Rule\NotRegex(
237
            array(
238
                'pattern' => $pattern
239 1
            )
240 1
        );
241
242 1
        return $validator->validate($value);
243
    }
244
245 2
    public static function equalTo($value, $otherElementOrValue, $context = null)
246
    {
247 2
        if (func_num_args() == 2) {
248 1
            return $value == $otherElementOrValue;
249
        }
250
251 1
        return $value == Arr::getByPath($context, $otherElementOrValue);
252
    }
253
254 1
    public static function notEqualTo($value, $otherElementOrValue, $context = null)
255
    {
256 1
        if (func_num_args() == 2) {
257
            return $value != $otherElementOrValue;
258
        }
259 1
260 1
        return $value != Arr::getByPath($context, $otherElementOrValue);
261
    }
262 1
263
    public static function date($value, $format = 'Y-m-d')
264
    {
265 1
        $validator = new Rule\Date(
266
            array(
267 1
                'format' => $format
268
            )
269
        );
270 1
271 1
        return $validator->validate($value);
272
    }
273 1
274
    public static function dateTime($value, $format = 'Y-m-d H:i:s')
275
    {
276 1
        $validator = new Rule\DateTime(
277
            array(
278 1
                'format' => $format
279
            )
280
        );
281 1
282 1
        return $validator->validate($value);
283
    }
284 1
285
    public static function time($value, $format = 'H:i:s')
286
    {
287 1
        $validator = new Rule\Time(
288
            array(
289 1
                'format' => $format
290
            )
291 1
        );
292
293
        return $validator->validate($value);
294 1
    }
295
296 1
    public static function website($value)
297
    {
298 1
        $validator = new Rule\Website();
299
300
        return $validator->validate($value);
301
    }
302
303
    public static function url($value)
304
    {
305
        $validator = new Rule\Url();
306
307
        return $validator->validate($value);
308 1
    }
309
310 1
    /**
311
     * Test if a variable is a valid IP address
312 1
     *
313
     * @param string $value
314
     *
315 1
     * @return bool
316
     */
317 1
    public static function ipAddress($value)
318
    {
319 1
        $validator = new Rule\IpAddress();
320
321
        return $validator->validate($value);
322
    }
323
324
    public static function email($value)
325
    {
326
        $validator = new Rule\Email();
327
328
        return $validator->validate($value);
329
    }
330 1
331
    /**
332 1
     * Test if a variable is a full name
333
     * Criterias: at least 6 characters, 2 words
334 1
     *
335
     * @param mixed $value
336
     *
337
     * @return bool
338
     */
339
    public static function fullName($value)
340
    {
341
        $validator = new Rule\FullName();
342
343
        return $validator->validate($value);
344 1
    }
345
346 1
    /**
347
     * Test if the domain of an email address is available
348 1
     *
349
     * @param string $value
350
     *
351
     * @return bool
352
     */
353
    public static function emailDomain($value)
354
    {
355
        $validator = new Rule\EmailDomain();
356
357
        return $validator->validate($value);
358
    }
359
}
360