1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Milkmeowo\Framework\Base\Validators; |
4
|
|
|
|
5
|
|
|
use Prettus\Validator\AbstractValidator; |
6
|
|
|
|
7
|
|
|
class Validator extends AbstractValidator |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Validation Custom Messages. |
11
|
|
|
* |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
protected $messages = []; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Validation Custom Attributes. |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $attributes = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Get Custom error messages for validation. |
25
|
|
|
* |
26
|
|
|
* @return array |
27
|
|
|
*/ |
28
|
|
|
public function getMessages() |
29
|
|
|
{ |
30
|
|
|
return $this->messages; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Set Custom error messages for Validation. |
35
|
|
|
* @param array $messages |
36
|
|
|
* |
37
|
|
|
* @return $this |
38
|
|
|
*/ |
39
|
|
|
public function setMessages(array $messages) |
40
|
|
|
{ |
41
|
|
|
$this->messages = $messages; |
42
|
|
|
|
43
|
|
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get Custom error attributes for validation. |
48
|
|
|
* |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
|
|
public function getAttributes() |
52
|
|
|
{ |
53
|
|
|
return $this->attributes; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Set Custom error attributes for Validation. |
58
|
|
|
* |
59
|
|
|
* @param array $attributes |
60
|
|
|
* |
61
|
|
|
* @return $this |
62
|
|
|
*/ |
63
|
|
|
public function setAttributes(array $attributes) |
64
|
|
|
{ |
65
|
|
|
$this->attributes = $attributes; |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Pass the data and the rules to the validator. |
72
|
|
|
* |
73
|
|
|
* @param string $action |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
|
|
public function passes($action = null) |
77
|
|
|
{ |
78
|
|
|
$rules = $this->getRules($action); |
|
|
|
|
79
|
|
|
$messages = $this->getMessages(); |
80
|
|
|
$attributes = $this->getAttributes(); |
81
|
|
|
$validator = app('validator')->make($this->data, $rules, $messages, $attributes); |
82
|
|
|
if ($validator->fails()) { |
83
|
|
|
$this->errors = $validator->messages(); |
84
|
|
|
|
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return true; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.