Ingen beskrivning
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Validate.php 44KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think;
  12. use think\Db;
  13. use think\validate\ValidateRule;
  14. class Validate
  15. {
  16. /**
  17. * 自定义验证类型
  18. * @var array
  19. */
  20. protected static $type = [];
  21. /**
  22. * 验证类型别名
  23. * @var array
  24. */
  25. protected $alias = [
  26. '>' => 'gt', '>=' => 'egt', '<' => 'lt', '<=' => 'elt', '=' => 'eq', 'same' => 'eq',
  27. ];
  28. /**
  29. * 当前验证规则
  30. * @var array
  31. */
  32. protected $rule = [];
  33. /**
  34. * 验证提示信息
  35. * @var array
  36. */
  37. protected $message = [];
  38. /**
  39. * 验证字段描述
  40. * @var array
  41. */
  42. protected $field = [];
  43. /**
  44. * 默认规则提示
  45. * @var array
  46. */
  47. protected static $typeMsg = [
  48. 'require' => ':attribute require',
  49. 'must' => ':attribute must',
  50. 'number' => ':attribute must be numeric',
  51. 'integer' => ':attribute must be integer',
  52. 'float' => ':attribute must be float',
  53. 'boolean' => ':attribute must be bool',
  54. 'email' => ':attribute not a valid email address',
  55. 'mobile' => ':attribute not a valid mobile',
  56. 'array' => ':attribute must be a array',
  57. 'accepted' => ':attribute must be yes,on or 1',
  58. 'date' => ':attribute not a valid datetime',
  59. 'file' => ':attribute not a valid file',
  60. 'image' => ':attribute not a valid image',
  61. 'alpha' => ':attribute must be alpha',
  62. 'alphaNum' => ':attribute must be alpha-numeric',
  63. 'alphaDash' => ':attribute must be alpha-numeric, dash, underscore',
  64. 'activeUrl' => ':attribute not a valid domain or ip',
  65. 'chs' => ':attribute must be chinese',
  66. 'chsAlpha' => ':attribute must be chinese or alpha',
  67. 'chsAlphaNum' => ':attribute must be chinese,alpha-numeric',
  68. 'chsDash' => ':attribute must be chinese,alpha-numeric,underscore, dash',
  69. 'url' => ':attribute not a valid url',
  70. 'ip' => ':attribute not a valid ip',
  71. 'dateFormat' => ':attribute must be dateFormat of :rule',
  72. 'in' => ':attribute must be in :rule',
  73. 'notIn' => ':attribute be notin :rule',
  74. 'between' => ':attribute must between :1 - :2',
  75. 'notBetween' => ':attribute not between :1 - :2',
  76. 'length' => 'size of :attribute must be :rule',
  77. 'max' => 'max size of :attribute must be :rule',
  78. 'min' => 'min size of :attribute must be :rule',
  79. 'after' => ':attribute cannot be less than :rule',
  80. 'before' => ':attribute cannot exceed :rule',
  81. 'afterWith' => ':attribute cannot be less than :rule',
  82. 'beforeWith' => ':attribute cannot exceed :rule',
  83. 'expire' => ':attribute not within :rule',
  84. 'allowIp' => 'access IP is not allowed',
  85. 'denyIp' => 'access IP denied',
  86. 'confirm' => ':attribute out of accord with :2',
  87. 'different' => ':attribute cannot be same with :2',
  88. 'egt' => ':attribute must greater than or equal :rule',
  89. 'gt' => ':attribute must greater than :rule',
  90. 'elt' => ':attribute must less than or equal :rule',
  91. 'lt' => ':attribute must less than :rule',
  92. 'eq' => ':attribute must equal :rule',
  93. 'unique' => ':attribute has exists',
  94. 'regex' => ':attribute not conform to the rules',
  95. 'method' => 'invalid Request method',
  96. 'token' => 'invalid token',
  97. 'fileSize' => 'filesize not match',
  98. 'fileExt' => 'extensions to upload is not allowed',
  99. 'fileMime' => 'mimetype to upload is not allowed',
  100. ];
  101. /**
  102. * 当前验证场景
  103. * @var array
  104. */
  105. protected $currentScene = null;
  106. /**
  107. * Filter_var 规则
  108. * @var array
  109. */
  110. protected $filter = [
  111. 'email' => FILTER_VALIDATE_EMAIL,
  112. 'ip' => [FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6],
  113. 'integer' => FILTER_VALIDATE_INT,
  114. 'url' => FILTER_VALIDATE_URL,
  115. 'macAddr' => FILTER_VALIDATE_MAC,
  116. 'float' => FILTER_VALIDATE_FLOAT,
  117. ];
  118. /**
  119. * 内置正则验证规则
  120. * @var array
  121. */
  122. protected $defaultRegex = [
  123. 'alphaDash' => '/^[A-Za-z0-9\-\_]+$/',
  124. 'chs' => '/^[\x{4e00}-\x{9fa5}\x{9fa6}-\x{9fef}\x{3400}-\x{4db5}\x{20000}-\x{2ebe0}]+$/u',
  125. 'chsAlpha' => '/^[\x{4e00}-\x{9fa5}\x{9fa6}-\x{9fef}\x{3400}-\x{4db5}\x{20000}-\x{2ebe0}a-zA-Z]+$/u',
  126. 'chsAlphaNum' => '/^[\x{4e00}-\x{9fa5}\x{9fa6}-\x{9fef}\x{3400}-\x{4db5}\x{20000}-\x{2ebe0}a-zA-Z0-9]+$/u',
  127. 'chsDash' => '/^[\x{4e00}-\x{9fa5}\x{9fa6}-\x{9fef}\x{3400}-\x{4db5}\x{20000}-\x{2ebe0}a-zA-Z0-9\_\-]+$/u',
  128. 'mobile' => '/^1[3-9][0-9]\d{8}$/',
  129. 'idCard' => '/(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$)/',
  130. 'zip' => '/\d{6}/',
  131. ];
  132. /**
  133. * 验证场景定义
  134. * @var array
  135. */
  136. protected $scene = [];
  137. /**
  138. * 验证失败错误信息
  139. * @var array
  140. */
  141. protected $error = [];
  142. /**
  143. * 是否批量验证
  144. * @var bool
  145. */
  146. protected $batch = false;
  147. /**
  148. * 场景需要验证的规则
  149. * @var array
  150. */
  151. protected $only = [];
  152. /**
  153. * 场景需要移除的验证规则
  154. * @var array
  155. */
  156. protected $remove = [];
  157. /**
  158. * 场景需要追加的验证规则
  159. * @var array
  160. */
  161. protected $append = [];
  162. /**
  163. * 验证正则定义
  164. * @var array
  165. */
  166. protected $regex = [];
  167. /**
  168. * 架构函数
  169. * @access public
  170. * @param array $rules 验证规则
  171. * @param array $message 验证提示信息
  172. * @param array $field 验证字段描述信息
  173. */
  174. public function __construct(array $rules = [], array $message = [], array $field = [])
  175. {
  176. $this->rule = $rules + $this->rule;
  177. $this->message = array_merge($this->message, $message);
  178. $this->field = array_merge($this->field, $field);
  179. }
  180. /**
  181. * 创建一个验证器类
  182. * @access public
  183. * @param array $rules 验证规则
  184. * @param array $message 验证提示信息
  185. * @param array $field 验证字段描述信息
  186. * @return Validate
  187. */
  188. public static function make(array $rules = [], array $message = [], array $field = [])
  189. {
  190. return new self($rules, $message, $field);
  191. }
  192. /**
  193. * 添加字段验证规则
  194. * @access protected
  195. * @param string|array $name 字段名称或者规则数组
  196. * @param mixed $rule 验证规则或者字段描述信息
  197. * @return $this
  198. */
  199. public function rule($name, $rule = '')
  200. {
  201. if (is_array($name)) {
  202. $this->rule = $name + $this->rule;
  203. if (is_array($rule)) {
  204. $this->field = array_merge($this->field, $rule);
  205. }
  206. } else {
  207. $this->rule[$name] = $rule;
  208. }
  209. return $this;
  210. }
  211. /**
  212. * 注册扩展验证(类型)规则
  213. * @access public
  214. * @param string $type 验证规则类型
  215. * @param mixed $callback callback方法(或闭包)
  216. * @return void
  217. */
  218. public static function extend($type, $callback = null)
  219. {
  220. if (is_array($type)) {
  221. self::$type = array_merge(self::$type, $type);
  222. } else {
  223. self::$type[$type] = $callback;
  224. }
  225. }
  226. /**
  227. * 设置验证规则的默认提示信息
  228. * @access public
  229. * @param string|array $type 验证规则类型名称或者数组
  230. * @param string $msg 验证提示信息
  231. * @return void
  232. */
  233. public static function setTypeMsg($type, $msg = null)
  234. {
  235. if (is_array($type)) {
  236. self::$typeMsg = array_merge(self::$typeMsg, $type);
  237. } else {
  238. self::$typeMsg[$type] = $msg;
  239. }
  240. }
  241. /**
  242. * 设置提示信息
  243. * @access public
  244. * @param string|array $name 字段名称
  245. * @param string $message 提示信息
  246. * @return Validate
  247. */
  248. public function message($name, $message = '')
  249. {
  250. if (is_array($name)) {
  251. $this->message = array_merge($this->message, $name);
  252. } else {
  253. $this->message[$name] = $message;
  254. }
  255. return $this;
  256. }
  257. /**
  258. * 设置验证场景
  259. * @access public
  260. * @param string $name 场景名
  261. * @return $this
  262. */
  263. public function scene($name)
  264. {
  265. // 设置当前场景
  266. $this->currentScene = $name;
  267. return $this;
  268. }
  269. /**
  270. * 判断是否存在某个验证场景
  271. * @access public
  272. * @param string $name 场景名
  273. * @return bool
  274. */
  275. public function hasScene($name)
  276. {
  277. return isset($this->scene[$name]) || method_exists($this, 'scene' . $name);
  278. }
  279. /**
  280. * 设置批量验证
  281. * @access public
  282. * @param bool $batch 是否批量验证
  283. * @return $this
  284. */
  285. public function batch($batch = true)
  286. {
  287. $this->batch = $batch;
  288. return $this;
  289. }
  290. /**
  291. * 指定需要验证的字段列表
  292. * @access public
  293. * @param array $fields 字段名
  294. * @return $this
  295. */
  296. public function only($fields)
  297. {
  298. $this->only = $fields;
  299. return $this;
  300. }
  301. /**
  302. * 移除某个字段的验证规则
  303. * @access public
  304. * @param string|array $field 字段名
  305. * @param mixed $rule 验证规则 null 移除所有规则
  306. * @return $this
  307. */
  308. public function remove($field, $rule = null)
  309. {
  310. if (is_array($field)) {
  311. foreach ($field as $key => $rule) {
  312. if (is_int($key)) {
  313. $this->remove($rule);
  314. } else {
  315. $this->remove($key, $rule);
  316. }
  317. }
  318. } else {
  319. if (is_string($rule)) {
  320. $rule = explode('|', $rule);
  321. }
  322. $this->remove[$field] = $rule;
  323. }
  324. return $this;
  325. }
  326. /**
  327. * 追加某个字段的验证规则
  328. * @access public
  329. * @param string|array $field 字段名
  330. * @param mixed $rule 验证规则
  331. * @return $this
  332. */
  333. public function append($field, $rule = null)
  334. {
  335. if (is_array($field)) {
  336. foreach ($field as $key => $rule) {
  337. $this->append($key, $rule);
  338. }
  339. } else {
  340. if (is_string($rule)) {
  341. $rule = explode('|', $rule);
  342. }
  343. $this->append[$field] = $rule;
  344. }
  345. return $this;
  346. }
  347. /**
  348. * 数据自动验证
  349. * @access public
  350. * @param array $data 数据
  351. * @param mixed $rules 验证规则
  352. * @param string $scene 验证场景
  353. * @return bool
  354. */
  355. public function check($data, $rules = [], $scene = '')
  356. {
  357. $this->error = [];
  358. if (empty($rules)) {
  359. // 读取验证规则
  360. $rules = $this->rule;
  361. }
  362. // 获取场景定义
  363. $this->getScene($scene);
  364. foreach ($this->append as $key => $rule) {
  365. if (!isset($rules[$key])) {
  366. $rules[$key] = $rule;
  367. unset($this->append[$key]);
  368. }
  369. }
  370. foreach ($rules as $key => $rule) {
  371. // field => 'rule1|rule2...' field => ['rule1','rule2',...]
  372. if (strpos($key, '|')) {
  373. // 字段|描述 用于指定属性名称
  374. list($key, $title) = explode('|', $key);
  375. } else {
  376. $title = isset($this->field[$key]) ? $this->field[$key] : $key;
  377. }
  378. // 场景检测
  379. if (!empty($this->only) && !in_array($key, $this->only)) {
  380. continue;
  381. }
  382. // 获取数据 支持多维数组
  383. $value = $this->getDataValue($data, $key);
  384. // 字段验证
  385. if ($rule instanceof \Closure) {
  386. $result = call_user_func_array($rule, [$value, $data, $title, $this]);
  387. } elseif ($rule instanceof ValidateRule) {
  388. // 验证因子
  389. $result = $this->checkItem($key, $value, $rule->getRule(), $data, $rule->getTitle() ?: $title, $rule->getMsg());
  390. } else {
  391. $result = $this->checkItem($key, $value, $rule, $data, $title);
  392. }
  393. if (true !== $result) {
  394. // 没有返回true 则表示验证失败
  395. if (!empty($this->batch)) {
  396. // 批量验证
  397. if (is_array($result)) {
  398. $this->error = array_merge($this->error, $result);
  399. } else {
  400. $this->error[$key] = $result;
  401. }
  402. } else {
  403. $this->error = $result;
  404. return false;
  405. }
  406. }
  407. }
  408. return !empty($this->error) ? false : true;
  409. }
  410. /**
  411. * 根据验证规则验证数据
  412. * @access public
  413. * @param mixed $value 字段值
  414. * @param mixed $rules 验证规则
  415. * @return bool
  416. */
  417. public function checkRule($value, $rules)
  418. {
  419. if ($rules instanceof \Closure) {
  420. return call_user_func_array($rules, [$value]);
  421. } elseif ($rules instanceof ValidateRule) {
  422. $rules = $rules->getRule();
  423. } elseif (is_string($rules)) {
  424. $rules = explode('|', $rules);
  425. }
  426. foreach ($rules as $key => $rule) {
  427. if ($rule instanceof \Closure) {
  428. $result = call_user_func_array($rule, [$value]);
  429. } else {
  430. // 判断验证类型
  431. list($type, $rule) = $this->getValidateType($key, $rule);
  432. $callback = isset(self::$type[$type]) ? self::$type[$type] : [$this, $type];
  433. $result = call_user_func_array($callback, [$value, $rule]);
  434. }
  435. if (true !== $result) {
  436. return $result;
  437. }
  438. }
  439. return true;
  440. }
  441. /**
  442. * 验证单个字段规则
  443. * @access protected
  444. * @param string $field 字段名
  445. * @param mixed $value 字段值
  446. * @param mixed $rules 验证规则
  447. * @param array $data 数据
  448. * @param string $title 字段描述
  449. * @param array $msg 提示信息
  450. * @return mixed
  451. */
  452. protected function checkItem($field, $value, $rules, $data, $title = '', $msg = [])
  453. {
  454. if (isset($this->remove[$field]) && true === $this->remove[$field] && empty($this->append[$field])) {
  455. // 字段已经移除 无需验证
  456. return true;
  457. }
  458. // 支持多规则验证 require|in:a,b,c|... 或者 ['require','in'=>'a,b,c',...]
  459. if (is_string($rules)) {
  460. $rules = explode('|', $rules);
  461. }
  462. if (isset($this->append[$field])) {
  463. // 追加额外的验证规则
  464. $rules = array_unique(array_merge($rules, $this->append[$field]), SORT_REGULAR);
  465. unset($this->append[$field]);
  466. }
  467. $i = 0;
  468. $result = true;
  469. foreach ($rules as $key => $rule) {
  470. if ($rule instanceof \Closure) {
  471. $result = call_user_func_array($rule, [$value, $data]);
  472. $info = is_numeric($key) ? '' : $key;
  473. } else {
  474. // 判断验证类型
  475. list($type, $rule, $info) = $this->getValidateType($key, $rule);
  476. if (isset($this->append[$field]) && in_array($info, $this->append[$field])) {
  477. } elseif (array_key_exists($field, $this->remove) && (null === $this->remove[$field] || in_array($info, $this->remove[$field]))) {
  478. // 规则已经移除
  479. $i++;
  480. continue;
  481. }
  482. // 验证类型
  483. if (isset(self::$type[$type])) {
  484. $result = call_user_func_array(self::$type[$type], [$value, $rule, $data, $field, $title]);
  485. } elseif ('must' == $info || 0 === strpos($info, 'require') || (!is_null($value) && '' !== $value)) {
  486. // 验证数据
  487. $result = call_user_func_array([$this, $type], [$value, $rule, $data, $field, $title]);
  488. } else {
  489. $result = true;
  490. }
  491. }
  492. if (false === $result) {
  493. // 验证失败 返回错误信息
  494. if (!empty($msg[$i])) {
  495. $message = $msg[$i];
  496. if (is_string($message) && strpos($message, '{%') === 0) {
  497. $message = facade\Lang::get(substr($message, 2, -1));
  498. }
  499. } else {
  500. $message = $this->getRuleMsg($field, $title, $info, $rule);
  501. }
  502. return $message;
  503. } elseif (true !== $result) {
  504. // 返回自定义错误信息
  505. if (is_string($result) && false !== strpos($result, ':')) {
  506. $result = str_replace(':attribute', $title, $result);
  507. if (strpos($result, ':rule') && is_scalar($rule)) {
  508. $result = str_replace(':rule', (string) $rule, $result);
  509. }
  510. }
  511. return $result;
  512. }
  513. $i++;
  514. }
  515. return $result;
  516. }
  517. /**
  518. * 获取当前验证类型及规则
  519. * @access public
  520. * @param mixed $key
  521. * @param mixed $rule
  522. * @return array
  523. */
  524. protected function getValidateType($key, $rule)
  525. {
  526. // 判断验证类型
  527. if (!is_numeric($key)) {
  528. return [$key, $rule, $key];
  529. }
  530. if (strpos($rule, ':')) {
  531. list($type, $rule) = explode(':', $rule, 2);
  532. if (isset($this->alias[$type])) {
  533. // 判断别名
  534. $type = $this->alias[$type];
  535. }
  536. $info = $type;
  537. } elseif (method_exists($this, $rule)) {
  538. $type = $rule;
  539. $info = $rule;
  540. $rule = '';
  541. } else {
  542. $type = 'is';
  543. $info = $rule;
  544. }
  545. return [$type, $rule, $info];
  546. }
  547. /**
  548. * 验证是否和某个字段的值一致
  549. * @access public
  550. * @param mixed $value 字段值
  551. * @param mixed $rule 验证规则
  552. * @param array $data 数据
  553. * @param string $field 字段名
  554. * @return bool
  555. */
  556. public function confirm($value, $rule, $data = [], $field = '')
  557. {
  558. if ('' == $rule) {
  559. if (strpos($field, '_confirm')) {
  560. $rule = strstr($field, '_confirm', true);
  561. } else {
  562. $rule = $field . '_confirm';
  563. }
  564. }
  565. return $this->getDataValue($data, $rule) === $value;
  566. }
  567. /**
  568. * 验证是否和某个字段的值是否不同
  569. * @access public
  570. * @param mixed $value 字段值
  571. * @param mixed $rule 验证规则
  572. * @param array $data 数据
  573. * @return bool
  574. */
  575. public function different($value, $rule, $data = [])
  576. {
  577. return $this->getDataValue($data, $rule) != $value;
  578. }
  579. /**
  580. * 验证是否大于等于某个值
  581. * @access public
  582. * @param mixed $value 字段值
  583. * @param mixed $rule 验证规则
  584. * @param array $data 数据
  585. * @return bool
  586. */
  587. public function egt($value, $rule, $data = [])
  588. {
  589. return $value >= $this->getDataValue($data, $rule);
  590. }
  591. /**
  592. * 验证是否大于某个值
  593. * @access public
  594. * @param mixed $value 字段值
  595. * @param mixed $rule 验证规则
  596. * @param array $data 数据
  597. * @return bool
  598. */
  599. public function gt($value, $rule, $data)
  600. {
  601. return $value > $this->getDataValue($data, $rule);
  602. }
  603. /**
  604. * 验证是否小于等于某个值
  605. * @access public
  606. * @param mixed $value 字段值
  607. * @param mixed $rule 验证规则
  608. * @param array $data 数据
  609. * @return bool
  610. */
  611. public function elt($value, $rule, $data = [])
  612. {
  613. return $value <= $this->getDataValue($data, $rule);
  614. }
  615. /**
  616. * 验证是否小于某个值
  617. * @access public
  618. * @param mixed $value 字段值
  619. * @param mixed $rule 验证规则
  620. * @param array $data 数据
  621. * @return bool
  622. */
  623. public function lt($value, $rule, $data = [])
  624. {
  625. return $value < $this->getDataValue($data, $rule);
  626. }
  627. /**
  628. * 验证是否等于某个值
  629. * @access public
  630. * @param mixed $value 字段值
  631. * @param mixed $rule 验证规则
  632. * @return bool
  633. */
  634. public function eq($value, $rule)
  635. {
  636. return $value == $rule;
  637. }
  638. /**
  639. * 必须验证
  640. * @access public
  641. * @param mixed $value 字段值
  642. * @param mixed $rule 验证规则
  643. * @return bool
  644. */
  645. public function must($value, $rule = null)
  646. {
  647. return !empty($value) || '0' == $value;
  648. }
  649. /**
  650. * 验证字段值是否为有效格式
  651. * @access public
  652. * @param mixed $value 字段值
  653. * @param string $rule 验证规则
  654. * @param array $data 验证数据
  655. * @return bool
  656. */
  657. public function is($value, $rule, $data = [])
  658. {
  659. switch (Loader::parseName($rule, 1, false)) {
  660. case 'require':
  661. // 必须
  662. $result = !empty($value) || '0' == $value;
  663. break;
  664. case 'accepted':
  665. // 接受
  666. $result = in_array($value, ['1', 'on', 'yes']);
  667. break;
  668. case 'date':
  669. // 是否是一个有效日期
  670. $result = false !== strtotime($value);
  671. break;
  672. case 'activeUrl':
  673. // 是否为有效的网址
  674. $result = checkdnsrr($value);
  675. break;
  676. case 'boolean':
  677. case 'bool':
  678. // 是否为布尔值
  679. $result = in_array($value, [true, false, 0, 1, '0', '1'], true);
  680. break;
  681. case 'number':
  682. $result = ctype_digit((string) $value);
  683. break;
  684. case 'alphaNum':
  685. $result = ctype_alnum($value);
  686. break;
  687. case 'array':
  688. // 是否为数组
  689. $result = is_array($value);
  690. break;
  691. case 'file':
  692. $result = $value instanceof File;
  693. break;
  694. case 'image':
  695. $result = $value instanceof File && in_array($this->getImageType($value->getRealPath()), [1, 2, 3, 6]);
  696. break;
  697. case 'token':
  698. $result = $this->token($value, '__token__', $data);
  699. break;
  700. default:
  701. if (isset(self::$type[$rule])) {
  702. // 注册的验证规则
  703. $result = call_user_func_array(self::$type[$rule], [$value]);
  704. } elseif (function_exists('ctype_' . $rule)) {
  705. // ctype验证规则
  706. $ctypeFun = 'ctype_' . $rule;
  707. $result = $ctypeFun($value);
  708. } elseif (isset($this->filter[$rule])) {
  709. // Filter_var验证规则
  710. $result = $this->filter($value, $this->filter[$rule]);
  711. } else {
  712. // 正则验证
  713. $result = $this->regex($value, $rule);
  714. }
  715. }
  716. return $result;
  717. }
  718. // 判断图像类型
  719. protected function getImageType($image)
  720. {
  721. if (function_exists('exif_imagetype')) {
  722. return exif_imagetype($image);
  723. }
  724. try {
  725. $info = getimagesize($image);
  726. return $info ? $info[2] : false;
  727. } catch (\Exception $e) {
  728. return false;
  729. }
  730. }
  731. /**
  732. * 验证是否为合格的域名或者IP 支持A,MX,NS,SOA,PTR,CNAME,AAAA,A6, SRV,NAPTR,TXT 或者 ANY类型
  733. * @access public
  734. * @param mixed $value 字段值
  735. * @param mixed $rule 验证规则
  736. * @return bool
  737. */
  738. public function activeUrl($value, $rule = 'MX')
  739. {
  740. if (!in_array($rule, ['A', 'MX', 'NS', 'SOA', 'PTR', 'CNAME', 'AAAA', 'A6', 'SRV', 'NAPTR', 'TXT', 'ANY'])) {
  741. $rule = 'MX';
  742. }
  743. return checkdnsrr($value, $rule);
  744. }
  745. /**
  746. * 验证是否有效IP
  747. * @access public
  748. * @param mixed $value 字段值
  749. * @param mixed $rule 验证规则 ipv4 ipv6
  750. * @return bool
  751. */
  752. public function ip($value, $rule = 'ipv4')
  753. {
  754. if (!in_array($rule, ['ipv4', 'ipv6'])) {
  755. $rule = 'ipv4';
  756. }
  757. return $this->filter($value, [FILTER_VALIDATE_IP, 'ipv6' == $rule ? FILTER_FLAG_IPV6 : FILTER_FLAG_IPV4]);
  758. }
  759. /**
  760. * 验证上传文件后缀
  761. * @access public
  762. * @param mixed $file 上传文件
  763. * @param mixed $rule 验证规则
  764. * @return bool
  765. */
  766. public function fileExt($file, $rule)
  767. {
  768. if (is_array($file)) {
  769. foreach ($file as $item) {
  770. if (!($item instanceof File) || !$item->checkExt($rule)) {
  771. return false;
  772. }
  773. }
  774. return true;
  775. } elseif ($file instanceof File) {
  776. return $file->checkExt($rule);
  777. }
  778. return false;
  779. }
  780. /**
  781. * 验证上传文件类型
  782. * @access public
  783. * @param mixed $file 上传文件
  784. * @param mixed $rule 验证规则
  785. * @return bool
  786. */
  787. public function fileMime($file, $rule)
  788. {
  789. if (is_array($file)) {
  790. foreach ($file as $item) {
  791. if (!($item instanceof File) || !$item->checkMime($rule)) {
  792. return false;
  793. }
  794. }
  795. return true;
  796. } elseif ($file instanceof File) {
  797. return $file->checkMime($rule);
  798. }
  799. return false;
  800. }
  801. /**
  802. * 验证上传文件大小
  803. * @access public
  804. * @param mixed $file 上传文件
  805. * @param mixed $rule 验证规则
  806. * @return bool
  807. */
  808. public function fileSize($file, $rule)
  809. {
  810. if (is_array($file)) {
  811. foreach ($file as $item) {
  812. if (!($item instanceof File) || !$item->checkSize($rule)) {
  813. return false;
  814. }
  815. }
  816. return true;
  817. } elseif ($file instanceof File) {
  818. return $file->checkSize($rule);
  819. }
  820. return false;
  821. }
  822. /**
  823. * 验证图片的宽高及类型
  824. * @access public
  825. * @param mixed $file 上传文件
  826. * @param mixed $rule 验证规则
  827. * @return bool
  828. */
  829. public function image($file, $rule)
  830. {
  831. if (!($file instanceof File)) {
  832. return false;
  833. }
  834. if ($rule) {
  835. $rule = explode(',', $rule);
  836. list($width, $height, $type) = getimagesize($file->getRealPath());
  837. if (isset($rule[2])) {
  838. $imageType = strtolower($rule[2]);
  839. if ('jpg' == $imageType) {
  840. $imageType = 'jpeg';
  841. }
  842. if (image_type_to_extension($type, false) != $imageType) {
  843. return false;
  844. }
  845. }
  846. list($w, $h) = $rule;
  847. return $w == $width && $h == $height;
  848. }
  849. return in_array($this->getImageType($file->getRealPath()), [1, 2, 3, 6]);
  850. }
  851. /**
  852. * 验证请求类型
  853. * @access public
  854. * @param mixed $value 字段值
  855. * @param mixed $rule 验证规则
  856. * @return bool
  857. */
  858. public function method($value, $rule)
  859. {
  860. $method = Container::pull('request')->method();
  861. return strtoupper($rule) == $method;
  862. }
  863. /**
  864. * 验证时间和日期是否符合指定格式
  865. * @access public
  866. * @param mixed $value 字段值
  867. * @param mixed $rule 验证规则
  868. * @return bool
  869. */
  870. public function dateFormat($value, $rule)
  871. {
  872. $info = date_parse_from_format($rule, $value);
  873. return 0 == $info['warning_count'] && 0 == $info['error_count'];
  874. }
  875. /**
  876. * 验证是否唯一
  877. * @access public
  878. * @param mixed $value 字段值
  879. * @param mixed $rule 验证规则 格式:数据表,字段名,排除ID,主键名
  880. * @param array $data 数据
  881. * @param string $field 验证字段名
  882. * @return bool
  883. */
  884. public function unique($value, $rule, $data, $field)
  885. {
  886. if (is_string($rule)) {
  887. $rule = explode(',', $rule);
  888. }
  889. if (false !== strpos($rule[0], '\\')) {
  890. // 指定模型类
  891. $db = new $rule[0];
  892. } else {
  893. $db = Db::name($rule[0]);
  894. }
  895. $key = isset($rule[1]) ? $rule[1] : $field;
  896. if (strpos($key, '^')) {
  897. // 支持多个字段验证
  898. $fields = explode('^', $key);
  899. foreach ($fields as $key) {
  900. if (isset($data[$key])) {
  901. $map[] = [$key, '=', $data[$key]];
  902. }
  903. }
  904. } elseif (strpos($key, '=')) {
  905. parse_str($key, $map);
  906. } elseif (isset($data[$field])) {
  907. $map[] = [$key, '=', $data[$field]];
  908. } else {
  909. $map = [];
  910. }
  911. $pk = !empty($rule[3]) ? $rule[3] : $db->getPk();
  912. if (is_string($pk)) {
  913. if (isset($rule[2])) {
  914. $map[] = [$pk, '<>', $rule[2]];
  915. } elseif (isset($data[$pk])) {
  916. $map[] = [$pk, '<>', $data[$pk]];
  917. }
  918. }
  919. if ($db->where($map)->field($pk)->find()) {
  920. return false;
  921. }
  922. return true;
  923. }
  924. /**
  925. * 使用行为类验证
  926. * @access public
  927. * @param mixed $value 字段值
  928. * @param mixed $rule 验证规则
  929. * @param array $data 数据
  930. * @return mixed
  931. */
  932. public function behavior($value, $rule, $data)
  933. {
  934. return Container::pull('hook')->exec($rule, $data);
  935. }
  936. /**
  937. * 使用filter_var方式验证
  938. * @access public
  939. * @param mixed $value 字段值
  940. * @param mixed $rule 验证规则
  941. * @return bool
  942. */
  943. public function filter($value, $rule)
  944. {
  945. if (is_string($rule) && strpos($rule, ',')) {
  946. list($rule, $param) = explode(',', $rule);
  947. } elseif (is_array($rule)) {
  948. $param = $rule[1] ?? 0;
  949. $rule = $rule[0];
  950. } else {
  951. $param = 0;
  952. }
  953. return false !== filter_var($value, is_int($rule) ? $rule : filter_id($rule), $param);
  954. }
  955. /**
  956. * 验证某个字段等于某个值的时候必须
  957. * @access public
  958. * @param mixed $value 字段值
  959. * @param mixed $rule 验证规则
  960. * @param array $data 数据
  961. * @return bool
  962. */
  963. public function requireIf($value, $rule, $data)
  964. {
  965. list($field, $val) = explode(',', $rule);
  966. if ($this->getDataValue($data, $field) == $val) {
  967. return !empty($value) || '0' == $value;
  968. }
  969. return true;
  970. }
  971. /**
  972. * 通过回调方法验证某个字段是否必须
  973. * @access public
  974. * @param mixed $value 字段值
  975. * @param mixed $rule 验证规则
  976. * @param array $data 数据
  977. * @return bool
  978. */
  979. public function requireCallback($value, $rule, $data)
  980. {
  981. $result = call_user_func_array([$this, $rule], [$value, $data]);
  982. if ($result) {
  983. return !empty($value) || '0' == $value;
  984. }
  985. return true;
  986. }
  987. /**
  988. * 验证某个字段有值的情况下必须
  989. * @access public
  990. * @param mixed $value 字段值
  991. * @param mixed $rule 验证规则
  992. * @param array $data 数据
  993. * @return bool
  994. */
  995. public function requireWith($value, $rule, $data)
  996. {
  997. $val = $this->getDataValue($data, $rule);
  998. if (!empty($val)) {
  999. return !empty($value) || '0' == $value;
  1000. }
  1001. return true;
  1002. }
  1003. /**
  1004. * 验证是否在范围内
  1005. * @access public
  1006. * @param mixed $value 字段值
  1007. * @param mixed $rule 验证规则
  1008. * @return bool
  1009. */
  1010. public function in($value, $rule)
  1011. {
  1012. return in_array($value, is_array($rule) ? $rule : explode(',', $rule));
  1013. }
  1014. /**
  1015. * 验证是否不在某个范围
  1016. * @access public
  1017. * @param mixed $value 字段值
  1018. * @param mixed $rule 验证规则
  1019. * @return bool
  1020. */
  1021. public function notIn($value, $rule)
  1022. {
  1023. return !in_array($value, is_array($rule) ? $rule : explode(',', $rule));
  1024. }
  1025. /**
  1026. * between验证数据
  1027. * @access public
  1028. * @param mixed $value 字段值
  1029. * @param mixed $rule 验证规则
  1030. * @return bool
  1031. */
  1032. public function between($value, $rule)
  1033. {
  1034. if (is_string($rule)) {
  1035. $rule = explode(',', $rule);
  1036. }
  1037. list($min, $max) = $rule;
  1038. return $value >= $min && $value <= $max;
  1039. }
  1040. /**
  1041. * 使用notbetween验证数据
  1042. * @access public
  1043. * @param mixed $value 字段值
  1044. * @param mixed $rule 验证规则
  1045. * @return bool
  1046. */
  1047. public function notBetween($value, $rule)
  1048. {
  1049. if (is_string($rule)) {
  1050. $rule = explode(',', $rule);
  1051. }
  1052. list($min, $max) = $rule;
  1053. return $value < $min || $value > $max;
  1054. }
  1055. /**
  1056. * 验证数据长度
  1057. * @access public
  1058. * @param mixed $value 字段值
  1059. * @param mixed $rule 验证规则
  1060. * @return bool
  1061. */
  1062. public function length($value, $rule)
  1063. {
  1064. if (is_array($value)) {
  1065. $length = count($value);
  1066. } elseif ($value instanceof File) {
  1067. $length = $value->getSize();
  1068. } else {
  1069. $length = mb_strlen((string) $value);
  1070. }
  1071. if (strpos($rule, ',')) {
  1072. // 长度区间
  1073. list($min, $max) = explode(',', $rule);
  1074. return $length >= $min && $length <= $max;
  1075. }
  1076. // 指定长度
  1077. return $length == $rule;
  1078. }
  1079. /**
  1080. * 验证数据最大长度
  1081. * @access public
  1082. * @param mixed $value 字段值
  1083. * @param mixed $rule 验证规则
  1084. * @return bool
  1085. */
  1086. public function max($value, $rule)
  1087. {
  1088. if (is_array($value)) {
  1089. $length = count($value);
  1090. } elseif ($value instanceof File) {
  1091. $length = $value->getSize();
  1092. } else {
  1093. $length = mb_strlen((string) $value);
  1094. }
  1095. return $length <= $rule;
  1096. }
  1097. /**
  1098. * 验证数据最小长度
  1099. * @access public
  1100. * @param mixed $value 字段值
  1101. * @param mixed $rule 验证规则
  1102. * @return bool
  1103. */
  1104. public function min($value, $rule)
  1105. {
  1106. if (is_array($value)) {
  1107. $length = count($value);
  1108. } elseif ($value instanceof File) {
  1109. $length = $value->getSize();
  1110. } else {
  1111. $length = mb_strlen((string) $value);
  1112. }
  1113. return $length >= $rule;
  1114. }
  1115. /**
  1116. * 验证日期
  1117. * @access public
  1118. * @param mixed $value 字段值
  1119. * @param mixed $rule 验证规则
  1120. * @param array $data 数据
  1121. * @return bool
  1122. */
  1123. public function after($value, $rule, $data)
  1124. {
  1125. return strtotime($value) >= strtotime($rule);
  1126. }
  1127. /**
  1128. * 验证日期
  1129. * @access public
  1130. * @param mixed $value 字段值
  1131. * @param mixed $rule 验证规则
  1132. * @param array $data 数据
  1133. * @return bool
  1134. */
  1135. public function before($value, $rule, $data)
  1136. {
  1137. return strtotime($value) <= strtotime($rule);
  1138. }
  1139. /**
  1140. * 验证日期字段
  1141. * @access protected
  1142. * @param mixed $value 字段值
  1143. * @param mixed $rule 验证规则
  1144. * @param array $data 数据
  1145. * @return bool
  1146. */
  1147. protected function afterWith($value, $rule, $data)
  1148. {
  1149. $rule = $this->getDataValue($data, $rule);
  1150. return !is_null($rule) && strtotime($value) >= strtotime($rule);
  1151. }
  1152. /**
  1153. * 验证日期字段
  1154. * @access protected
  1155. * @param mixed $value 字段值
  1156. * @param mixed $rule 验证规则
  1157. * @param array $data 数据
  1158. * @return bool
  1159. */
  1160. protected function beforeWith($value, $rule, $data)
  1161. {
  1162. $rule = $this->getDataValue($data, $rule);
  1163. return !is_null($rule) && strtotime($value) <= strtotime($rule);
  1164. }
  1165. /**
  1166. * 验证有效期
  1167. * @access public
  1168. * @param mixed $value 字段值
  1169. * @param mixed $rule 验证规则
  1170. * @return bool
  1171. */
  1172. public function expire($value, $rule)
  1173. {
  1174. if (is_string($rule)) {
  1175. $rule = explode(',', $rule);
  1176. }
  1177. list($start, $end) = $rule;
  1178. if (!is_numeric($start)) {
  1179. $start = strtotime($start);
  1180. }
  1181. if (!is_numeric($end)) {
  1182. $end = strtotime($end);
  1183. }
  1184. return $_SERVER['REQUEST_TIME'] >= $start && $_SERVER['REQUEST_TIME'] <= $end;
  1185. }
  1186. /**
  1187. * 验证IP许可
  1188. * @access public
  1189. * @param string $value 字段值
  1190. * @param mixed $rule 验证规则
  1191. * @return mixed
  1192. */
  1193. public function allowIp($value, $rule)
  1194. {
  1195. return in_array($value, is_array($rule) ? $rule : explode(',', $rule));
  1196. }
  1197. /**
  1198. * 验证IP禁用
  1199. * @access public
  1200. * @param string $value 字段值
  1201. * @param mixed $rule 验证规则
  1202. * @return mixed
  1203. */
  1204. public function denyIp($value, $rule)
  1205. {
  1206. return !in_array($value, is_array($rule) ? $rule : explode(',', $rule));
  1207. }
  1208. /**
  1209. * 使用正则验证数据
  1210. * @access public
  1211. * @param mixed $value 字段值
  1212. * @param mixed $rule 验证规则 正则规则或者预定义正则名
  1213. * @return bool
  1214. */
  1215. public function regex($value, $rule)
  1216. {
  1217. if (isset($this->regex[$rule])) {
  1218. $rule = $this->regex[$rule];
  1219. } elseif (isset($this->defaultRegex[$rule])) {
  1220. $rule = $this->defaultRegex[$rule];
  1221. }
  1222. if (0 !== strpos($rule, '/') && !preg_match('/\/[imsU]{0,4}$/', $rule)) {
  1223. // 不是正则表达式则两端补上/
  1224. $rule = '/^' . $rule . '$/';
  1225. }
  1226. return is_scalar($value) && 1 === preg_match($rule, (string) $value);
  1227. }
  1228. /**
  1229. * 验证表单令牌
  1230. * @access public
  1231. * @param mixed $value 字段值
  1232. * @param mixed $rule 验证规则
  1233. * @param array $data 数据
  1234. * @return bool
  1235. */
  1236. public function token($value, $rule, $data)
  1237. {
  1238. $rule = !empty($rule) ? $rule : '__token__';
  1239. $session = Container::pull('session');
  1240. if (!isset($data[$rule]) || !$session->has($rule)) {
  1241. // 令牌数据无效
  1242. return false;
  1243. }
  1244. // 令牌验证
  1245. if (isset($data[$rule]) && $session->get($rule) === $data[$rule]) {
  1246. // 防止重复提交
  1247. $session->delete($rule); // 验证完成销毁session
  1248. return true;
  1249. }
  1250. // 开启TOKEN重置
  1251. $session->delete($rule);
  1252. return false;
  1253. }
  1254. // 获取错误信息
  1255. public function getError()
  1256. {
  1257. return $this->error;
  1258. }
  1259. /**
  1260. * 获取数据值
  1261. * @access protected
  1262. * @param array $data 数据
  1263. * @param string $key 数据标识 支持多维
  1264. * @return mixed
  1265. */
  1266. protected function getDataValue($data, $key)
  1267. {
  1268. if (is_numeric($key)) {
  1269. $value = $key;
  1270. } elseif (strpos($key, '.')) {
  1271. // 支持多维数组验证
  1272. foreach (explode('.', $key) as $key) {
  1273. if (!isset($data[$key])) {
  1274. $value = null;
  1275. break;
  1276. }
  1277. $value = $data = $data[$key];
  1278. }
  1279. } else {
  1280. $value = isset($data[$key]) ? $data[$key] : null;
  1281. }
  1282. return $value;
  1283. }
  1284. /**
  1285. * 获取验证规则的错误提示信息
  1286. * @access protected
  1287. * @param string $attribute 字段英文名
  1288. * @param string $title 字段描述名
  1289. * @param string $type 验证规则名称
  1290. * @param mixed $rule 验证规则数据
  1291. * @return string
  1292. */
  1293. protected function getRuleMsg($attribute, $title, $type, $rule)
  1294. {
  1295. $lang = Container::pull('lang');
  1296. if (isset($this->message[$attribute . '.' . $type])) {
  1297. $msg = $this->message[$attribute . '.' . $type];
  1298. } elseif (isset($this->message[$attribute][$type])) {
  1299. $msg = $this->message[$attribute][$type];
  1300. } elseif (isset($this->message[$attribute])) {
  1301. $msg = $this->message[$attribute];
  1302. } elseif (isset(self::$typeMsg[$type])) {
  1303. $msg = self::$typeMsg[$type];
  1304. } elseif (0 === strpos($type, 'require')) {
  1305. $msg = self::$typeMsg['require'];
  1306. } else {
  1307. $msg = $title . $lang->get('not conform to the rules');
  1308. }
  1309. if (!is_string($msg)) {
  1310. return $msg;
  1311. }
  1312. if (0 === strpos($msg, '{%')) {
  1313. $msg = $lang->get(substr($msg, 2, -1));
  1314. } elseif ($lang->has($msg)) {
  1315. $msg = $lang->get($msg);
  1316. }
  1317. if (is_scalar($rule) && false !== strpos($msg, ':')) {
  1318. // 变量替换
  1319. if (is_string($rule) && strpos($rule, ',')) {
  1320. $array = array_pad(explode(',', $rule), 3, '');
  1321. } else {
  1322. $array = array_pad([], 3, '');
  1323. }
  1324. $msg = str_replace(
  1325. [':attribute', ':1', ':2', ':3'],
  1326. [$title, $array[0], $array[1], $array[2]],
  1327. $msg);
  1328. if (strpos($msg, ':rule')) {
  1329. $msg = str_replace(':rule', (string) $rule, $msg);
  1330. }
  1331. }
  1332. return $msg;
  1333. }
  1334. /**
  1335. * 获取数据验证的场景
  1336. * @access protected
  1337. * @param string $scene 验证场景
  1338. * @return void
  1339. */
  1340. protected function getScene($scene = '')
  1341. {
  1342. if (empty($scene)) {
  1343. // 读取指定场景
  1344. $scene = $this->currentScene;
  1345. }
  1346. $this->only = $this->append = $this->remove = [];
  1347. if (empty($scene)) {
  1348. return;
  1349. }
  1350. if (method_exists($this, 'scene' . $scene)) {
  1351. call_user_func([$this, 'scene' . $scene]);
  1352. } elseif (isset($this->scene[$scene])) {
  1353. // 如果设置了验证适用场景
  1354. $scene = $this->scene[$scene];
  1355. if (is_string($scene)) {
  1356. $scene = explode(',', $scene);
  1357. }
  1358. $this->only = $scene;
  1359. }
  1360. }
  1361. /**
  1362. * 动态方法 直接调用is方法进行验证
  1363. * @access public
  1364. * @param string $method 方法名
  1365. * @param array $args 调用参数
  1366. * @return bool
  1367. */
  1368. public function __call($method, $args)
  1369. {
  1370. if ('is' == strtolower(substr($method, 0, 2))) {
  1371. $method = substr($method, 2);
  1372. }
  1373. array_push($args, lcfirst($method));
  1374. return call_user_func_array([$this, 'is'], $args);
  1375. }
  1376. }