| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentValidator; |
| 4 |
|
| 5 |
trait MessageBag |
| 6 |
{ |
| 7 |
/** |
| 8 |
* The default message bag. |
| 9 |
* |
| 10 |
* @var array |
| 11 |
*/ |
| 12 |
protected $bag = [ |
| 13 |
'email' => 'The :attribute must be a valid email address.', |
| 14 |
'max' => [ |
| 15 |
'numeric' => 'The :attribute may not be greater than :max.', |
| 16 |
'file' => 'The :attribute may not be greater than :max kilobytes.', |
| 17 |
'string' => 'The :attribute may not be greater than :max characters.', |
| 18 |
'array' => 'The :attribute may not have more than :max items.', |
| 19 |
], |
| 20 |
'mimes' => 'The :attribute must be a file of type: :values.', |
| 21 |
'mimetypes' => 'The :attribute must be a file of type: :values.', |
| 22 |
'min' => [ |
| 23 |
'numeric' => 'The :attribute must be at least :min.', |
| 24 |
'file' => 'The :attribute must be at least :min kilobytes.', |
| 25 |
'string' => 'The :attribute must be at least :min characters.', |
| 26 |
'array' => 'The :attribute must have at least :min items.', |
| 27 |
], |
| 28 |
'numeric' => 'The :attribute must be a number.', |
| 29 |
'required' => 'The :attribute field is required.', |
| 30 |
'required_if' => 'The :attribute field is required when :other is :value.', |
| 31 |
'same' => 'The :attribute and :other must match.', |
| 32 |
'size' => [ |
| 33 |
'numeric' => 'The :attribute must be :size.', |
| 34 |
'file' => 'The :attribute must be :size kilobytes.', |
| 35 |
'string' => 'The :attribute must be :size characters.', |
| 36 |
'array' => 'The :attribute must contain :size items.', |
| 37 |
], |
| 38 |
'url' => 'The :attribute format is invalid.', |
| 39 |
'digits' => 'The :attribute must be :digits characters.' |
| 40 |
]; |
| 41 |
|
| 42 |
/** |
| 43 |
* Generate a validation error message. |
| 44 |
* |
| 45 |
* @param $attribute |
| 46 |
* @param $rule |
| 47 |
* @param $parameters |
| 48 |
* |
| 49 |
* @return mixed |
| 50 |
*/ |
| 51 |
protected function generate($attribute, $rule, $parameters) |
| 52 |
{ |
| 53 |
$method = 'replace'.str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $rule))); |
| 54 |
|
| 55 |
if ($this->hasMethod($method)) { |
| 56 |
return $this->$method($attribute, $parameters); |
| 57 |
} |
| 58 |
|
| 59 |
return ''; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
/** |
| 64 |
* Get the replacement text of the error message. |
| 65 |
* |
| 66 |
* @param $customMessagesKey |
| 67 |
* @param $bagAccessor |
| 68 |
* |
| 69 |
* @return string |
| 70 |
*/ |
| 71 |
protected function getReplacementText($customMessagesKey, $bagAccessor) |
| 72 |
{ |
| 73 |
return isset($this->customMessages[$customMessagesKey]) |
| 74 |
? $this->customMessages[$customMessagesKey] |
| 75 |
: Arr::get($this->bag, $bagAccessor, ''); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Make bag accessor key. |
| 80 |
* |
| 81 |
* @param $attribute |
| 82 |
* @param $rule |
| 83 |
* |
| 84 |
* @return string |
| 85 |
*/ |
| 86 |
protected function makeBagKey($attribute, $rule) |
| 87 |
{ |
| 88 |
$type = $this->deduceType($this->getValue($attribute)); |
| 89 |
|
| 90 |
return $rule.'.'.$type; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Replace all place-holders for the required rule. |
| 95 |
* |
| 96 |
* @param $attribute |
| 97 |
* @param $parameters |
| 98 |
* |
| 99 |
* @return string |
| 100 |
*/ |
| 101 |
protected function replaceRequired($attribute, $parameters) |
| 102 |
{ |
| 103 |
$text = $this->getReplacementText($attribute.'.required', 'required'); |
| 104 |
|
| 105 |
return str_replace(':attribute', $attribute, $text); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Replace all place-holders for the required if rule. |
| 110 |
* |
| 111 |
* @param $attribute |
| 112 |
* @param $parameters |
| 113 |
* |
| 114 |
* @return string |
| 115 |
*/ |
| 116 |
protected function replaceRequiredIf($attribute, $parameters) |
| 117 |
{ |
| 118 |
$text = $this->getReplacementText($attribute.'.required_if', 'required_if'); |
| 119 |
|
| 120 |
return str_replace([':attribute', ':other', ':value'], [$attribute, $parameters[0], $parameters[1]], $text); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Replace all place-holders for the email rule. |
| 125 |
* |
| 126 |
* @param $attribute |
| 127 |
* @param $parameters |
| 128 |
* |
| 129 |
* @return string |
| 130 |
*/ |
| 131 |
protected function replaceEmail($attribute, $parameters) |
| 132 |
{ |
| 133 |
$text = $this->getReplacementText($attribute.'.email', 'email'); |
| 134 |
|
| 135 |
return str_replace(':attribute', $attribute, $text); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Replace all place-holders for the size rule. |
| 140 |
* |
| 141 |
* @param $attribute |
| 142 |
* @param $parameters |
| 143 |
* |
| 144 |
* @return string |
| 145 |
*/ |
| 146 |
protected function replaceSize($attribute, $parameters) |
| 147 |
{ |
| 148 |
$text = $this->getReplacementText($attribute.'.size', $this->makeBagKey($attribute, 'size')); |
| 149 |
|
| 150 |
return str_replace([':attribute', ':size'], [$attribute, $parameters[0]], $text); |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Replace all place-holders for the min rule. |
| 155 |
* |
| 156 |
* @param $attribute |
| 157 |
* @param $parameters |
| 158 |
* |
| 159 |
* @return string |
| 160 |
*/ |
| 161 |
protected function replaceMin($attribute, $parameters) |
| 162 |
{ |
| 163 |
$text = $this->getReplacementText($attribute.'.min', $this->makeBagKey($attribute, 'min')); |
| 164 |
|
| 165 |
return str_replace([':attribute', ':min'], [$attribute, $parameters[0]], $text); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Replace all place-holders for the max rule. |
| 170 |
* |
| 171 |
* @param $attribute |
| 172 |
* @param $parameters |
| 173 |
* |
| 174 |
* @return string |
| 175 |
*/ |
| 176 |
protected function replaceMax($attribute, $parameters) |
| 177 |
{ |
| 178 |
$text = $this->getReplacementText($attribute.'.max', $this->makeBagKey($attribute, 'max')); |
| 179 |
|
| 180 |
return str_replace([':attribute', ':max'], [$attribute, $parameters[0]], $text); |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Replace all place-holders for the min rule. |
| 185 |
* |
| 186 |
* @param $attribute |
| 187 |
* @param $parameters |
| 188 |
* |
| 189 |
* @return string |
| 190 |
*/ |
| 191 |
protected function replaceSame($attribute, $parameters) |
| 192 |
{ |
| 193 |
$text = $this->getReplacementText($attribute.'.same', 'same'); |
| 194 |
|
| 195 |
return str_replace([':attribute', ':other'], [$attribute, $parameters[0]], $text); |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Replace all place-holders for the url rule. |
| 200 |
* |
| 201 |
* @param $attribute |
| 202 |
* @param $parameters |
| 203 |
* |
| 204 |
* @return string |
| 205 |
*/ |
| 206 |
protected function replaceUrl($attribute, $parameters) |
| 207 |
{ |
| 208 |
$text = $this->getReplacementText($attribute.'.url', 'url'); |
| 209 |
|
| 210 |
return str_replace(':attribute', $attribute, $text); |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Replace all place-holders for the numeric rule. |
| 215 |
* |
| 216 |
* @param $attribute |
| 217 |
* @param $parameters |
| 218 |
* |
| 219 |
* @return string |
| 220 |
*/ |
| 221 |
protected function replaceNumeric($attribute, $parameters) |
| 222 |
{ |
| 223 |
$text = $this->getReplacementText($attribute.'.numeric', 'numeric'); |
| 224 |
|
| 225 |
return str_replace(':attribute', $attribute, $text); |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Replace all place-holders for the mimes rule. |
| 230 |
* |
| 231 |
* @param $attribute |
| 232 |
* @param $parameters |
| 233 |
* |
| 234 |
* @return string |
| 235 |
*/ |
| 236 |
protected function replaceMimes($attribute, $parameters) |
| 237 |
{ |
| 238 |
$text = $this->getReplacementText($attribute.'.mimes', 'mimes'); |
| 239 |
|
| 240 |
return str_replace([':attribute', ':values'], [$attribute, implode(', ', $parameters)], $text); |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Replace all place-holders for the digits rule. |
| 245 |
* |
| 246 |
* @param $attribute |
| 247 |
* @param $parameters |
| 248 |
* |
| 249 |
* @return string |
| 250 |
*/ |
| 251 |
protected function replaceDigits($attribute, $parameters) |
| 252 |
{ |
| 253 |
$text = $this->getReplacementText($attribute.'.digits', 'digits'); |
| 254 |
|
| 255 |
return str_replace([':attribute', ':digits'], [$attribute, $parameters[0]], $text); |
| 256 |
} |
| 257 |
} |