Rules.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. * 数据保存规则
  4. * @author Jinhui Zhu<jinhui.zhu@live.cn> 2015-12-16
  5. */
  6. namespace Qii\Driver;
  7. class Rules
  8. {
  9. const VERSION = '1.2';
  10. /**
  11. * @var $rules
  12. */
  13. private $rules;
  14. /**
  15. * @var $cacheRules
  16. */
  17. static $cacheRules;
  18. static $invalidMessage;
  19. public function __construct($rules)
  20. {
  21. $this->rules = $rules;
  22. }
  23. /**
  24. * 获取使用的数据表名
  25. *
  26. * @return mixed
  27. * @throws \Exception 未定义database的时候就抛出异常
  28. */
  29. public function getDatabase()
  30. {
  31. if (!isset($this->rules['database'])) {
  32. throw new \Exception(\Qii::i(5001, 'database'), __LINE__);
  33. }
  34. return $this->rules['database'];
  35. }
  36. /**
  37. * 获取数据表名称
  38. *
  39. * @return mixed
  40. * @throws \Exception 未定义table的时候就抛出异常
  41. * @return string
  42. */
  43. public function getTableName()
  44. {
  45. if (!isset($this->rules['tableName'])) {
  46. throw new \Exception(\Qii::i(5001, 'tableName'), __LINE__);
  47. }
  48. return $this->rules['tableName'];
  49. }
  50. /**
  51. * 获取数据表中的字段列表
  52. *
  53. * @return mixed
  54. * @throws \Exception
  55. */
  56. public function getFields()
  57. {
  58. if (!isset($this->rules['rules']['fields'])) {
  59. throw new \Exception(\Qii::i(5002, 'fields'), __LINE__);
  60. }
  61. return $this->rules['rules']['fields'];
  62. }
  63. /**
  64. * 获取验证失败的时候提示信息
  65. * 优先使用规则里表中的验证提示信息,如果未指定,再使用自动生成的规则信息
  66. * @return array 提示信息
  67. */
  68. public function getInvalidMessage()
  69. {
  70. if (isset($this->rules['rules']['invalidMessage'])) return $this->rules['rules']['invalidMessage'];
  71. if (isset(self::$invalidMessage[$this->rules['database'] . $this->rules['tableName']])) return self::$invalidMessage[$this->rules['database'] . $this->rules['tableName']];
  72. return array();
  73. }
  74. /**
  75. * 获取数据表保存\更新\的验证规则
  76. * @return array 获取验证规则
  77. */
  78. public function getOriginalRules()
  79. {
  80. if (!isset($this->rules['rules'])) {
  81. return array();
  82. }
  83. return $this->rules['rules'];
  84. }
  85. /**
  86. * 重置privateKeys使用
  87. *
  88. * @return array
  89. */
  90. public function getOperateValidFields($opt = null)
  91. {
  92. $fields = array();
  93. //检查是否存在数据使用字段
  94. if (isset($this->rules['rules']['existValid'])) {
  95. $fields['exist'] = array_keys($this->rules['rules']['existValid']);
  96. }
  97. //检查是保存使用字段
  98. if (isset($this->rules['rules']['saveValid'])) {
  99. $fields['save'] = array_keys($this->rules['rules']['saveValid']);
  100. }
  101. //更新使用字段
  102. if (isset($this->rules['rules']['updateValid'])) {
  103. $fields['update'] = array_keys($this->rules['rules']['updateValid']);
  104. }
  105. //删除使用字段
  106. if (isset($this->rules['rules']['removeValid'])) {
  107. $fields['remove'] = array_keys($this->rules['rules']['removeValid']);
  108. }
  109. if (!$opt) return $fields;
  110. return isset($fields[$opt]) ? $fields[$opt] : array();
  111. }
  112. /**
  113. * 获取唯一字段,如果不设置privatekey就是用此字段作为privatekey
  114. */
  115. public function getPrivateKey()
  116. {
  117. if (isset($this->rules['rules']['uni']) && count($this->rules['rules']['uni']) > 0) return $this->rules['rules']['uni'];
  118. }
  119. /**
  120. * 根据字段获取验证规则
  121. * @param string $field 需要验证的字段
  122. * @return array 验证规则
  123. */
  124. public function getRulesByField($field)
  125. {
  126. $rules = $this->buildRules();
  127. return isset($rules[$field]) ? $rules[$field] : array();
  128. }
  129. /**
  130. * 根据配置生成所有验证需要的规则
  131. *
  132. * @return array 生成规则
  133. */
  134. public function buildRules()
  135. {
  136. $rules = array();
  137. if (!isset($this->rules['rules']['validate']) && count($this->rules['rules']['validate']) == 0) {
  138. return $rules;
  139. }
  140. if (isset(self::$cacheRules[$this->rules['database'] . $this->rules['tableName']]['rules'])) return self::$cacheRules[$this->rules['database'] . $this->rules['tableName']]['rules'];
  141. foreach ($this->rules['rules']['validate'] AS $key => $validate) {
  142. $fieldRules = array();
  143. $fieldRules['required'] = false;
  144. foreach ($validate AS $rule) {
  145. if ($rule == 'minlength') {
  146. $fieldRules[$rule] = 1;
  147. continue;
  148. }
  149. if ($rule == 'maxlength') {
  150. $fieldRules[$rule] = $this->rules['rules']['length'][$key];
  151. continue;
  152. }
  153. if ($rule == 'sets') {
  154. if (isset($this->rules['rules']['sets'][$key])) $fieldRules[$rule] = $this->rules['rules']['sets'][$key];
  155. continue;
  156. }
  157. $fieldRules[$rule] = true;
  158. //根据验证类型自动生成规则验证错误信息
  159. $alias = isset($this->rules['rules']['alias']) && isset($this->rules['rules']['alias'][$key]) ? $this->rules['rules']['alias'][$key] : $key;
  160. self::$invalidMessage[$this->rules['database'] . $this->rules['tableName']][$key][$rule] = $rule == 'required' ? \Qii::i(5003, $alias) : \Qii::i(5004, $alias);
  161. }
  162. $rules[$key] = $fieldRules;
  163. }
  164. self::$cacheRules[$this->rules['database'] . $this->rules['tableName']]['rules'] = $rules;
  165. return $rules;
  166. }
  167. /**
  168. * 通过数据库操作类型获取对应的规则
  169. * @param string $opt 操作类型
  170. * @return array 规则
  171. */
  172. public function getRulesByOperate($opt = 'save')
  173. {
  174. $rules = array();
  175. $allowOpt = array('save', 'update', 'remove');
  176. if (!in_array($opt, $allowOpt)) {
  177. throw new \Qii\Exceptions\NotAllowed(\Qii::i(5002, $opt), __LINE__);
  178. }
  179. if (isset(self::$cacheRules[$this->rules['database'] . $this->rules['tableName']][$opt])) return self::$cacheRules[$this->rules['database'] . $this->rules['tableName']][$opt];
  180. $buildRules = $this->buildRules();
  181. //获取字段的验证类型
  182. $fieldsValidate = $this->rules['rules']['validate'];
  183. //获取操作需要验证的字段
  184. if (!isset($this->rules['rules'][$opt]) || count($this->rules['rules'][$opt]) == 0) return $rules;
  185. foreach ($this->rules['rules'][$opt] AS $key => $val) {
  186. //如果操作需要验证字段没有设定规则,就验证字段必须有值
  187. if (isset($buildRules[$key])) {
  188. $rules[$key] = $buildRules[$key];
  189. } else {
  190. $rules[$key] = array('required' => true);
  191. }
  192. }
  193. self::$cacheRules[$this->rules['database'] . $this->rules['tableName']][$opt] = $rules;
  194. return $rules;
  195. }
  196. /**
  197. * 通过 $this->$val来获取 $this->$val();的返回内容
  198. * @param string $name 属性名称
  199. */
  200. public function __get($name)
  201. {
  202. if (method_exists($this, $name)) {
  203. return call_user_func_array(array($this, $name), array());
  204. }
  205. throw new \Qii\Exceptions\MethodNotFound(\Qii::i(1101, $name), __LINE__);
  206. }
  207. /**
  208. * 调用不存在的方法抛出方法不存在的异常
  209. * @param string $method
  210. * @param mix $args
  211. */
  212. public function __call($method, $args)
  213. {
  214. throw new \Qii\Exceptions\MethodNotFound(\Qii::i(1101, $method), __LINE__);
  215. }
  216. }