1
|
|
|
<?php |
2
|
|
|
namespace NwLaravel\Validation; |
3
|
|
|
|
4
|
|
|
use Prettus\Validator\LaravelValidator; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class BaseValidator |
8
|
|
|
* @abstract |
9
|
|
|
*/ |
10
|
|
|
abstract class BaseValidator extends LaravelValidator |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $keyName; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Get Validator |
19
|
|
|
* |
20
|
|
|
* @return \Illuminate\Validation\Factory |
21
|
|
|
*/ |
22
|
1 |
|
public function getValidator() |
23
|
|
|
{ |
24
|
1 |
|
return $this->validator; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Set Key Name |
29
|
|
|
* |
30
|
|
|
* @param Key $keyName Key Name |
31
|
|
|
* |
32
|
|
|
* @return $this |
33
|
|
|
*/ |
34
|
1 |
|
public function setKeyName($keyName) |
35
|
|
|
{ |
36
|
1 |
|
$this->keyName = $keyName; |
|
|
|
|
37
|
1 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get rule for validation by action ValidatorInterface::RULE_CREATE or ValidatorInterface::RULE_UPDATE |
41
|
|
|
* |
42
|
|
|
* Default rule: ValidatorInterface::RULE_CREATE |
43
|
|
|
* |
44
|
|
|
* @param null $action |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
1 |
|
public function getRules($action = null) |
48
|
|
|
{ |
49
|
1 |
|
$rules = []; |
50
|
|
|
|
51
|
1 |
|
if (isset($this->rules[$action])) { |
52
|
1 |
|
$rules = $this->rules[$action]; |
53
|
1 |
|
} |
54
|
|
|
|
55
|
1 |
|
return $this->parserValidationRules($rules, $this->id); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Parser Validation Rules |
60
|
|
|
* |
61
|
|
|
* @param Unknown $rules Rules |
62
|
|
|
* @param null $id Null Id |
63
|
|
|
* |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
1 |
|
protected function parserValidationRules($rules, $id = null) |
67
|
|
|
{ |
68
|
|
|
|
69
|
1 |
|
if ($id === null) { |
70
|
1 |
|
return $rules; |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
array_walk($rules, function (&$rules, $field) use ($id) { |
74
|
1 |
|
if (!is_array($rules)) { |
75
|
1 |
|
$rules = explode("|", $rules); |
76
|
1 |
|
} |
77
|
|
|
|
78
|
1 |
|
foreach ($rules as $ruleIdx => $rule) { |
79
|
|
|
// get name and parameters |
80
|
1 |
|
@list($name, $params) = array_pad(explode(":", $rule), 2, null); |
|
|
|
|
81
|
|
|
|
82
|
|
|
// only do someting for the unique rule |
83
|
1 |
|
if (strtolower($name) != "unique") { |
84
|
1 |
|
continue; // continue in foreach loop, nothing left to do here |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
$p = array_map("trim", explode(",", $params)); |
88
|
|
|
|
89
|
|
|
// set field name to rules key ($field) (laravel convention) |
90
|
1 |
|
if (!isset($p[1])) { |
91
|
1 |
|
$p[1] = $field; |
92
|
1 |
|
} |
93
|
|
|
|
94
|
|
|
// set 3rd parameter to id given to getValidationRules() |
95
|
1 |
|
$p[2] = $id; |
96
|
1 |
|
if ($this->keyName) { |
97
|
1 |
|
$p[3] = $this->keyName; |
98
|
1 |
|
} |
99
|
|
|
|
100
|
1 |
|
$params = implode(",", $p); |
101
|
1 |
|
$rules[$ruleIdx] = $name.":".$params; |
102
|
1 |
|
} |
103
|
1 |
|
}); |
104
|
|
|
|
105
|
1 |
|
return $rules; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..