Completed
Push — master ( 14b48b...4568b1 )
by Anton
13s
created

ContainsRule   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
dl 0
loc 48
ccs 10
cts 11
cp 0.9091
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A validate() 0 13 3
A getDescription() 0 4 1
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