-
Notifications
You must be signed in to change notification settings - Fork 11.2k
/
Copy pathFormatsMessages.php
400 lines (342 loc) · 12.9 KB
/
FormatsMessages.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
<?php
namespace Illuminate\Validation\Concerns;
use Closure;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Symfony\Component\HttpFoundation\File\UploadedFile;
trait FormatsMessages
{
use ReplacesAttributes;
/**
* Get the validation message for an attribute and rule.
*
* @param string $attribute
* @param string $rule
* @return string
*/
protected function getMessage($attribute, $rule)
{
$inlineMessage = $this->getInlineMessage($attribute, $rule);
// First we will retrieve the custom message for the validation rule if one
// exists. If a custom validation message is being used we'll return the
// custom message, otherwise we'll keep searching for a valid message.
if (! is_null($inlineMessage)) {
return $inlineMessage;
}
$lowerRule = Str::snake($rule);
$customMessage = $this->getCustomMessageFromTranslator(
$customKey = "validation.custom.{$attribute}.{$lowerRule}"
);
// First we check for a custom defined validation message for the attribute
// and rule. This allows the developer to specify specific messages for
// only some attributes and rules that need to get specially formed.
if ($customMessage !== $customKey) {
return $customMessage;
}
// If the rule being validated is a "size" rule, we will need to gather the
// specific error message for the type of attribute being validated such
// as a number, file or string which all have different message types.
elseif (in_array($rule, $this->sizeRules)) {
return $this->getSizeMessage($attribute, $rule);
}
// Finally, if no developer specified messages have been set, and no other
// special messages apply for this rule, we will just pull the default
// messages out of the translator service for this validation rule.
$key = "validation.{$lowerRule}";
if ($key != ($value = $this->translator->get($key))) {
return $value;
}
return $this->getFromLocalArray(
$attribute, $lowerRule, $this->fallbackMessages
) ?: $key;
}
/**
* Get the proper inline error message for standard and size rules.
*
* @param string $attribute
* @param string $rule
* @return string|null
*/
protected function getInlineMessage($attribute, $rule)
{
$inlineEntry = $this->getFromLocalArray($attribute, Str::snake($rule));
return is_array($inlineEntry) && in_array($rule, $this->sizeRules)
? $inlineEntry[$this->getAttributeType($attribute)]
: $inlineEntry;
}
/**
* Get the inline message for a rule if it exists.
*
* @param string $attribute
* @param string $lowerRule
* @param array|null $source
* @return string|null
*/
protected function getFromLocalArray($attribute, $lowerRule, $source = null)
{
$source = $source ?: $this->customMessages;
$keys = ["{$attribute}.{$lowerRule}", $lowerRule];
// First we will check for a custom message for an attribute specific rule
// message for the fields, then we will check for a general custom line
// that is not attribute specific. If we find either we'll return it.
foreach ($keys as $key) {
foreach (array_keys($source) as $sourceKey) {
if (strpos($sourceKey, '*') !== false) {
$pattern = str_replace('\*', '([^.]*)', preg_quote($sourceKey, '#'));
if (preg_match('#^'.$pattern.'\z#u', $key) === 1) {
return $source[$sourceKey];
}
continue;
}
if (Str::is($sourceKey, $key)) {
return $source[$sourceKey];
}
}
}
}
/**
* Get the custom error message from the translator.
*
* @param string $key
* @return string
*/
protected function getCustomMessageFromTranslator($key)
{
if (($message = $this->translator->get($key)) !== $key) {
return $message;
}
// If an exact match was not found for the key, we will collapse all of these
// messages and loop through them and try to find a wildcard match for the
// given key. Otherwise, we will simply return the key's value back out.
$shortKey = preg_replace(
'/^validation\.custom\./', '', $key
);
return $this->getWildcardCustomMessages(Arr::dot(
(array) $this->translator->get('validation.custom')
), $shortKey, $key);
}
/**
* Check the given messages for a wildcard key.
*
* @param array $messages
* @param string $search
* @param string $default
* @return string
*/
protected function getWildcardCustomMessages($messages, $search, $default)
{
foreach ($messages as $key => $message) {
if ($search === $key || (Str::contains($key, ['*']) && Str::is($key, $search))) {
return $message;
}
}
return $default;
}
/**
* Get the proper error message for an attribute and size rule.
*
* @param string $attribute
* @param string $rule
* @return string
*/
protected function getSizeMessage($attribute, $rule)
{
$lowerRule = Str::snake($rule);
// There are three different types of size validations. The attribute may be
// either a number, file, or string so we will check a few things to know
// which type of value it is and return the correct line for that type.
$type = $this->getAttributeType($attribute);
$key = "validation.{$lowerRule}.{$type}";
return $this->translator->get($key);
}
/**
* Get the data type of the given attribute.
*
* @param string $attribute
* @return string
*/
protected function getAttributeType($attribute)
{
// We assume that the attributes present in the file array are files so that
// means that if the attribute does not have a numeric rule and the files
// list doesn't have it we'll just consider it a string by elimination.
if ($this->hasRule($attribute, $this->numericRules)) {
return 'numeric';
} elseif ($this->hasRule($attribute, ['Array'])) {
return 'array';
} elseif ($this->getValue($attribute) instanceof UploadedFile) {
return 'file';
}
return 'string';
}
/**
* Replace all error message place-holders with actual values.
*
* @param string $message
* @param string $attribute
* @param string $rule
* @param array $parameters
* @return string
*/
public function makeReplacements($message, $attribute, $rule, $parameters)
{
$message = $this->replaceAttributePlaceholder(
$message, $this->getDisplayableAttribute($attribute)
);
$message = $this->replaceInputPlaceholder($message, $attribute);
if (isset($this->replacers[Str::snake($rule)])) {
return $this->callReplacer($message, $attribute, Str::snake($rule), $parameters, $this);
} elseif (method_exists($this, $replacer = "replace{$rule}")) {
return $this->$replacer($message, $attribute, $rule, $parameters);
}
return $message;
}
/**
* Get the displayable name of the attribute.
*
* @param string $attribute
* @return string
*/
public function getDisplayableAttribute($attribute)
{
$primaryAttribute = $this->getPrimaryAttribute($attribute);
$expectedAttributes = $attribute != $primaryAttribute
? [$attribute, $primaryAttribute] : [$attribute];
foreach ($expectedAttributes as $name) {
// The developer may dynamically specify the array of custom attributes on this
// validator instance. If the attribute exists in this array it is used over
// the other ways of pulling the attribute name for this given attributes.
if (isset($this->customAttributes[$name])) {
return $this->customAttributes[$name];
}
// We allow for a developer to specify language lines for any attribute in this
// application, which allows flexibility for displaying a unique displayable
// version of the attribute name instead of the name used in an HTTP POST.
if ($line = $this->getAttributeFromTranslations($name)) {
return $line;
}
}
// When no language line has been specified for the attribute and it is also
// an implicit attribute we will display the raw attribute's name and not
// modify it with any of these replacements before we display the name.
if (isset($this->implicitAttributes[$primaryAttribute])) {
return ($formatter = $this->implicitAttributesFormatter)
? $formatter($attribute)
: $attribute;
}
return str_replace('_', ' ', Str::snake($attribute));
}
/**
* Get the given attribute from the attribute translations.
*
* @param string $name
* @return string
*/
protected function getAttributeFromTranslations($name)
{
return Arr::get($this->translator->get('validation.attributes'), $name);
}
/**
* Replace the :attribute placeholder in the given message.
*
* @param string $message
* @param string $value
* @return string
*/
protected function replaceAttributePlaceholder($message, $value)
{
return str_replace(
[':attribute', ':ATTRIBUTE', ':Attribute'],
[$value, Str::upper($value), Str::ucfirst($value)],
$message
);
}
/**
* Replace the :input placeholder in the given message.
*
* @param string $message
* @param string $attribute
* @return string
*/
protected function replaceInputPlaceholder($message, $attribute)
{
$actualValue = $this->getValue($attribute);
if (is_scalar($actualValue) || is_null($actualValue)) {
$message = str_replace(':input', $this->getDisplayableValue($attribute, $actualValue), $message);
}
return $message;
}
/**
* Get the displayable name of the value.
*
* @param string $attribute
* @param mixed $value
* @return string
*/
public function getDisplayableValue($attribute, $value)
{
if (isset($this->customValues[$attribute][$value])) {
return $this->customValues[$attribute][$value];
}
$key = "validation.values.{$attribute}.{$value}";
if (($line = $this->translator->get($key)) !== $key) {
return $line;
}
if (is_bool($value)) {
return $value ? 'true' : 'false';
}
return $value;
}
/**
* Transform an array of attributes to their displayable form.
*
* @param array $values
* @return array
*/
protected function getAttributeList(array $values)
{
$attributes = [];
// For each attribute in the list we will simply get its displayable form as
// this is convenient when replacing lists of parameters like some of the
// replacement functions do when formatting out the validation message.
foreach ($values as $key => $value) {
$attributes[$key] = $this->getDisplayableAttribute($value);
}
return $attributes;
}
/**
* Call a custom validator message replacer.
*
* @param string $message
* @param string $attribute
* @param string $rule
* @param array $parameters
* @param \Illuminate\Validation\Validator $validator
* @return string|null
*/
protected function callReplacer($message, $attribute, $rule, $parameters, $validator)
{
$callback = $this->replacers[$rule];
if ($callback instanceof Closure) {
return $callback(...func_get_args());
} elseif (is_string($callback)) {
return $this->callClassBasedReplacer($callback, $message, $attribute, $rule, $parameters, $validator);
}
}
/**
* Call a class based validator message replacer.
*
* @param string $callback
* @param string $message
* @param string $attribute
* @param string $rule
* @param array $parameters
* @param \Illuminate\Validation\Validator $validator
* @return string
*/
protected function callClassBasedReplacer($callback, $message, $attribute, $rule, $parameters, $validator)
{
[$class, $method] = Str::parseCallback($callback, 'replace');
return $this->container->make($class)->{$method}(...array_slice(func_get_args(), 1));
}
}