PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 3.6.40
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v3.6.40
6.2.14 6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 All 196 releases
fluentform / app / Services / fluentvalidator / src / MessageBag.php

MessageBag.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 3.6.40, at app/Services/fluentvalidator/src/MessageBag.php

241 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 ];
40
41 /**
42 * Generate a validation error message.
43 *
44 * @param $attribute
45 * @param $rule
46 * @param $parameters
47 *
48 * @return mixed
49 */
50 protected function generate($attribute, $rule, $parameters)
51 {
52 $method = 'replace'.str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $rule)));
53
54 if ($this->hasMethod($method)) {
55 return $this->$method($attribute, $parameters);
56 }
57
58 return '';
59 }
60
61
62 /**
63 * Get the replacement text of the error message.
64 *
65 * @param $customMessagesKey
66 * @param $bagAccessor
67 *
68 * @return string
69 */
70 protected function getReplacementText($customMessagesKey, $bagAccessor)
71 {
72 return isset($this->customMessages[$customMessagesKey])
73 ? $this->customMessages[$customMessagesKey]
74 : Arr::get($this->bag, $bagAccessor, '');
75 }
76
77 /**
78 * Make bag accessor key.
79 *
80 * @param $attribute
81 * @param $rule
82 *
83 * @return string
84 */
85 protected function makeBagKey($attribute, $rule)
86 {
87 $type = $this->deduceType($this->getValue($attribute));
88
89 return $rule.'.'.$type;
90 }
91
92 /**
93 * Replace all place-holders for the required rule.
94 *
95 * @param $attribute
96 * @param $parameters
97 *
98 * @return string
99 */
100 protected function replaceRequired($attribute, $parameters)
101 {
102 $text = $this->getReplacementText($attribute.'.required', 'required');
103
104 return str_replace(':attribute', $attribute, $text);
105 }
106
107 /**
108 * Replace all place-holders for the required if rule.
109 *
110 * @param $attribute
111 * @param $parameters
112 *
113 * @return string
114 */
115 protected function replaceRequiredIf($attribute, $parameters)
116 {
117 $text = $this->getReplacementText($attribute.'.required_if', 'required_if');
118
119 return str_replace([':attribute', ':other', ':value'], [$attribute, $parameters[0], $parameters[1]], $text);
120 }
121
122 /**
123 * Replace all place-holders for the email rule.
124 *
125 * @param $attribute
126 * @param $parameters
127 *
128 * @return string
129 */
130 protected function replaceEmail($attribute, $parameters)
131 {
132 $text = $this->getReplacementText($attribute.'.email', 'email');
133
134 return str_replace(':attribute', $attribute, $text);
135 }
136
137 /**
138 * Replace all place-holders for the size rule.
139 *
140 * @param $attribute
141 * @param $parameters
142 *
143 * @return string
144 */
145 protected function replaceSize($attribute, $parameters)
146 {
147 $text = $this->getReplacementText($attribute.'.size', $this->makeBagKey($attribute, 'size'));
148
149 return str_replace([':attribute', ':size'], [$attribute, $parameters[0]], $text);
150 }
151
152 /**
153 * Replace all place-holders for the min rule.
154 *
155 * @param $attribute
156 * @param $parameters
157 *
158 * @return string
159 */
160 protected function replaceMin($attribute, $parameters)
161 {
162 $text = $this->getReplacementText($attribute.'.min', $this->makeBagKey($attribute, 'min'));
163
164 return str_replace([':attribute', ':min'], [$attribute, $parameters[0]], $text);
165 }
166
167 /**
168 * Replace all place-holders for the max rule.
169 *
170 * @param $attribute
171 * @param $parameters
172 *
173 * @return string
174 */
175 protected function replaceMax($attribute, $parameters)
176 {
177 $text = $this->getReplacementText($attribute.'.max', $this->makeBagKey($attribute, 'max'));
178
179 return str_replace([':attribute', ':max'], [$attribute, $parameters[0]], $text);
180 }
181
182 /**
183 * Replace all place-holders for the min rule.
184 *
185 * @param $attribute
186 * @param $parameters
187 *
188 * @return string
189 */
190 protected function replaceSame($attribute, $parameters)
191 {
192 $text = $this->getReplacementText($attribute.'.same', 'same');
193
194 return str_replace([':attribute', ':other'], [$attribute, $parameters[0]], $text);
195 }
196
197 /**
198 * Replace all place-holders for the url rule.
199 *
200 * @param $attribute
201 * @param $parameters
202 *
203 * @return string
204 */
205 protected function replaceUrl($attribute, $parameters)
206 {
207 $text = $this->getReplacementText($attribute.'.url', 'url');
208
209 return str_replace(':attribute', $attribute, $text);
210 }
211
212 /**
213 * Replace all place-holders for the numeric rule.
214 *
215 * @param $attribute
216 * @param $parameters
217 *
218 * @return string
219 */
220 protected function replaceNumeric($attribute, $parameters)
221 {
222 $text = $this->getReplacementText($attribute.'.numeric', 'numeric');
223
224 return str_replace(':attribute', $attribute, $text);
225 }
226
227 /**
228 * Replace all place-holders for the mimes rule.
229 *
230 * @param $attribute
231 * @param $parameters
232 *
233 * @return string
234 */
235 protected function replaceMimes($attribute, $parameters)
236 {
237 $text = $this->getReplacementText($attribute.'.mimes', 'mimes');
238
239 return str_replace([':attribute', ':values'], [$attribute, implode(', ', $parameters)], $text);
240 }
241 }