Rules.php 7.3 KB

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