1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace IanOlson\Support\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use Illuminate\Support\Facades\Lang; |
7
|
|
|
use Illuminate\Support\Facades\Validator; |
8
|
|
|
use IanOlson\Support\Exceptions\ValidateException; |
9
|
|
|
|
10
|
|
|
trait ValidateTrait |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Validation types. |
14
|
|
|
* |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
protected $validationTypes = ['rules', 'messages']; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Custom validation options for messages and rules used for validation. |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $validationOptions = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Check validation types. |
28
|
|
|
* |
29
|
|
|
* @param string $type |
30
|
|
|
* |
31
|
|
|
* @throws ValidateException |
32
|
|
|
*/ |
33
|
28 |
|
protected function checkValidationTypes($type) |
34
|
|
|
{ |
35
|
28 |
|
if (!in_array($type, $this->validationTypes)) { |
36
|
6 |
|
throw new ValidateException(Lang::get('support.exceptions.validate.type')); |
37
|
|
|
} |
38
|
22 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Add validation option. |
42
|
|
|
* |
43
|
|
|
* @param string $type |
44
|
|
|
* @param string $key |
45
|
|
|
* @param string $value |
46
|
|
|
* |
47
|
|
|
* @throws ValidateException |
48
|
|
|
* |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
20 |
|
protected function addValidationOption($type, $key, $value) |
52
|
|
|
{ |
53
|
20 |
|
$this->checkValidationTypes($type); |
54
|
|
|
|
55
|
18 |
|
$this->validationOptions = Arr::add($this->validationOptions, "{$type}.{$key}", $value); |
56
|
|
|
|
57
|
18 |
|
return $this->validationOptions; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Remove validation option. |
62
|
|
|
* |
63
|
|
|
* @param string $type |
64
|
|
|
* @param string $key |
65
|
|
|
* |
66
|
|
|
* @throws ValidateException |
67
|
|
|
* |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
6 |
|
protected function removeValidationOption($type, $key) |
71
|
|
|
{ |
72
|
6 |
|
$this->checkValidationTypes($type); |
73
|
|
|
|
74
|
4 |
|
Arr::forget($this->validationOptions, "{$type}.{$key}"); |
75
|
|
|
|
76
|
4 |
|
return $this->validationOptions; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get validation options by type. |
81
|
|
|
* |
82
|
|
|
* @param string $type |
83
|
|
|
* |
84
|
|
|
* @throws ValidateException |
85
|
|
|
* |
86
|
|
|
* @return mixed |
87
|
|
|
*/ |
88
|
24 |
|
protected function getValidationOptions($type) |
89
|
|
|
{ |
90
|
24 |
|
$this->checkValidationTypes($type); |
91
|
|
|
|
92
|
22 |
|
return Arr::get($this->validationOptions, $type, []); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Validate the form data. |
97
|
|
|
* |
98
|
|
|
* @param array $data An array of data to validate against the rules. |
99
|
|
|
* @param null|string $message Message to be thrown with the custom exception. |
100
|
|
|
* |
101
|
|
|
* @throws ValidateException |
102
|
|
|
* |
103
|
|
|
* @return boolean |
104
|
|
|
*/ |
105
|
14 |
|
protected function validate(array $data = [], $message = null) |
106
|
|
|
{ |
107
|
14 |
|
$validator = Validator::make( |
108
|
7 |
|
$data, |
109
|
14 |
|
$this->getValidationOptions('rules'), |
110
|
14 |
|
$this->getValidationOptions('messages') |
111
|
7 |
|
); |
112
|
|
|
|
113
|
14 |
|
if ($validator->fails()) { |
114
|
8 |
|
if (is_null($message)) { |
115
|
6 |
|
$message = Lang::get('support.exceptions.validate.fail'); |
116
|
3 |
|
} |
117
|
|
|
|
118
|
8 |
|
throw new ValidateException($message, $validator); |
119
|
|
|
} |
120
|
|
|
|
121
|
6 |
|
return true; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|