Rules.php 8.5 KB

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