Rule::setAttributes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
namespace JayaCode\Framework\Core\Validator\Rule;
3
4
/**
5
 * Class Rule
6
 * @package JayaCode\Framework\Core\Validator\Rule
7
 */
8
abstract class Rule
9
{
10
    /**
11
     * @var array
12
     */
13
    protected $attributes = [];
14
15
    /**
16
     * @var array
17
     */
18
    protected $errorMessage = [];
19
    /**
20
     * @var null
21
     */
22
    protected $data = null;
23
    /**
24
     * @var bool
25
     */
26
    protected $requireAttribute = false;
27
28
    /**
29
     * Validator constructor.
30
     */
31 12
    public function __construct($rulesString, $defaultName)
32
    {
33 12
        $this->attributes['name'] = $defaultName;
34 12
        $this->setRule($rulesString);
35 12
    }
36
    
37
    /**
38
     * @param $rulesString
39
     * @throws \Exception
40
     */
41 12
    public function setRule($rulesString)
42
    {
43 12
        $rulesArr = explode(":", $rulesString);
44 12
        $this->attributes['validatorName'] = $rulesArr[0];
45
46 12
        if (isset($rulesArr[1])) {
47 6
            $this->setAttribute($rulesArr[1]);
48 12
        } elseif ($this->requireAttribute) {
49
            throw new \Exception("required arg = {$this->attributes['validatorName']}:argument");
50
        }
51 12
    }
52
53
    /**
54
     * @param $stringAttribute
55
     */
56 6
    protected function setAttribute($stringAttribute)
57
    {
58 6
        $this->attributes['arg'] = $stringAttribute;
59 6
    }
60
61
    /**
62
     * @return mixed
63
     */
64
    abstract public function isValid();
65
66
    /**
67
     * @return array
68
     */
69 12
    public function getErrorMessage()
70
    {
71 12
        return $this->errorMessage;
72
    }
73
74
    /**
75
     * @param $name
76
     * @param $message
77
     */
78 12
    protected function setErrorMessage($name, $message)
79
    {
80 12
        $this->errorMessage[$name] = $message;
81 12
    }
82
83
    /**
84
     * @return null
85
     */
86
    public function getData()
87
    {
88
        return $this->data;
89
    }
90
91
    /**
92
     * @param $data
93
     */
94 12
    public function setData($data)
95
    {
96 12
        $this->data = $data;
97 12
    }
98
99
    /**
100
     * @param $name
101
     * @param $value
102
     */
103
    public function setAttributes($name, $value)
104
    {
105
        $this->attributes[$name] = $value;
106
    }
107
}
108