Messages.php
3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
/**
* Created by PhpStorm.
* User: Zip
* Date: 15/4/19
* Time: 下午7:29
*/
namespace Hood\Helper\Validation;
class Messages
{
private $messages = array();
private $_customMessages = array();
private $defaultMessagesLanguage = array();
public function __construct(array $customMessages = array())
{
$this->defaultMessagesLanguage = Language::$messages;
$this->customMessages($customMessages);
}
/**
* TODO 自定义数据
* xxx.required=>'xxx'
* xxx.min=>'xxx'
*
* 设置自定义消息
* @param array $customMessages
*/
public function customMessages(array $customMessages = array())
{
$_customMessages = array();
foreach ($customMessages as $key => $custom) {
if (is_string($custom)) {
$_customMessages[$key] = $custom;
} elseif (is_array($custom)) {
foreach ($custom as $_key => $_val) {
$_customMessages[$key . '.' . $_key] = $_val;
}
}
}
unset($customMessages);
$this->_customMessages = $_customMessages;
}
/**
* 设置一条自定义消息
* @param $key
* @param $message
* @return $this
*/
public function add($key, $message)
{
$this->_customMessages[$key] = $message;
return $this;
}
/**
* 获取所有错误
* @return array
*/
public function all()
{
$all = array();
foreach ($this->messages as $key => $messages) {
$all = array_merge($all, $messages);
}
return $all;
}
/**
* 存储消息
* @param $key
* @param $val
*/
public function setMessages($key, $val)
{
$this->messages[$key][] = $val;
}
/**
* 查看所有字段的所有错误信息
* @param $key
* @return array
*/
public function get($key)
{
if (isset($this->messages[$key])) {
return $this->messages[$key];
}
return array();
}
/**
* 判断是否有错误信息
* @return bool
*/
public function has($key = null)
{
if ($key == null) {
return count($this->messages) > 0;
}
return isset($this->messages[$key]);
}
/**
* 第一个错误
* @param $key
* @return array
*/
public function first($key = null)
{
if (is_null($key) && count($this->messages) > 0) {
$messages = array_slice($this->messages, 0, 1);
return $messages[key($messages)][0];
}
$messages = $this->get($key);
return (count($messages) > 0) ? $messages[0] : '';
}
/**
* 获取消息
* @param $key
* @param null $type
* @return string
*/
public function getMessages($attribute, $rule, $type = null)
{
if (isset($this->_customMessages[$attribute . '.' . $rule])) {
return $this->_customMessages[$attribute . '.' . $rule];
} elseif (isset($this->_customMessages[$attribute])) {
return $this->_customMessages[$attribute];
} elseif (array_key_exists($rule, $this->defaultMessagesLanguage)) {
if (!empty($type) && isset($this->defaultMessagesLanguage[$rule][$type])) {
return $this->defaultMessagesLanguage[$rule][$type];
}
return $this->defaultMessagesLanguage[$rule];
}
return '';
}
}