Rule::isValid()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 13
nc 2
nop 2
dl 0
loc 24
ccs 12
cts 12
cp 1
crap 2
rs 8.9713
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: harry
5
 * Date: 2/15/18
6
 * Time: 5:05 PM
7
 */
8
9
namespace PluginSimpleValidate;
10
11
use PluginSimpleValidate\Libraries\Language;
12
13
class Rule implements \PluginSimpleValidate\Contracts\Rule
14
{
15
    /**
16
     * @var string
17
     */
18
    protected $validationMethod;
19
20
    /**
21
     * @var string
22
     */
23
    protected $langKey;
24
25
    /**
26
     * @var bool
27
     */
28
    protected $status;
29
30
    /**
31
     * @var string
32
     */
33
    protected $error;
34
35
    /**
36
     * @var array
37
     */
38
    protected $args;
39
40
    /**
41
     * Rule constructor.
42
     * @param string $validationMethod
43
     * @param string $langKey
44
     * @param array $args
45
     */
46 16
    public function __construct(string $validationMethod, string $langKey, $args = [])
47
    {
48 16
        $this->validationMethod = $validationMethod;
49 16
        $this->langKey = $langKey;
50 16
        $this->status = false;
51 16
        $this->args = $args;
52 16
        $this->error = '';
53 16
    }
54
55
    /**
56
     * @return string
57
     */
58 16
    public function getValidationMethod(): string
59
    {
60 16
        return $this->validationMethod;
61
    }
62
63
    /**
64
     * @return string
65
     */
66 16
    public function getLangKey(): string
67
    {
68 16
        return $this->langKey;
69
    }
70
71
    /**
72
     * @return bool
73
     */
74
    public function getStatus(): bool
75
    {
76
        return $this->status;
77
    }
78
79
    /**
80
     * @param Language $language
81
     * @param $value
82
     * @return bool
83
     */
84 16
    public function isValid(Language $language, $value) : bool
85
    {
86 16
        if (!call_user_func_array(
87 16
            '\\PluginSimpleValidate\\helper\\Validate\\' . $this->getValidationMethod(),
88
            [
89 16
                $value,
90 16
                $this->args
91
            ]
92
        )) {
93 16
            $this->status = false;
94
95 16
            $this->error = vsprintf(
96 16
                $language->getTranslation(
97 16
                    $this->getLangKey()
98
                ),
99 16
                $this->args
100
            );
101
102
        } else {
103 4
            $this->status = true;
104
        }
105
106 16
        return $this->status;
107
    }
108
109
    /**
110
     * @return string
111
     */
112 16
    public function getError(): string
113
    {
114 16
        return $this->error;
115
    }
116
}