11<?php
2- /**
3- * Created by PhpStorm.
4- * User: inhere
5- * Date: 2017-03-17
6- * Time: 11:26
7- */
82
93namespace Inhere \Validate \Traits ;
104
115use Inhere \Validate \Helper ;
12- use Inhere \Validate \Validator \Messages ;
6+ use Inhere \Validate \Validator \GlobalMessage ;
137use Inhere \Validate \Validators ;
148
159/**
@@ -76,7 +70,7 @@ protected function shouldStop(): bool
7670 }
7771
7872 /**
79- * 是否有错误
73+ * Is there an error?
8074 * @return boolean
8175 */
8276 public function hasError (): bool
@@ -94,6 +88,7 @@ public function isFail(): bool
9488
9589 /**
9690 * @return bool
91+ * @deprecated will delete, please use isFail() instead
9792 */
9893 public function fail (): bool
9994 {
@@ -110,6 +105,7 @@ public function failed(): bool
110105
111106 /**
112107 * @return bool
108+ * @deprecated will delete, please use isOk() or isPassed() instead
113109 */
114110 public function ok (): bool
115111 {
@@ -125,7 +121,7 @@ public function isOk(): bool
125121 }
126122
127123 /**
128- * @deprecated will delete
124+ * @deprecated will delete, please use isOk() or isPassed() instead
129125 * @return bool
130126 */
131127 public function passed (): bool
@@ -169,10 +165,10 @@ public function addError(string $field, string $msg)
169165 }
170166
171167 /**
172- * @param string|null $field Only get errors of the field.
168+ * @param string $field Only get errors of the field.
173169 * @return array
174170 */
175- public function getErrors (string $ field = null ): array
171+ public function getErrors (string $ field = '' ): array
176172 {
177173 if ($ field ) {
178174 $ errors = [];
@@ -197,27 +193,32 @@ public function clearErrors()
197193 }
198194
199195 /**
200- * 得到第一个错误信息
201- * @author inhere
196+ * Get the first error message
202197 * @param bool $onlyMsg
203198 * @return array|string
204199 */
205200 public function firstError (bool $ onlyMsg = true )
206201 {
207- $ errors = $ this ->_errors ;
208- $ first = \array_shift ($ errors );
202+ if (!$ errors = $ this ->_errors ) {
203+ return $ onlyMsg ? '' : [];
204+ }
205+
206+ $ first = \array_shift ($ errors );
209207 return $ onlyMsg ? $ first ['msg ' ] : $ first ;
210208 }
211209
212210 /**
213- * 得到最后一个错误信息
211+ * Get the last error message
214212 * @param bool $onlyMsg
215213 * @return array|string
216214 */
217215 public function lastError (bool $ onlyMsg = true )
218216 {
219- $ ers = $ this ->_errors ;
220- $ last = \array_pop ($ ers );
217+ if (!$ errors = $ this ->_errors ) {
218+ return $ onlyMsg ? '' : [];
219+ }
220+
221+ $ last = \array_pop ($ errors );
221222 return $ onlyMsg ? $ last ['msg ' ] : $ last ;
222223 }
223224
@@ -294,19 +295,19 @@ public function getMessage($validator, string $field, array $args = [], $message
294295
295296 // get message from built in dict.
296297 if (!$ message ) {
297- $ message = $ this ->findMessage ($ field , $ rawName ) ?: Messages ::getDefault ();
298+ $ message = $ this ->findMessage ($ field , $ rawName ) ?: GlobalMessage ::getDefault ();
298299 // is array. It's defined multi error messages
299300 } elseif (\is_array ($ message )) {
300301 $ message = $ message [$ rawName ] ?? $ this ->findMessage ($ field , $ rawName );
301302
302303 if (!$ message ) { // use default
303- return \strtr (Messages ::getDefault (), $ params );
304+ return \strtr (GlobalMessage ::getDefault (), $ params );
304305 }
305306 } else {
306307 $ message = (string )$ message ;
307308 }
308309
309- /** @see Messages ::$messages['size'] */
310+ /** @see GlobalMessage ::$messages['size'] */
310311 if (\is_array ($ message )) {
311312 $ msgKey = \count ($ params ) - 1 ;
312313 $ message = $ message [$ msgKey ] ?? $ message [0 ];
@@ -345,7 +346,7 @@ protected function findMessage(string $field, string $rawName)
345346 } elseif (isset ($ this ->_messages [$ realName ])) {
346347 $ message = $ this ->_messages [$ realName ];
347348 } else { // get from default
348- $ message = Messages ::get ($ realName );
349+ $ message = GlobalMessage ::get ($ realName );
349350 }
350351
351352 return $ message ;
@@ -421,7 +422,7 @@ public function isPrettifyName(): bool
421422 /**
422423 * @param bool $prettifyName
423424 */
424- public function setPrettifyName (bool $ prettifyName ): void
425+ public function setPrettifyName (bool $ prettifyName = true ): void
425426 {
426427 $ this ->_prettifyName = $ prettifyName ;
427428 }
0 commit comments