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

Contains   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 6
c 2
b 0
f 1
lcom 0
cbo 0
dl 0
loc 21
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B isValid() 0 15 6
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