Completed
Push — master ( db029f...19202d )
by Restu
16:00
created

Rule::isValid()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
nc 1
dl 0
loc 1
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
    public function __construct($rulesString, $defaultName)
32
    {
33
        $this->attributes['name'] = $defaultName;
34
        $this->setRule($rulesString);
35
    }
36
    
37
    /**
38
     * @param $rulesString
39
     * @throws \Exception
40
     */
41
    public function setRule($rulesString)
42
    {
43
        $rulesArr = explode(":", $rulesString);
44
        $this->attributes['validatorName'] = $rulesArr[0];
45
46
        if (isset($rulesArr[1])) {
47
            $this->setAttribute($rulesArr[1]);
48
        } elseif ($this->requireAttribute) {
49
            throw new \Exception("required arg = {$this->attributes['validatorName']}:argument");
50
        }
51
    }
52
53
    /**
54
     * @param $stringAttribute
55
     */
56
    protected function setAttribute($stringAttribute)
57
    {
58
        $this->attributes['arg'] = $stringAttribute;
59
    }
60
61
    /**
62
     * @return mixed
63
     */
64
    abstract public function isValid();
65
66
    /**
67
     * @return array
68
     */
69
    public function getErrorMessage()
70
    {
71
        return $this->errorMessage;
72
    }
73
74
    /**
75
     * @param $name
76
     * @param $message
77
     */
78
    protected function setErrorMessage($name, $message)
79
    {
80
        $this->errorMessage[$name] = $message;
81
    }
82
83
    /**
84
     * @return null
85
     */
86
    public function getData()
87
    {
88
        return $this->data;
89
    }
90
91
    /**
92
     * @param null $data
93
     */
94
    public function setData($data)
95
    {
96
        $this->data = $data;
97
    }
98
99
    /**
100
     * @param $name
101
     * @param $value
102
     */
103
    public function setAttributes($name, $value)
104
    {
105
        $this->attributes[$name] = $value;
106
    }
107
}
108