1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CodeZero\RouteKeyExists; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Validation\Rule; |
6
|
|
|
|
7
|
|
|
class RouteKeyExists implements Rule |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The model to validate. |
11
|
|
|
* |
12
|
|
|
* @var \Illuminate\Database\Eloquent\Model |
13
|
|
|
*/ |
14
|
|
|
protected $model; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The attribute being validated. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $attribute; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Replace the route key in the request |
25
|
|
|
* with the actual database value? |
26
|
|
|
* |
27
|
|
|
* @var string|bool |
28
|
|
|
*/ |
29
|
|
|
protected $replaceAttribute = false; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Attribute with the actual database value |
33
|
|
|
* to add to the request. |
34
|
|
|
* |
35
|
|
|
* @var string|bool |
36
|
|
|
*/ |
37
|
|
|
protected $addAttribute = false; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Create a new rule instance. |
41
|
|
|
* |
42
|
|
|
* @param string $model |
43
|
|
|
* |
44
|
|
|
* @return static |
45
|
|
|
*/ |
46
|
4 |
|
public static function model($model) |
47
|
|
|
{ |
48
|
4 |
|
return new static($model); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Create a new rule instance. |
53
|
|
|
* |
54
|
|
|
* @param string $model |
55
|
|
|
*/ |
56
|
4 |
|
public function __construct($model) |
57
|
|
|
{ |
58
|
4 |
|
$this->model = new $model; |
59
|
4 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Replace the route key in the request with the actual database value. |
63
|
|
|
* If an attribute name is provided, the original request attribute |
64
|
|
|
* name will be removed and replaced with the new one. |
65
|
|
|
* |
66
|
|
|
* @param string|null $attribute |
67
|
|
|
* |
68
|
|
|
* @return $this |
69
|
|
|
*/ |
70
|
2 |
|
public function replace($attribute = null) |
71
|
|
|
{ |
72
|
2 |
|
$this->replaceAttribute = $attribute ?: true; |
73
|
|
|
|
74
|
2 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Add the given attribute with the actual |
79
|
|
|
* database value to the request. |
80
|
|
|
* |
81
|
|
|
* @param string $attribute |
82
|
|
|
* |
83
|
|
|
* @return $this |
84
|
|
|
*/ |
85
|
1 |
|
public function add($attribute) |
86
|
|
|
{ |
87
|
1 |
|
$this->addAttribute = $attribute; |
88
|
|
|
|
89
|
1 |
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Determine if the validation rule passes. |
94
|
|
|
* |
95
|
|
|
* @param string $attribute |
96
|
|
|
* @param string $value |
97
|
|
|
* |
98
|
|
|
* @return bool |
99
|
|
|
*/ |
100
|
4 |
|
public function passes($attribute, $value) |
101
|
|
|
{ |
102
|
4 |
|
$this->attribute = $attribute; |
103
|
|
|
|
104
|
4 |
|
if ( ! $model = $this->model->resolveRouteBinding($value)) { |
105
|
1 |
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
4 |
|
$this->updateRequest($model); |
109
|
|
|
|
110
|
4 |
|
return true; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get the validation error message. |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
1 |
|
public function message() |
119
|
|
|
{ |
120
|
1 |
|
return trans('validation.exists', [ |
121
|
1 |
|
'attribute' => $this->attribute, |
122
|
|
|
]); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Update the request attributes if needed. |
127
|
|
|
* |
128
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
129
|
|
|
* |
130
|
|
|
* @return void |
131
|
|
|
*/ |
132
|
4 |
|
protected function updateRequest($model) |
133
|
|
|
{ |
134
|
4 |
|
$actualKey = $model->{$model->getRouteKeyName()}; |
135
|
|
|
|
136
|
4 |
|
if ($this->replaceAttribute === true) { |
137
|
1 |
|
$this->mergeRequest($this->attribute, $actualKey); |
138
|
|
|
} |
139
|
|
|
|
140
|
4 |
|
if (is_string($this->replaceAttribute)) { |
141
|
1 |
|
$this->mergeRequest($this->replaceAttribute, $actualKey); |
142
|
1 |
|
$this->removeFromRequest($this->attribute); |
143
|
|
|
} |
144
|
|
|
|
145
|
4 |
|
if (is_string($this->addAttribute)) { |
146
|
1 |
|
$this->mergeRequest($this->addAttribute, $actualKey); |
147
|
|
|
} |
148
|
4 |
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Merge the request attributes with the given key / value pair. |
152
|
|
|
* |
153
|
|
|
* @param string $key |
154
|
|
|
* @param mixed $value |
155
|
|
|
* |
156
|
|
|
* @return void |
157
|
|
|
*/ |
158
|
3 |
|
protected function mergeRequest($key, $value) |
159
|
|
|
{ |
160
|
3 |
|
request()->merge([ |
161
|
3 |
|
$key => $value, |
162
|
|
|
]); |
163
|
3 |
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Remove an attribute from the request. |
167
|
|
|
* |
168
|
|
|
* @param string $attribute |
169
|
|
|
* |
170
|
|
|
* @return void |
171
|
|
|
*/ |
172
|
1 |
|
protected function removeFromRequest($attribute) |
173
|
|
|
{ |
174
|
1 |
|
$attributes = request()->all(); |
175
|
|
|
|
176
|
1 |
|
if (isset($attributes[$attribute])) { |
177
|
1 |
|
unset($attributes[$attribute]); |
178
|
|
|
} |
179
|
|
|
|
180
|
1 |
|
request()->replace($attributes); |
181
|
1 |
|
} |
182
|
|
|
} |
183
|
|
|
|