|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Copyright (c) 2017 Salah Alkhwlani <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view |
|
7
|
|
|
* the LICENSE file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Yemenifree\Validation\Traits; |
|
11
|
|
|
|
|
12
|
|
|
use Rakit\Validation\Validation; |
|
13
|
|
|
use Rakit\Validation\Validator; |
|
14
|
|
|
use Yemenifree\Validation\TranslateLoader; |
|
15
|
|
|
|
|
16
|
|
|
trait HasValidator |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var array */ |
|
19
|
|
|
protected $aliases = []; |
|
20
|
|
|
/** @var string */ |
|
21
|
|
|
protected $validatorLocal = 'ar'; |
|
22
|
|
|
|
|
23
|
|
|
/** @var Validator */ |
|
24
|
|
|
protected $validator; |
|
25
|
|
|
/** @var TranslateLoader */ |
|
26
|
|
|
protected $translateLoader; |
|
27
|
|
|
|
|
28
|
|
|
/** @var Validation */ |
|
29
|
|
|
protected $validation; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* valid data. |
|
33
|
|
|
* |
|
34
|
|
|
* @param array $data |
|
35
|
|
|
* @param array $rules |
|
36
|
|
|
* @param array $messages |
|
37
|
|
|
* @param array $aliases |
|
38
|
|
|
* |
|
39
|
|
|
* @return mixed|\Rakit\Validation\Validation |
|
40
|
|
|
*/ |
|
41
|
|
|
protected function valid(array $data, array $rules, array $messages = [], array $aliases = []) |
|
42
|
|
|
{ |
|
43
|
|
|
// load translate. |
|
44
|
|
|
$this->loadTranslate(); |
|
45
|
|
|
|
|
46
|
|
|
$this->validation = $this->getValidator()->make($data, $rules, $messages); |
|
47
|
|
|
if (\count($aliases) > 0) { |
|
48
|
|
|
$this->setAliases($aliases); |
|
49
|
|
|
} |
|
50
|
|
|
$this->validation->setAliases($this->getAliases()); |
|
51
|
|
|
$this->validation->validate(); |
|
52
|
|
|
|
|
53
|
|
|
// if has custom invalid callback. |
|
54
|
|
|
if (\method_exists($this, 'InValidCallback') && $this->getValidation()->fails()) { |
|
55
|
|
|
return $this->InValidCallback($this->getValidation()->errors()->firstOfAll()); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return !$this->getValidation()->fails(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* load translate. |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function loadTranslate(): self |
|
65
|
|
|
{ |
|
66
|
|
|
$this->getValidator()->setMessages($this->getTranslateLoader()->load($this->getValidatorLocal())); |
|
67
|
|
|
|
|
68
|
|
|
return $this; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return Validator |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function getValidator(): Validator |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->validator ?: $this->validator = new Validator(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return TranslateLoader |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getTranslateLoader(): TranslateLoader |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->translateLoader ?: $this->translateLoader = new TranslateLoader(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return string |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getValidatorLocal(): string |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->validatorLocal; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* set local of validation errors message. |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $local |
|
99
|
|
|
* @param null|string $path |
|
100
|
|
|
* |
|
101
|
|
|
* @return HasValidator |
|
102
|
|
|
*/ |
|
103
|
|
|
public function setValidatorLocal($local, $path = null): self |
|
104
|
|
|
{ |
|
105
|
|
|
$this->validatorLocal = $local; |
|
106
|
|
|
|
|
107
|
|
|
if (!empty($path)) { |
|
108
|
|
|
$this->getTranslateLoader()->setPath($path); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $this; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return array |
|
116
|
|
|
*/ |
|
117
|
|
|
public function getAliases(): array |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->aliases; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param array $aliases |
|
124
|
|
|
* |
|
125
|
|
|
* @return self |
|
126
|
|
|
*/ |
|
127
|
|
|
public function setAliases($aliases): self |
|
128
|
|
|
{ |
|
129
|
|
|
$this->aliases = $aliases; |
|
130
|
|
|
|
|
131
|
|
|
return $this; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @return Validation |
|
136
|
|
|
*/ |
|
137
|
|
|
protected function getValidation(): Validation |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->validation; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Get validation errors. |
|
144
|
|
|
* |
|
145
|
|
|
* @param bool $firstOfAll |
|
146
|
|
|
* |
|
147
|
|
|
* @return array|\Rakit\Validation\ErrorBag |
|
148
|
|
|
*/ |
|
149
|
|
|
protected function getValidErrors($firstOfAll = false) |
|
150
|
|
|
{ |
|
151
|
|
|
return $firstOfAll ? $this->getValidation()->errors()->firstOfAll() : $this->getValidation()->errors(); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|