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

Contains::isValidComparison()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 7
rs 9.4285
nc 2
cc 2
eloc 5
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