ContainsRule::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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