1 | <?php |
||
2 | |||
3 | namespace SilverStripe\ORM; |
||
4 | |||
5 | use InvalidArgumentException; |
||
6 | use Serializable; |
||
7 | use SilverStripe\Core\Injector\Injectable; |
||
8 | use SilverStripe\Dev\Deprecation; |
||
9 | |||
10 | /** |
||
11 | * A class that combined as a boolean result with an optional list of error messages. |
||
12 | * This is used for returning validation results from validators |
||
13 | * |
||
14 | * Each message can have a code or field which will uniquely identify that message. However, |
||
15 | * messages can be stored without a field or message as an "overall" message. |
||
16 | */ |
||
17 | class ValidationResult implements Serializable |
||
18 | { |
||
19 | use Injectable; |
||
20 | |||
21 | /** |
||
22 | * Standard "error" type |
||
23 | */ |
||
24 | const TYPE_ERROR = 'error'; |
||
25 | |||
26 | /** |
||
27 | * Standard "good" message type |
||
28 | */ |
||
29 | const TYPE_GOOD = 'good'; |
||
30 | |||
31 | /** |
||
32 | * Non-error message type. |
||
33 | */ |
||
34 | const TYPE_INFO = 'info'; |
||
35 | |||
36 | /** |
||
37 | * Warning message type |
||
38 | */ |
||
39 | const TYPE_WARNING = 'warning'; |
||
40 | |||
41 | /** |
||
42 | * Message type is html |
||
43 | */ |
||
44 | const CAST_HTML = 'html'; |
||
45 | |||
46 | /** |
||
47 | * Message type is plain text |
||
48 | */ |
||
49 | const CAST_TEXT = 'text'; |
||
50 | |||
51 | /** |
||
52 | * Is the result valid or not. |
||
53 | * Note that there can be non-error messages in the list. |
||
54 | * |
||
55 | * @var bool |
||
56 | */ |
||
57 | protected $isValid = true; |
||
58 | |||
59 | /** |
||
60 | * List of messages |
||
61 | * |
||
62 | * @var array |
||
63 | */ |
||
64 | protected $messages = array(); |
||
65 | |||
66 | /** |
||
67 | * Create a new ValidationResult. |
||
68 | * By default, it is a successful result. Call $this->error() to record errors. |
||
69 | */ |
||
70 | public function __construct() |
||
71 | { |
||
72 | if (func_num_args() > 0) { |
||
73 | Deprecation::notice( |
||
74 | '3.2', |
||
75 | '$valid parameter is deprecated please addError to mark the result as invalid', |
||
76 | false |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
77 | ); |
||
78 | $this->isValid = func_get_arg(0); |
||
79 | } |
||
80 | if (func_num_args() > 1) { |
||
81 | Deprecation::notice( |
||
82 | '3.2', |
||
83 | '$message parameter is deprecated please use addMessage or addError instead', |
||
84 | false |
||
85 | ); |
||
86 | $this->addError(func_get_arg(1)); |
||
87 | } |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Record an error against this validation result, |
||
92 | * |
||
93 | * @param string $message The message string. |
||
94 | * @param string $messageType Passed as a CSS class to the form, so other values can be used if desired. |
||
95 | * Standard types are defined by the TYPE_ constant definitions. |
||
96 | * @param string $code A codename for this error. Only one message per codename will be added. |
||
97 | * This can be usedful for ensuring no duplicate messages |
||
98 | * @param string|bool $cast Cast type; One of the CAST_ constant definitions. |
||
99 | * Bool values will be treated as plain text flag. |
||
100 | * @return $this |
||
101 | */ |
||
102 | public function addError($message, $messageType = self::TYPE_ERROR, $code = null, $cast = self::CAST_TEXT) |
||
103 | { |
||
104 | return $this->addFieldError(null, $message, $messageType, $code, $cast); |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Record an error against this validation result, |
||
109 | * |
||
110 | * @param string $fieldName The field to link the message to. If omitted; a form-wide message is assumed. |
||
111 | * @param string $message The message string. |
||
112 | * @param string $messageType The type of message: e.g. "bad", "warning", "good", or "required". Passed as a CSS |
||
113 | * class to the form, so other values can be used if desired. |
||
114 | * @param string $code A codename for this error. Only one message per codename will be added. |
||
115 | * This can be usedful for ensuring no duplicate messages |
||
116 | * @param string|bool $cast Cast type; One of the CAST_ constant definitions. |
||
117 | * Bool values will be treated as plain text flag. |
||
118 | * @return $this |
||
119 | */ |
||
120 | public function addFieldError( |
||
121 | $fieldName, |
||
122 | $message, |
||
123 | $messageType = self::TYPE_ERROR, |
||
124 | $code = null, |
||
125 | $cast = self::CAST_TEXT |
||
126 | ) { |
||
127 | $this->isValid = false; |
||
128 | return $this->addFieldMessage($fieldName, $message, $messageType, $code, $cast); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Add a message to this ValidationResult without necessarily marking it as an error |
||
133 | * |
||
134 | * @param string $message The message string. |
||
135 | * @param string $messageType The type of message: e.g. "bad", "warning", "good", or "required". Passed as a CSS |
||
136 | * class to the form, so other values can be used if desired. |
||
137 | * @param string $code A codename for this error. Only one message per codename will be added. |
||
138 | * This can be usedful for ensuring no duplicate messages |
||
139 | * @param string|bool $cast Cast type; One of the CAST_ constant definitions. |
||
140 | * Bool values will be treated as plain text flag. |
||
141 | * @return $this |
||
142 | */ |
||
143 | public function addMessage($message, $messageType = self::TYPE_ERROR, $code = null, $cast = self::CAST_TEXT) |
||
144 | { |
||
145 | return $this->addFieldMessage(null, $message, $messageType, $code, $cast); |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Add a message to this ValidationResult without necessarily marking it as an error |
||
150 | * |
||
151 | * @param string $fieldName The field to link the message to. If omitted; a form-wide message is assumed. |
||
152 | * @param string $message The message string. |
||
153 | * @param string $messageType The type of message: e.g. "bad", "warning", "good", or "required". Passed as a CSS |
||
154 | * class to the form, so other values can be used if desired. |
||
155 | * @param string $code A codename for this error. Only one message per codename will be added. |
||
156 | * This can be usedful for ensuring no duplicate messages |
||
157 | * @param string|bool $cast Cast type; One of the CAST_ constant definitions. |
||
158 | * Bool values will be treated as plain text flag. |
||
159 | * @return $this |
||
160 | */ |
||
161 | public function addFieldMessage( |
||
162 | $fieldName, |
||
163 | $message, |
||
164 | $messageType = self::TYPE_ERROR, |
||
165 | $code = null, |
||
166 | $cast = self::CAST_TEXT |
||
167 | ) { |
||
168 | if ($code && is_numeric($code)) { |
||
169 | throw new InvalidArgumentException("Don't use a numeric code '$code'. Use a string."); |
||
170 | } |
||
171 | if (is_bool($cast)) { |
||
172 | $cast = $cast ? self::CAST_TEXT : self::CAST_HTML; |
||
173 | } |
||
174 | $metadata = array( |
||
175 | 'message' => $message, |
||
176 | 'fieldName' => $fieldName, |
||
177 | 'messageType' => $messageType, |
||
178 | 'messageCast' => $cast, |
||
179 | ); |
||
180 | |||
181 | if ($code) { |
||
182 | $this->messages[$code] = $metadata; |
||
183 | } else { |
||
184 | $this->messages[] = $metadata; |
||
185 | } |
||
186 | |||
187 | return $this; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Returns true if the result is valid. |
||
192 | * @return boolean |
||
193 | */ |
||
194 | public function isValid() |
||
195 | { |
||
196 | return $this->isValid; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Return the full error meta-data, suitable for combining with another ValidationResult. |
||
201 | * |
||
202 | * @return array Array of messages, where each item is an array of data for that message. |
||
203 | */ |
||
204 | public function getMessages() |
||
205 | { |
||
206 | return $this->messages; |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Combine this Validation Result with the ValidationResult given in other. |
||
211 | * It will be valid if both this and the other result are valid. |
||
212 | * This object will be modified to contain the new validation information. |
||
213 | * |
||
214 | * @param ValidationResult $other the validation result object to combine |
||
215 | * @return $this |
||
216 | */ |
||
217 | public function combineAnd(ValidationResult $other) |
||
218 | { |
||
219 | $this->isValid = $this->isValid && $other->isValid(); |
||
220 | $this->messages = array_merge($this->messages, $other->getMessages()); |
||
221 | return $this; |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * String representation of object |
||
226 | * |
||
227 | * @return string the string representation of the object or null |
||
228 | */ |
||
229 | public function serialize() |
||
230 | { |
||
231 | return json_encode([$this->messages, $this->isValid]); |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Constructs the object |
||
236 | * |
||
237 | * @param string $serialized |
||
238 | */ |
||
239 | public function unserialize($serialized) |
||
240 | { |
||
241 | list($this->messages, $this->isValid) = json_decode($serialized, true); |
||
242 | } |
||
243 | } |
||
244 |