33namespace Odan \Validation ;
44
55/**
6- * Validation Message.
6+ * Message.
77 *
8- * Represents a container for the results of a validation request .
8+ * Represents a status and a message .
99 */
1010class ValidationMessage
1111{
1212 /**
13- * @var array
13+ * @var string
1414 */
15- protected $ errors = [] ;
15+ protected $ field ;
1616
1717 /**
18- * @var string|null
19- */
20- protected $ message = null ;
21-
22- /**
23- * Get all errors.
24- *
25- * @return array Errors
18+ * @var string
2619 */
27- public function getErrors (): array
28- {
29- return $ this ->errors ;
30- }
20+ protected $ message ;
3121
3222 /**
33- * Get first error.
34- *
35- * @return mixed Error
36- */
37- public function getFirstError ()
38- {
39- return reset ($ this ->errors );
40- }
41-
42- /**
43- * Get message.
44- *
45- * @return string|null
23+ * @var string|null
4624 */
47- public function getMessage (): ?string
48- {
49- return $ this ->message ;
50- }
25+ protected $ code = null ;
5126
5227 /**
53- * Set the default success message.
54- *
55- * @param string $message The default success message
28+ * Constructor.
5629 *
57- * @return void
30+ * @param string $field The field name
31+ * @param string $message The Message
32+ * @param string|null $code The error code (optional)
5833 */
59- public function setMessage (string $ message )
34+ public function __construct (string $ field , string $ message, string $ code = null )
6035 {
36+ $ this ->field = $ field ;
6137 $ this ->message = $ message ;
38+ $ this ->code = $ code ;
6239 }
6340
6441 /**
65- * Returns the success of the validation .
42+ * Returns the message .
6643 *
67- * @return bool true if validation was successful; otherwise, false
44+ * @return string
6845 */
69- public function success (): bool
46+ public function getMessage (): string
7047 {
71- return empty ($ this ->errors );
72- }
73-
74- /**
75- * Get validation failed status.
76- *
77- * @return bool Status
78- */
79- public function failed (): bool
80- {
81- return !empty ($ this ->errors );
48+ return $ this ->message ;
8249 }
8350
8451 /**
85- * Clear errors and message .
52+ * Returns the field name .
8653 *
87- * @return void
54+ * @return string The field name
8855 */
89- public function clear ()
56+ public function getField (): string
9057 {
91- $ this ->message = null ;
92- $ this ->errors = [];
58+ return $ this ->field ;
9359 }
9460
9561 /**
96- * Add error message.
97- *
98- * @param string $field the field name containing the error
99- * @param string $message A String providing a short description of the error.
100- * The message SHOULD be limited to a concise single sentence.
101- * @param string|null $code A numeric or alphanumeric value that indicates the error type that occurred. (optional)
62+ * Returns the validation status code.
10263 *
103- * @return void
64+ * @return string|null The validation status code
10465 */
105- public function addError ( string $ field , string $ message , string $ code = null )
66+ public function getCode (): ? string
10667 {
107- if ($ code === null ) {
108- $ this ->errors [] = ['field ' => $ field , 'message ' => $ message ];
109- } else {
110- $ this ->errors [] = ['field ' => $ field , 'message ' => $ message , 'code ' => $ code ];
111- }
68+ return $ this ->code ;
11269 }
11370
11471 /**
@@ -119,16 +76,13 @@ public function addError(string $field, string $message, string $code = null)
11976 public function toArray (): array
12077 {
12178 $ result = [
122- 'success ' => $ this ->success (),
79+ 'field ' => $ this ->getField (),
80+ 'message ' => $ this ->getMessage (),
12381 ];
12482
125- $ message = $ this ->getMessage ();
126- if ($ message !== null ) {
127- $ result ['message ' ] = $ message ;
128- }
129-
130- if ($ errors = $ this ->getErrors ()) {
131- $ result ['errors ' ] = $ errors ;
83+ $ code = $ this ->getCode ();
84+ if ($ code !== null ) {
85+ $ result ['code ' ] = $ code ;
13286 }
13387
13488 return $ result ;
0 commit comments