Completed
Push — master ( 121fac...aa6513 )
by Harry Osmar
06:34 queued 03:36
created

Rule::getError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
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
    private $validationMethod;
19
20
    /**
21
     * @var string
22
     */
23
    private $langKey;
24
25
    /**
26
     * @var bool
27
     */
28
    private $status;
29
30
    /**
31
     * @var string
32
     */
33
    private $error;
34
35
    /**
36
     * @var array
37
     */
38
    private $args;
39
40
    /**
41
     * Rule constructor.
42
     * @param string $validationMethod
43
     * @param string $langKey
44
     * @param array $args
45
     */
46 9
    public function __construct(string $validationMethod, string $langKey, $args = [])
47
    {
48 9
        $this->validationMethod = $validationMethod;
49 9
        $this->langKey = $langKey;
50 9
        $this->status = false;
51 9
        $this->args = $args;
52 9
        $this->error = '';
53 9
    }
54
55
    /**
56
     * @return string
57
     */
58 9
    public function getValidationMethod(): string
59
    {
60 9
        return $this->validationMethod;
61
    }
62
63
    /**
64
     * @return string
65
     */
66 9
    public function getLangKey(): string
67
    {
68 9
        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 9
    public function isValid(Language $language, $value) : bool
85
    {
86 9
        if (!call_user_func_array(
87 9
            '\\PluginSimpleValidate\\helper\\Validate\\' . $this->getValidationMethod(),
88
            [
89 9
                $value,
90 9
                $this->args
91
            ]
92
        )) {
93 9
            $this->status = false;
94
95 9
            $this->error = vsprintf(
96 9
                $language->getTranslation(
97 9
                    $this->getLangKey()
98
                ),
99 9
                $this->args
100
            );
101
102
        } else {
103 1
            $this->status = true;
104
        }
105
106 9
        return $this->status;
107
    }
108
109
    /**
110
     * @return string
111
     */
112 9
    public function getError(): string
113
    {
114 9
        return $this->error;
115
    }
116
}