Rules.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. namespace Qii\Base;
  3. class Rules
  4. {
  5. /**
  6. * @var array $rules 验证规则
  7. */
  8. private $rules = array();
  9. /**
  10. * @var array $message 验证消息
  11. */
  12. private $message = array();
  13. /**
  14. * @var array $data 待验证的数据
  15. */
  16. private $data = array();
  17. /**
  18. * 需要验证的字段
  19. */
  20. private $forceValidKey = array();
  21. /**
  22. * 选择其中一个去验证
  23. */
  24. private $optionValidKey = array();
  25. public function __construct()
  26. {
  27. $this->constants();
  28. $this->clean();
  29. }
  30. public function getDefaultValues()
  31. {
  32. $data = array();
  33. foreach($this->fields() AS $key => $val)
  34. {
  35. $data[$val] = '';
  36. }
  37. return $data;
  38. }
  39. /**
  40. * 返回数据表中的字段名
  41. *
  42. * @return array
  43. */
  44. public function fields()
  45. {
  46. return array();
  47. }
  48. /**
  49. * 定义规则
  50. */
  51. public function constants()
  52. {
  53. return array();
  54. }
  55. public function defaultValues()
  56. {
  57. $keys = $this->fields();
  58. $values = array();
  59. foreach($keys AS $key => $val)
  60. {
  61. $values[$val] = '';
  62. }
  63. return $values;
  64. }
  65. /**
  66. * 获取所有字段中的数据
  67. *
  68. * @return array
  69. */
  70. public function getValues()
  71. {
  72. return $this->data;
  73. }
  74. /**
  75. * 获取制定字段数据
  76. *
  77. * @param string $field 字段名
  78. * @return mixed|null
  79. */
  80. public function getValue($field)
  81. {
  82. if(isset($this->data[$field])) return $this->data[$field];
  83. return null;
  84. }
  85. /**
  86. * 清空数据
  87. */
  88. public function clean()
  89. {
  90. $this->data = array();
  91. $this->forceValidKey = array();
  92. }
  93. /**
  94. * 获取指定字段的规则配置
  95. * @param string $field
  96. * @return array
  97. */
  98. public function get($field)
  99. {
  100. $data = array();
  101. if(isset($this->rules[$field]))
  102. {
  103. $data['rules'] = $this->rules[$field];
  104. $data['message'] = isset($this->message[$field]) ? $this->message[$field] : '未设置';
  105. }
  106. return $data;
  107. }
  108. /**
  109. * 自动获取rules中定义的规则
  110. * @param string $method 方法名
  111. */
  112. public function autoForceValidKeyForMethod($method)
  113. {
  114. if(!$method) throw new Exception(__METHOD__ . ' parameter error.', 1);
  115. if(method_exists($this, $method))
  116. {
  117. $this->$method();
  118. }
  119. if(!$method) throw new Exception(__METHOD__ . ' undefined.', 1);
  120. }
  121. /**
  122. * 添加规则
  123. */
  124. public function addRules($field, $key, $isValid, $message)
  125. {
  126. if(!$field || !$key || $isValid === null || $isValid === '') return;
  127. if(!$this->isAllow($key)) return;
  128. $this->rules[$field][$key] = $isValid;
  129. $this->message[$field][$key] = $message;
  130. }
  131. /**
  132. * 添加必须验证用的字段
  133. * @param string $key 字段名
  134. */
  135. public function addForceValidKey($key)
  136. {
  137. if(!$key) return;
  138. if(is_array($key))
  139. {
  140. foreach ($key as $k => $value)
  141. {
  142. $this->addForceValidKey($value);
  143. }
  144. }
  145. else
  146. {
  147. if(!in_array($key, $this->forceValidKey))
  148. {
  149. $this->forceValidKey[] = $key;
  150. }
  151. }
  152. }
  153. /**
  154. * 添加必须其中某一个字段,选择不为空的字段去验证
  155. *
  156. * @param string $key 字段名
  157. */
  158. public function addOptionValidKey($key)
  159. {
  160. if(!$key) return;
  161. if(is_array($key))
  162. {
  163. foreach ($key as $k => $value)
  164. {
  165. $this->addOptionValidKey($value);
  166. }
  167. }
  168. else
  169. {
  170. if(!in_array($key, $this->optionValidKey))
  171. {
  172. $this->optionValidKey[] = $key;
  173. }
  174. }
  175. }
  176. /**
  177. * 给数据添加属性
  178. * @param string $field 字段
  179. * @param mix $val 值
  180. */
  181. public function addValue($field, $val)
  182. {
  183. if(in_array($field, $this->fields())){
  184. $this->data[$field] = $val;
  185. }
  186. }
  187. /**
  188. * 添加数据
  189. * @param array $data 数据
  190. */
  191. public function addValues($data)
  192. {
  193. foreach ($data AS $field => $value){
  194. $this->addValue($field, $value);
  195. }
  196. }
  197. /**
  198. * 验证数据,验证将返回数据及验证结果
  199. * @return bool
  200. */
  201. public function verify()
  202. {
  203. $data = array();
  204. $data['data'] = $this->data;
  205. $data['code'] = 0;
  206. $data['valid'] = true;
  207. $data['msg'] = '';
  208. if(empty($this->forceValidKey))
  209. {
  210. return $data;
  211. }
  212. $valid = _loadClass('\Qii\Library\Validate');
  213. //将optionValidKey中不为空的字段添加到必须验证的字段中去
  214. //如果选择验证的都没数据就提示参数错误
  215. $options = array();
  216. foreach($this->optionValidKey AS $key)
  217. {
  218. if($this->data[$key] && $this->data[$key] != '')
  219. {
  220. $options[] = $key;
  221. $this->addForceValidKey($key);
  222. }
  223. }
  224. if(count($this->optionValidKey) > 0 && count($options) == 0)
  225. {
  226. $data['valid'] = false;
  227. $data['code'] = 20000;
  228. $data['errorInfo'][] = join(',', $this->optionValidKey) . ' 字段必须填写一个';
  229. $data['msg'] = _i($data['code']);
  230. return $data;
  231. }
  232. foreach($this->forceValidKey AS $key)
  233. {
  234. $rule = $this->get($key);
  235. if(!$rule)
  236. {
  237. $rule['rules'] = array('required' => true);
  238. $rule['message'] = array('required' => $key .'不能为空');
  239. }
  240. $result = $valid->verify(
  241. array($key => isset($this->data[$key]) ? $this->data[$key] : ''),
  242. array($key => $rule['rules']),
  243. array($key => $rule['message'])
  244. );
  245. if($result !== true){
  246. $data['valid'] = false;
  247. $data['code'] = 20000;
  248. $data['errorInfo'][$key] = $result;
  249. }
  250. }
  251. if($data['code'] > 0)
  252. {
  253. $data['msg'] = _i($data['code']);
  254. }
  255. return $data;
  256. }
  257. /**
  258. * 验证数据,验证将返回数据及验证结果
  259. * @return bool
  260. */
  261. /*
  262. public function verify()
  263. {
  264. $data = array();
  265. $data['data'] = array();
  266. $valid = _loadClass('Qii_Library_Validate');
  267. foreach($this->data AS $key => $val)
  268. {
  269. $rule = $this->get($key);
  270. $data['data'][$key] = $val;
  271. if(empty($rule))
  272. {
  273. continue;
  274. }
  275. $result = $valid->verify(array($key => $val), array($key => $rule['rules']), array($key => $rule['message']));
  276. if($result !== true){
  277. $data['valid'][$key] = $result;
  278. }
  279. }
  280. return $data;
  281. }*/
  282. /**
  283. * 是否在允许的规则内
  284. * @param string $key 规则名称
  285. * @return bool
  286. */
  287. public function isAllow($key)
  288. {
  289. $allow = array(
  290. 'required', 'email', 'idcode', 'http',
  291. 'qq', 'postcode', 'ip', 'phone', 'telephone',
  292. 'mobile', 'en', 'cn', 'account', 'number', 'date',
  293. 'safe', 'password', 'maxlength', 'minlength', 'length',
  294. 'rangeof', 'string', 'sets', 'setsArray'
  295. );
  296. return in_array($key, $allow);
  297. }
  298. }