Completed
Push — master ( ccf0eb...14b48b )
by Anton
10s
created

ContainsRule::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 13
ccs 5
cts 5
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link      https://github.com/bluzphp/framework
7
 */
8
9
declare(strict_types=1);
10
11
namespace Bluz\Validator\Rule;
12
13
/**
14
 * Check for contains
15
 *
16
 * @package Bluz\Validator\Rule
17
 */
18
class ContainsRule extends AbstractRule
19
{
20
    /**
21
     * @var string needle for search inside input data (haystack)
22
     */
23
    protected $containsValue;
24
25
    /**
26
     * Setup validation rule
27
     *
28
     * @param mixed $containsValue
29
     */
30 15
    public function __construct($containsValue)
31
    {
32 15
        $this->containsValue = $containsValue;
33 15
    }
34
35
    /**
36
     * Check input data
37
     *
38
     * @param  string|array $input
39
     *
40
     * @return bool
41
     */
42 9
    public function validate($input): bool
43
    {
44
        // for array
45 9
        if (is_array($input)) {
46 4
            return in_array($this->containsValue, $input, false);
47
        }
48
        // for string
49 5
        if (is_string($input)) {
50 5
            return false !== mb_stripos($input, $this->containsValue, 0, mb_detect_encoding($input));
51
        }
52
        // can't compare
53
        return false;
54
    }
55
56
    /**
57
     * Get error template
58
     *
59
     * @return string
60
     */
61 4
    public function getDescription() : string
62
    {
63 4
        return __('must contain the value "%s"', $this->containsValue);
64
    }
65
}
66