|
1
|
|
|
<?php |
|
2
|
|
|
namespace JayaCode\Framework\Core\Validator\Dispatcher; |
|
3
|
|
|
|
|
4
|
|
|
use JayaCode\Framework\Core\Validator\Rule\Rule; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class BasicValidatorDispatcher |
|
8
|
|
|
* @package JayaCode\Framework\Core\Validator\Dispatcher |
|
9
|
|
|
*/ |
|
10
|
|
|
class BasicValidatorDispatcher extends ValidatorDispatcher |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @param array $data |
|
15
|
|
|
* @return bool |
|
16
|
|
|
*/ |
|
17
|
12 |
|
public function isValid($data = []) |
|
18
|
|
|
{ |
|
19
|
12 |
|
$this->errorMessage = []; |
|
20
|
|
|
|
|
21
|
12 |
|
$isValid = true; |
|
22
|
12 |
|
foreach ($this->rules as $nameData => $rules) { |
|
23
|
12 |
|
$this->rulesCheckValid($rules, $data, $nameData, $isValid); |
|
24
|
12 |
|
} |
|
25
|
|
|
|
|
26
|
12 |
|
return $isValid; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param array $rules |
|
31
|
|
|
* @param $data |
|
32
|
|
|
* @param $nameData |
|
33
|
|
|
* @param $isValid |
|
34
|
|
|
*/ |
|
35
|
12 |
|
protected function rulesCheckValid(array $rules, $data, $nameData, &$isValid) |
|
36
|
|
|
{ |
|
37
|
12 |
|
foreach ($rules as $rule) { |
|
38
|
|
|
/** @var Rule $rule */ |
|
39
|
12 |
|
$this->ruleCheckValid($rule, $data, $nameData, $isValid); |
|
40
|
12 |
|
} |
|
41
|
12 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param Rule $rule |
|
45
|
|
|
* @param $data |
|
46
|
|
|
* @param $nameData |
|
47
|
|
|
* @param $isValid |
|
48
|
|
|
*/ |
|
49
|
12 |
|
protected function ruleCheckValid(Rule $rule, $data, $nameData, &$isValid) |
|
50
|
|
|
{ |
|
51
|
12 |
|
$rule->setData(arr_get($data, $nameData)); |
|
52
|
12 |
|
if (!$rule->isValid()) { |
|
53
|
12 |
|
$this->initRuleErrorMessage($rule->getErrorMessage(), $nameData); |
|
54
|
12 |
|
$isValid = $isValid?false:$isValid; |
|
55
|
12 |
|
} |
|
56
|
12 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param $messages |
|
60
|
|
|
* @param $nameData |
|
61
|
|
|
*/ |
|
62
|
12 |
|
protected function initRuleErrorMessage($messages, $nameData) |
|
63
|
|
|
{ |
|
64
|
12 |
|
foreach ($messages as $message) { |
|
65
|
12 |
|
if (!isset($this->errorMessage[$nameData])) { |
|
66
|
12 |
|
$this->errorMessage[$nameData] = []; |
|
67
|
12 |
|
} |
|
68
|
12 |
|
array_push($this->errorMessage[$nameData], $message); |
|
69
|
12 |
|
} |
|
70
|
12 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param array $rules |
|
75
|
|
|
*/ |
|
76
|
12 |
|
public function initRule(array $rules) |
|
77
|
|
|
{ |
|
78
|
12 |
|
foreach ($rules as $nameData => $attributeRule) { |
|
79
|
12 |
|
$name = $nameData; |
|
80
|
12 |
|
if (preg_match('/\|?name\:(\w+[\s\w+]*)\|?/', $attributeRule, $match)) { |
|
81
|
3 |
|
$name = $match[1]; |
|
82
|
3 |
|
} |
|
83
|
12 |
|
$attributeRuleArr = explode("|", $attributeRule); |
|
84
|
|
|
|
|
85
|
12 |
|
foreach ($attributeRuleArr as $item) { |
|
86
|
12 |
|
if (!preg_match('/name.*/', $item)) { |
|
87
|
12 |
|
$this->rules[$nameData][] = $this->getObjectRule($item, $name); |
|
88
|
12 |
|
} |
|
89
|
12 |
|
} |
|
90
|
12 |
|
} |
|
91
|
12 |
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param $name |
|
95
|
|
|
* @return string |
|
96
|
|
|
* @throws \Exception |
|
97
|
|
|
*/ |
|
98
|
12 |
|
protected function getClassRule($name) |
|
99
|
|
|
{ |
|
100
|
12 |
|
$className = ucwords($name).'Rule'; |
|
101
|
|
|
|
|
102
|
12 |
|
foreach ($this->namespaceClassRule as $namespace) { |
|
103
|
12 |
|
if (class_exists($namespace.$className)) { |
|
104
|
12 |
|
return $namespace.$className; |
|
105
|
|
|
} |
|
106
|
12 |
|
} |
|
107
|
|
|
|
|
108
|
|
|
throw new \Exception("class {$className} not found"); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param $attributeRule |
|
113
|
|
|
* @param $nameData |
|
114
|
|
|
* @return mixed |
|
115
|
|
|
* @throws \Exception |
|
116
|
|
|
*/ |
|
117
|
12 |
|
protected function getObjectRule($attributeRule, $nameData) |
|
118
|
|
|
{ |
|
119
|
12 |
|
$rulesArr = explode(":", $attributeRule); |
|
120
|
12 |
|
$class = $this->getClassRule($rulesArr[0]); |
|
121
|
|
|
|
|
122
|
12 |
|
return new $class($attributeRule, $nameData); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|