33namespace Odan \Validation ;
44
55/**
6- * ValidationMessage .
6+ * Validation Message .
77 *
88 * Represents a container for the results of a validation request.
99 */
@@ -24,7 +24,7 @@ class ValidationMessage
2424 *
2525 * @return array Errors
2626 */
27- public function getErrors ()
27+ public function getErrors (): array
2828 {
2929 return $ this ->errors ;
3030 }
@@ -34,7 +34,7 @@ public function getErrors()
3434 *
3535 * @return mixed Error
3636 */
37- public function getError ()
37+ public function getFirstError ()
3838 {
3939 return reset ($ this ->errors );
4040 }
@@ -44,7 +44,7 @@ public function getError()
4444 *
4545 * @return string|null
4646 */
47- public function getMessage ()
47+ public function getMessage (): ? string
4848 {
4949 return $ this ->message ;
5050 }
@@ -54,13 +54,11 @@ public function getMessage()
5454 *
5555 * @param string $message The default success message
5656 *
57- * @return self self
57+ * @return void
5858 */
5959 public function setMessage (string $ message )
6060 {
6161 $ this ->message = $ message ;
62-
63- return $ this ;
6462 }
6563
6664 /**
@@ -86,29 +84,31 @@ public function failed(): bool
8684 /**
8785 * Clear errors and message.
8886 *
89- * @return self
87+ * @return void
9088 */
9189 public function clear ()
9290 {
9391 $ this ->message = null ;
9492 $ this ->errors = [];
95-
96- return $ this ;
9793 }
9894
9995 /**
10096 * Add error message.
10197 *
102- * @param string $field Field name
103- * @param string $message Message
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)
104102 *
105- * @return self
103+ * @return void
106104 */
107- public function addError (string $ field , string $ message )
105+ public function addError (string $ field , string $ message, string $ code = null )
108106 {
109- $ this ->errors [] = ['field ' => $ field , 'message ' => $ message ];
110-
111- return $ this ;
107+ if ($ code === null ) {
108+ $ this ->errors [] = ['field ' => $ field , 'message ' => $ message ];
109+ } else {
110+ $ this ->errors [] = ['field ' => $ field , 'message ' => $ message , 'code ' => $ code ];
111+ }
112112 }
113113
114114 /**
0 commit comments