Completed
Push — master ( 8a6007...930467 )
by Philip
02:12
created

Contains::validateParameterCount()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
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
use Valdi\ValidationException;
15
16
/**
17
 * Validator for strings containing a substring.
18
 */
19
class Contains implements ValidatorInterface {
20
21
    /**
22
     * Throws an exception if the parameters don't fullfill the expected
23
     * parameter count.
24
     *
25
     * @param integer $parameterAmount
26
     * the amount of expected parameters
27
     */
28
    protected function validateParameterCount($parameterAmount) {
29
        if ($parameterAmount < 1) {
30
            throw new ValidationException('"contains" expects at least 1 parameter.');
31
        }
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function isValid($value, array $parameters) {
38
        $parameterAmount = count($parameters);
39
40
        $this->validateParameterCount($parameterAmount);
41
42
        $caseInsensitive = $parameterAmount == 1 || $parameterAmount > 1 && $parameters[1];
43
44
        if ($caseInsensitive) {
45
            $parameters[0] = strtolower($parameters[0]);
46
            $value         = strtolower($value);
47
        }
48
        return in_array($value, array('', null), true) || strpos($value, $parameters[0]) !== false;
49
    }
50
}
51