Completed
Push — master ( 8cd9f7...9bbb8c )
by Adrian
03:39
created

Helper::alphanumhyphen()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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