Completed
Push — master ( 869eaa...8a6007 )
by Philip
02:14
created

Contains::isValid()   B

Complexity

Conditions 6
Paths 13

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 8.8571
cc 6
eloc 9
nc 13
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Valdi package.
5
 *
6
 * (c) Philip Lehmann-Böhm <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Valdi\Validator;
13
14
/**
15
 * Validator for strings containing a substring.
16
 */
17
class Contains implements ValidatorInterface {
18
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function isValid($value, array $parameters) {
23
        $parametersCount = count($parameters);
24
25
        if ($parametersCount < 1) {
26
            throw new ValidationException('"contains" expects at least 1 parameter.');
27
        }
28
29
        $caseInsensitive = $parametersCount == 1 || $parametersCount > 1 && $parameters[1];
30
31
        if ($caseInsensitive) {
32
            $parameters[0] = strtolower($parameters[0]);
33
            $value         = strtolower($value);
34
        }
35
        return in_array($value, array('', null), true) || strpos($value, $parameters[0]) !== false;
36
    }
37
}
38