Validate.php 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554
  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\exception\ClassNotFoundException;
  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}]+$/u',
  125. 'chsAlpha' => '/^[\x{4e00}-\x{9fa5}a-zA-Z]+$/u',
  126. 'chsAlphaNum' => '/^[\x{4e00}-\x{9fa5}a-zA-Z0-9]+$/u',
  127. 'chsDash' => '/^[\x{4e00}-\x{9fa5}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. }
  368. }
  369. foreach ($rules as $key => $rule) {
  370. // field => 'rule1|rule2...' field => ['rule1','rule2',...]
  371. if (strpos($key, '|')) {
  372. // 字段|描述 用于指定属性名称
  373. list($key, $title) = explode('|', $key);
  374. } else {
  375. $title = isset($this->field[$key]) ? $this->field[$key] : $key;
  376. }
  377. // 场景检测
  378. if (!empty($this->only) && !in_array($key, $this->only)) {
  379. continue;
  380. }
  381. // 获取数据 支持多维数组
  382. $value = $this->getDataValue($data, $key);
  383. // 字段验证
  384. if ($rule instanceof \Closure) {
  385. $result = call_user_func_array($rule, [$value, $data, $title, $this]);
  386. } elseif ($rule instanceof ValidateRule) {
  387. // 验证因子
  388. $result = $this->checkItem($key, $value, $rule->getRule(), $data, $rule->getTitle() ?: $title, $rule->getMsg());
  389. } else {
  390. $result = $this->checkItem($key, $value, $rule, $data, $title);
  391. }
  392. if (true !== $result) {
  393. // 没有返回true 则表示验证失败
  394. if (!empty($this->batch)) {
  395. // 批量验证
  396. if (is_array($result)) {
  397. $this->error = array_merge($this->error, $result);
  398. } else {
  399. $this->error[$key] = $result;
  400. }
  401. } else {
  402. $this->error = $result;
  403. return false;
  404. }
  405. }
  406. }
  407. return !empty($this->error) ? false : true;
  408. }
  409. /**
  410. * 根据验证规则验证数据
  411. * @access public
  412. * @param mixed $value 字段值
  413. * @param mixed $rules 验证规则
  414. * @return bool
  415. */
  416. public function checkRule($value, $rules)
  417. {
  418. if ($rules instanceof \Closure) {
  419. return call_user_func_array($rules, [$value]);
  420. } elseif ($rules instanceof ValidateRule) {
  421. $rules = $rules->getRule();
  422. } elseif (is_string($rules)) {
  423. $rules = explode('|', $rules);
  424. }
  425. foreach ($rules as $key => $rule) {
  426. if ($rule instanceof \Closure) {
  427. $result = call_user_func_array($rule, [$value]);
  428. } else {
  429. // 判断验证类型
  430. list($type, $rule) = $this->getValidateType($key, $rule);
  431. $callback = isset(self::$type[$type]) ? self::$type[$type] : [$this, $type];
  432. $result = call_user_func_array($callback, [$value, $rule]);
  433. }
  434. if (true !== $result) {
  435. return $result;
  436. }
  437. }
  438. return true;
  439. }
  440. /**
  441. * 验证单个字段规则
  442. * @access protected
  443. * @param string $field 字段名
  444. * @param mixed $value 字段值
  445. * @param mixed $rules 验证规则
  446. * @param array $data 数据
  447. * @param string $title 字段描述
  448. * @param array $msg 提示信息
  449. * @return mixed
  450. */
  451. protected function checkItem($field, $value, $rules, $data, $title = '', $msg = [])
  452. {
  453. if (isset($this->remove[$field]) && true === $this->remove[$field] && empty($this->append[$field])) {
  454. // 字段已经移除 无需验证
  455. return true;
  456. }
  457. // 支持多规则验证 require|in:a,b,c|... 或者 ['require','in'=>'a,b,c',...]
  458. if (is_string($rules)) {
  459. $rules = explode('|', $rules);
  460. }
  461. if (isset($this->append[$field])) {
  462. // 追加额外的验证规则
  463. $rules = array_unique(array_merge($rules, $this->append[$field]), SORT_REGULAR);
  464. }
  465. $i = 0;
  466. $result = true;
  467. foreach ($rules as $key => $rule) {
  468. if ($rule instanceof \Closure) {
  469. $result = call_user_func_array($rule, [$value, $data]);
  470. $info = is_numeric($key) ? '' : $key;
  471. } else {
  472. // 判断验证类型
  473. list($type, $rule, $info) = $this->getValidateType($key, $rule);
  474. if (isset($this->append[$field]) && in_array($info, $this->append[$field])) {
  475. } elseif (array_key_exists($field, $this->remove) && (null === $this->remove[$field] || in_array($info, $this->remove[$field]))) {
  476. // 规则已经移除
  477. $i++;
  478. continue;
  479. }
  480. // 验证类型
  481. if (isset(self::$type[$type])) {
  482. $result = call_user_func_array(self::$type[$type], [$value, $rule, $data, $field, $title]);
  483. } elseif ('must' == $info || 0 === strpos($info, 'require') || (!is_null($value) && '' !== $value)) {
  484. // 验证数据
  485. $result = call_user_func_array([$this, $type], [$value, $rule, $data, $field, $title]);
  486. } else {
  487. $result = true;
  488. }
  489. }
  490. if (false === $result) {
  491. // 验证失败 返回错误信息
  492. if (!empty($msg[$i])) {
  493. $message = $msg[$i];
  494. if (is_string($message) && strpos($message, '{%') === 0) {
  495. $message = facade\Lang::get(substr($message, 2, -1));
  496. }
  497. } else {
  498. $message = $this->getRuleMsg($field, $title, $info, $rule);
  499. }
  500. return $message;
  501. } elseif (true !== $result) {
  502. // 返回自定义错误信息
  503. if (is_string($result) && false !== strpos($result, ':')) {
  504. $result = str_replace(':attribute', $title, $result);
  505. if (strpos($result, ':rule') && is_scalar($rule)) {
  506. $result = str_replace(':rule', (string) $rule, $result);
  507. }
  508. }
  509. return $result;
  510. }
  511. $i++;
  512. }
  513. return $result;
  514. }
  515. /**
  516. * 获取当前验证类型及规则
  517. * @access public
  518. * @param mixed $key
  519. * @param mixed $rule
  520. * @return array
  521. */
  522. protected function getValidateType($key, $rule)
  523. {
  524. // 判断验证类型
  525. if (!is_numeric($key)) {
  526. return [$key, $rule, $key];
  527. }
  528. if (strpos($rule, ':')) {
  529. list($type, $rule) = explode(':', $rule, 2);
  530. if (isset($this->alias[$type])) {
  531. // 判断别名
  532. $type = $this->alias[$type];
  533. }
  534. $info = $type;
  535. } elseif (method_exists($this, $rule)) {
  536. $type = $rule;
  537. $info = $rule;
  538. $rule = '';
  539. } else {
  540. $type = 'is';
  541. $info = $rule;
  542. }
  543. return [$type, $rule, $info];
  544. }
  545. /**
  546. * 验证是否和某个字段的值一致
  547. * @access public
  548. * @param mixed $value 字段值
  549. * @param mixed $rule 验证规则
  550. * @param array $data 数据
  551. * @param string $field 字段名
  552. * @return bool
  553. */
  554. public function confirm($value, $rule, $data = [], $field = '')
  555. {
  556. if ('' == $rule) {
  557. if (strpos($field, '_confirm')) {
  558. $rule = strstr($field, '_confirm', true);
  559. } else {
  560. $rule = $field . '_confirm';
  561. }
  562. }
  563. return $this->getDataValue($data, $rule) === $value;
  564. }
  565. /**
  566. * 验证是否和某个字段的值是否不同
  567. * @access public
  568. * @param mixed $value 字段值
  569. * @param mixed $rule 验证规则
  570. * @param array $data 数据
  571. * @return bool
  572. */
  573. public function different($value, $rule, $data = [])
  574. {
  575. return $this->getDataValue($data, $rule) != $value;
  576. }
  577. /**
  578. * 验证是否大于等于某个值
  579. * @access public
  580. * @param mixed $value 字段值
  581. * @param mixed $rule 验证规则
  582. * @param array $data 数据
  583. * @return bool
  584. */
  585. public function egt($value, $rule, $data = [])
  586. {
  587. return $value >= $this->getDataValue($data, $rule);
  588. }
  589. /**
  590. * 验证是否大于某个值
  591. * @access public
  592. * @param mixed $value 字段值
  593. * @param mixed $rule 验证规则
  594. * @param array $data 数据
  595. * @return bool
  596. */
  597. public function gt($value, $rule, $data)
  598. {
  599. return $value > $this->getDataValue($data, $rule);
  600. }
  601. /**
  602. * 验证是否小于等于某个值
  603. * @access public
  604. * @param mixed $value 字段值
  605. * @param mixed $rule 验证规则
  606. * @param array $data 数据
  607. * @return bool
  608. */
  609. public function elt($value, $rule, $data = [])
  610. {
  611. return $value <= $this->getDataValue($data, $rule);
  612. }
  613. /**
  614. * 验证是否小于某个值
  615. * @access public
  616. * @param mixed $value 字段值
  617. * @param mixed $rule 验证规则
  618. * @param array $data 数据
  619. * @return bool
  620. */
  621. public function lt($value, $rule, $data = [])
  622. {
  623. return $value < $this->getDataValue($data, $rule);
  624. }
  625. /**
  626. * 验证是否等于某个值
  627. * @access public
  628. * @param mixed $value 字段值
  629. * @param mixed $rule 验证规则
  630. * @return bool
  631. */
  632. public function eq($value, $rule)
  633. {
  634. return $value == $rule;
  635. }
  636. /**
  637. * 必须验证
  638. * @access public
  639. * @param mixed $value 字段值
  640. * @param mixed $rule 验证规则
  641. * @return bool
  642. */
  643. public function must($value, $rule = null)
  644. {
  645. return !empty($value) || '0' == $value;
  646. }
  647. /**
  648. * 验证字段值是否为有效格式
  649. * @access public
  650. * @param mixed $value 字段值
  651. * @param string $rule 验证规则
  652. * @param array $data 验证数据
  653. * @return bool
  654. */
  655. public function is($value, $rule, $data = [])
  656. {
  657. switch (Loader::parseName($rule, 1, false)) {
  658. case 'require':
  659. // 必须
  660. $result = !empty($value) || '0' == $value;
  661. break;
  662. case 'accepted':
  663. // 接受
  664. $result = in_array($value, ['1', 'on', 'yes']);
  665. break;
  666. case 'date':
  667. // 是否是一个有效日期
  668. $result = false !== strtotime($value);
  669. break;
  670. case 'activeUrl':
  671. // 是否为有效的网址
  672. $result = checkdnsrr($value);
  673. break;
  674. case 'boolean':
  675. case 'bool':
  676. // 是否为布尔值
  677. $result = in_array($value, [true, false, 0, 1, '0', '1'], true);
  678. break;
  679. case 'number':
  680. $result = ctype_digit((string) $value);
  681. break;
  682. case 'alphaNum':
  683. $result = ctype_alnum($value);
  684. break;
  685. case 'array':
  686. // 是否为数组
  687. $result = is_array($value);
  688. break;
  689. case 'file':
  690. $result = $value instanceof File;
  691. break;
  692. case 'image':
  693. $result = $value instanceof File && in_array($this->getImageType($value->getRealPath()), [1, 2, 3, 6]);
  694. break;
  695. case 'token':
  696. $result = $this->token($value, '__token__', $data);
  697. break;
  698. default:
  699. if (isset(self::$type[$rule])) {
  700. // 注册的验证规则
  701. $result = call_user_func_array(self::$type[$rule], [$value]);
  702. } elseif (function_exists('ctype_' . $rule)) {
  703. // ctype验证规则
  704. $ctypeFun = 'ctype_' . $rule;
  705. $result = $ctypeFun($value);
  706. } elseif (isset($this->filter[$rule])) {
  707. // Filter_var验证规则
  708. $result = $this->filter($value, $this->filter[$rule]);
  709. } else {
  710. // 正则验证
  711. $result = $this->regex($value, $rule);
  712. }
  713. }
  714. return $result;
  715. }
  716. // 判断图像类型
  717. protected function getImageType($image)
  718. {
  719. if (function_exists('exif_imagetype')) {
  720. return exif_imagetype($image);
  721. }
  722. try {
  723. $info = getimagesize($image);
  724. return $info ? $info[2] : false;
  725. } catch (\Exception $e) {
  726. return false;
  727. }
  728. }
  729. /**
  730. * 验证是否为合格的域名或者IP 支持A,MX,NS,SOA,PTR,CNAME,AAAA,A6, SRV,NAPTR,TXT 或者 ANY类型
  731. * @access public
  732. * @param mixed $value 字段值
  733. * @param mixed $rule 验证规则
  734. * @return bool
  735. */
  736. public function activeUrl($value, $rule = 'MX')
  737. {
  738. if (!in_array($rule, ['A', 'MX', 'NS', 'SOA', 'PTR', 'CNAME', 'AAAA', 'A6', 'SRV', 'NAPTR', 'TXT', 'ANY'])) {
  739. $rule = 'MX';
  740. }
  741. return checkdnsrr($value, $rule);
  742. }
  743. /**
  744. * 验证是否有效IP
  745. * @access public
  746. * @param mixed $value 字段值
  747. * @param mixed $rule 验证规则 ipv4 ipv6
  748. * @return bool
  749. */
  750. public function ip($value, $rule = 'ipv4')
  751. {
  752. if (!in_array($rule, ['ipv4', 'ipv6'])) {
  753. $rule = 'ipv4';
  754. }
  755. return $this->filter($value, [FILTER_VALIDATE_IP, 'ipv6' == $rule ? FILTER_FLAG_IPV6 : FILTER_FLAG_IPV4]);
  756. }
  757. /**
  758. * 验证上传文件后缀
  759. * @access public
  760. * @param mixed $file 上传文件
  761. * @param mixed $rule 验证规则
  762. * @return bool
  763. */
  764. public function fileExt($file, $rule)
  765. {
  766. if (is_array($file)) {
  767. foreach ($file as $item) {
  768. if (!($item instanceof File) || !$item->checkExt($rule)) {
  769. return false;
  770. }
  771. }
  772. return true;
  773. } elseif ($file instanceof File) {
  774. return $file->checkExt($rule);
  775. }
  776. return false;
  777. }
  778. /**
  779. * 验证上传文件类型
  780. * @access public
  781. * @param mixed $file 上传文件
  782. * @param mixed $rule 验证规则
  783. * @return bool
  784. */
  785. public function fileMime($file, $rule)
  786. {
  787. if (is_array($file)) {
  788. foreach ($file as $item) {
  789. if (!($item instanceof File) || !$item->checkMime($rule)) {
  790. return false;
  791. }
  792. }
  793. return true;
  794. } elseif ($file instanceof File) {
  795. return $file->checkMime($rule);
  796. }
  797. return false;
  798. }
  799. /**
  800. * 验证上传文件大小
  801. * @access public
  802. * @param mixed $file 上传文件
  803. * @param mixed $rule 验证规则
  804. * @return bool
  805. */
  806. public function fileSize($file, $rule)
  807. {
  808. if (is_array($file)) {
  809. foreach ($file as $item) {
  810. if (!($item instanceof File) || !$item->checkSize($rule)) {
  811. return false;
  812. }
  813. }
  814. return true;
  815. } elseif ($file instanceof File) {
  816. return $file->checkSize($rule);
  817. }
  818. return false;
  819. }
  820. /**
  821. * 验证图片的宽高及类型
  822. * @access public
  823. * @param mixed $file 上传文件
  824. * @param mixed $rule 验证规则
  825. * @return bool
  826. */
  827. public function image($file, $rule)
  828. {
  829. if (!($file instanceof File)) {
  830. return false;
  831. }
  832. if ($rule) {
  833. $rule = explode(',', $rule);
  834. list($width, $height, $type) = getimagesize($file->getRealPath());
  835. if (isset($rule[2])) {
  836. $imageType = strtolower($rule[2]);
  837. if ('jpg' == $imageType) {
  838. $imageType = 'jpeg';
  839. }
  840. if (image_type_to_extension($type, false) != $imageType) {
  841. return false;
  842. }
  843. }
  844. list($w, $h) = $rule;
  845. return $w == $width && $h == $height;
  846. }
  847. return in_array($this->getImageType($file->getRealPath()), [1, 2, 3, 6]);
  848. }
  849. /**
  850. * 验证请求类型
  851. * @access public
  852. * @param mixed $value 字段值
  853. * @param mixed $rule 验证规则
  854. * @return bool
  855. */
  856. public function method($value, $rule)
  857. {
  858. $method = Container::get('request')->method();
  859. return strtoupper($rule) == $method;
  860. }
  861. /**
  862. * 验证时间和日期是否符合指定格式
  863. * @access public
  864. * @param mixed $value 字段值
  865. * @param mixed $rule 验证规则
  866. * @return bool
  867. */
  868. public function dateFormat($value, $rule)
  869. {
  870. $info = date_parse_from_format($rule, $value);
  871. return 0 == $info['warning_count'] && 0 == $info['error_count'];
  872. }
  873. /**
  874. * 验证是否唯一
  875. * @access public
  876. * @param mixed $value 字段值
  877. * @param mixed $rule 验证规则 格式:数据表,字段名,排除ID,主键名
  878. * @param array $data 数据
  879. * @param string $field 验证字段名
  880. * @return bool
  881. */
  882. public function unique($value, $rule, $data, $field)
  883. {
  884. if (is_string($rule)) {
  885. $rule = explode(',', $rule);
  886. }
  887. if (false !== strpos($rule[0], '\\')) {
  888. // 指定模型类
  889. $db = new $rule[0];
  890. } else {
  891. try {
  892. $db = Container::get('app')->model($rule[0]);
  893. } catch (ClassNotFoundException $e) {
  894. $db = Db::name($rule[0]);
  895. }
  896. }
  897. $key = isset($rule[1]) ? $rule[1] : $field;
  898. if (strpos($key, '^')) {
  899. // 支持多个字段验证
  900. $fields = explode('^', $key);
  901. foreach ($fields as $key) {
  902. if (isset($data[$key])) {
  903. $map[] = [$key, '=', $data[$key]];
  904. }
  905. }
  906. } elseif (strpos($key, '=')) {
  907. parse_str($key, $map);
  908. } elseif (isset($data[$field])) {
  909. $map[] = [$key, '=', $data[$field]];
  910. } else {
  911. $map = [];
  912. }
  913. $pk = !empty($rule[3]) ? $rule[3] : $db->getPk();
  914. if (is_string($pk)) {
  915. if (isset($rule[2])) {
  916. $map[] = [$pk, '<>', $rule[2]];
  917. } elseif (isset($data[$pk])) {
  918. $map[] = [$pk, '<>', $data[$pk]];
  919. }
  920. }
  921. if ($db->where($map)->field($pk)->find()) {
  922. return false;
  923. }
  924. return true;
  925. }
  926. /**
  927. * 使用行为类验证
  928. * @access public
  929. * @param mixed $value 字段值
  930. * @param mixed $rule 验证规则
  931. * @param array $data 数据
  932. * @return mixed
  933. */
  934. public function behavior($value, $rule, $data)
  935. {
  936. return Container::get('hook')->exec($rule, $data);
  937. }
  938. /**
  939. * 使用filter_var方式验证
  940. * @access public
  941. * @param mixed $value 字段值
  942. * @param mixed $rule 验证规则
  943. * @return bool
  944. */
  945. public function filter($value, $rule)
  946. {
  947. if (is_string($rule) && strpos($rule, ',')) {
  948. list($rule, $param) = explode(',', $rule);
  949. } elseif (is_array($rule)) {
  950. $param = isset($rule[1]) ? $rule[1] : null;
  951. $rule = $rule[0];
  952. } else {
  953. $param = null;
  954. }
  955. return false !== filter_var($value, is_int($rule) ? $rule : filter_id($rule), $param);
  956. }
  957. /**
  958. * 验证某个字段等于某个值的时候必须
  959. * @access public
  960. * @param mixed $value 字段值
  961. * @param mixed $rule 验证规则
  962. * @param array $data 数据
  963. * @return bool
  964. */
  965. public function requireIf($value, $rule, $data)
  966. {
  967. list($field, $val) = explode(',', $rule);
  968. if ($this->getDataValue($data, $field) == $val) {
  969. return !empty($value) || '0' == $value;
  970. }
  971. return true;
  972. }
  973. /**
  974. * 通过回调方法验证某个字段是否必须
  975. * @access public
  976. * @param mixed $value 字段值
  977. * @param mixed $rule 验证规则
  978. * @param array $data 数据
  979. * @return bool
  980. */
  981. public function requireCallback($value, $rule, $data)
  982. {
  983. $result = call_user_func_array([$this, $rule], [$value, $data]);
  984. if ($result) {
  985. return !empty($value) || '0' == $value;
  986. }
  987. return true;
  988. }
  989. /**
  990. * 验证某个字段有值的情况下必须
  991. * @access public
  992. * @param mixed $value 字段值
  993. * @param mixed $rule 验证规则
  994. * @param array $data 数据
  995. * @return bool
  996. */
  997. public function requireWith($value, $rule, $data)
  998. {
  999. $val = $this->getDataValue($data, $rule);
  1000. if (!empty($val)) {
  1001. return !empty($value) || '0' == $value;
  1002. }
  1003. return true;
  1004. }
  1005. /**
  1006. * 验证是否在范围内
  1007. * @access public
  1008. * @param mixed $value 字段值
  1009. * @param mixed $rule 验证规则
  1010. * @return bool
  1011. */
  1012. public function in($value, $rule)
  1013. {
  1014. return in_array($value, is_array($rule) ? $rule : explode(',', $rule));
  1015. }
  1016. /**
  1017. * 验证是否不在某个范围
  1018. * @access public
  1019. * @param mixed $value 字段值
  1020. * @param mixed $rule 验证规则
  1021. * @return bool
  1022. */
  1023. public function notIn($value, $rule)
  1024. {
  1025. return !in_array($value, is_array($rule) ? $rule : explode(',', $rule));
  1026. }
  1027. /**
  1028. * between验证数据
  1029. * @access public
  1030. * @param mixed $value 字段值
  1031. * @param mixed $rule 验证规则
  1032. * @return bool
  1033. */
  1034. public function between($value, $rule)
  1035. {
  1036. if (is_string($rule)) {
  1037. $rule = explode(',', $rule);
  1038. }
  1039. list($min, $max) = $rule;
  1040. return $value >= $min && $value <= $max;
  1041. }
  1042. /**
  1043. * 使用notbetween验证数据
  1044. * @access public
  1045. * @param mixed $value 字段值
  1046. * @param mixed $rule 验证规则
  1047. * @return bool
  1048. */
  1049. public function notBetween($value, $rule)
  1050. {
  1051. if (is_string($rule)) {
  1052. $rule = explode(',', $rule);
  1053. }
  1054. list($min, $max) = $rule;
  1055. return $value < $min || $value > $max;
  1056. }
  1057. /**
  1058. * 验证数据长度
  1059. * @access public
  1060. * @param mixed $value 字段值
  1061. * @param mixed $rule 验证规则
  1062. * @return bool
  1063. */
  1064. public function length($value, $rule)
  1065. {
  1066. if (is_array($value)) {
  1067. $length = count($value);
  1068. } elseif ($value instanceof File) {
  1069. $length = $value->getSize();
  1070. } else {
  1071. $length = mb_strlen((string) $value);
  1072. }
  1073. if (strpos($rule, ',')) {
  1074. // 长度区间
  1075. list($min, $max) = explode(',', $rule);
  1076. return $length >= $min && $length <= $max;
  1077. }
  1078. // 指定长度
  1079. return $length == $rule;
  1080. }
  1081. /**
  1082. * 验证数据最大长度
  1083. * @access public
  1084. * @param mixed $value 字段值
  1085. * @param mixed $rule 验证规则
  1086. * @return bool
  1087. */
  1088. public function max($value, $rule)
  1089. {
  1090. if (is_array($value)) {
  1091. $length = count($value);
  1092. } elseif ($value instanceof File) {
  1093. $length = $value->getSize();
  1094. } else {
  1095. $length = mb_strlen((string) $value);
  1096. }
  1097. return $length <= $rule;
  1098. }
  1099. /**
  1100. * 验证数据最小长度
  1101. * @access public
  1102. * @param mixed $value 字段值
  1103. * @param mixed $rule 验证规则
  1104. * @return bool
  1105. */
  1106. public function min($value, $rule)
  1107. {
  1108. if (is_array($value)) {
  1109. $length = count($value);
  1110. } elseif ($value instanceof File) {
  1111. $length = $value->getSize();
  1112. } else {
  1113. $length = mb_strlen((string) $value);
  1114. }
  1115. return $length >= $rule;
  1116. }
  1117. /**
  1118. * 验证日期
  1119. * @access public
  1120. * @param mixed $value 字段值
  1121. * @param mixed $rule 验证规则
  1122. * @param array $data 数据
  1123. * @return bool
  1124. */
  1125. public function after($value, $rule, $data)
  1126. {
  1127. return strtotime($value) >= strtotime($rule);
  1128. }
  1129. /**
  1130. * 验证日期
  1131. * @access public
  1132. * @param mixed $value 字段值
  1133. * @param mixed $rule 验证规则
  1134. * @param array $data 数据
  1135. * @return bool
  1136. */
  1137. public function before($value, $rule, $data)
  1138. {
  1139. return strtotime($value) <= strtotime($rule);
  1140. }
  1141. /**
  1142. * 验证日期字段
  1143. * @access protected
  1144. * @param mixed $value 字段值
  1145. * @param mixed $rule 验证规则
  1146. * @param array $data 数据
  1147. * @return bool
  1148. */
  1149. protected function afterWith($value, $rule, $data)
  1150. {
  1151. $rule = $this->getDataValue($data, $rule);
  1152. return !is_null($rule) && strtotime($value) >= strtotime($rule);
  1153. }
  1154. /**
  1155. * 验证日期字段
  1156. * @access protected
  1157. * @param mixed $value 字段值
  1158. * @param mixed $rule 验证规则
  1159. * @param array $data 数据
  1160. * @return bool
  1161. */
  1162. protected function beforeWith($value, $rule, $data)
  1163. {
  1164. $rule = $this->getDataValue($data, $rule);
  1165. return !is_null($rule) && strtotime($value) <= strtotime($rule);
  1166. }
  1167. /**
  1168. * 验证有效期
  1169. * @access public
  1170. * @param mixed $value 字段值
  1171. * @param mixed $rule 验证规则
  1172. * @return bool
  1173. */
  1174. public function expire($value, $rule)
  1175. {
  1176. if (is_string($rule)) {
  1177. $rule = explode(',', $rule);
  1178. }
  1179. list($start, $end) = $rule;
  1180. if (!is_numeric($start)) {
  1181. $start = strtotime($start);
  1182. }
  1183. if (!is_numeric($end)) {
  1184. $end = strtotime($end);
  1185. }
  1186. return $_SERVER['REQUEST_TIME'] >= $start && $_SERVER['REQUEST_TIME'] <= $end;
  1187. }
  1188. /**
  1189. * 验证IP许可
  1190. * @access public
  1191. * @param string $value 字段值
  1192. * @param mixed $rule 验证规则
  1193. * @return mixed
  1194. */
  1195. public function allowIp($value, $rule)
  1196. {
  1197. return in_array($value, is_array($rule) ? $rule : explode(',', $rule));
  1198. }
  1199. /**
  1200. * 验证IP禁用
  1201. * @access public
  1202. * @param string $value 字段值
  1203. * @param mixed $rule 验证规则
  1204. * @return mixed
  1205. */
  1206. public function denyIp($value, $rule)
  1207. {
  1208. return !in_array($value, is_array($rule) ? $rule : explode(',', $rule));
  1209. }
  1210. /**
  1211. * 使用正则验证数据
  1212. * @access public
  1213. * @param mixed $value 字段值
  1214. * @param mixed $rule 验证规则 正则规则或者预定义正则名
  1215. * @return bool
  1216. */
  1217. public function regex($value, $rule)
  1218. {
  1219. if (isset($this->regex[$rule])) {
  1220. $rule = $this->regex[$rule];
  1221. } elseif (isset($this->defaultRegex[$rule])) {
  1222. $rule = $this->defaultRegex[$rule];
  1223. }
  1224. if (0 !== strpos($rule, '/') && !preg_match('/\/[imsU]{0,4}$/', $rule)) {
  1225. // 不是正则表达式则两端补上/
  1226. $rule = '/^' . $rule . '$/';
  1227. }
  1228. return is_scalar($value) && 1 === preg_match($rule, (string) $value);
  1229. }
  1230. /**
  1231. * 验证表单令牌
  1232. * @access public
  1233. * @param mixed $value 字段值
  1234. * @param mixed $rule 验证规则
  1235. * @param array $data 数据
  1236. * @return bool
  1237. */
  1238. public function token($value, $rule, $data)
  1239. {
  1240. $rule = !empty($rule) ? $rule : '__token__';
  1241. $session = Container::get('session');
  1242. if (!isset($data[$rule]) || !$session->has($rule)) {
  1243. // 令牌数据无效
  1244. return false;
  1245. }
  1246. // 令牌验证
  1247. if (isset($data[$rule]) && $session->get($rule) === $data[$rule]) {
  1248. // 防止重复提交
  1249. $session->delete($rule); // 验证完成销毁session
  1250. return true;
  1251. }
  1252. // 开启TOKEN重置
  1253. $session->delete($rule);
  1254. return false;
  1255. }
  1256. // 获取错误信息
  1257. public function getError()
  1258. {
  1259. return $this->error;
  1260. }
  1261. /**
  1262. * 获取数据值
  1263. * @access protected
  1264. * @param array $data 数据
  1265. * @param string $key 数据标识 支持多维
  1266. * @return mixed
  1267. */
  1268. protected function getDataValue($data, $key)
  1269. {
  1270. if (is_numeric($key)) {
  1271. $value = $key;
  1272. } elseif (strpos($key, '.')) {
  1273. // 支持多维数组验证
  1274. foreach (explode('.', $key) as $key) {
  1275. if (!isset($data[$key])) {
  1276. $value = null;
  1277. break;
  1278. }
  1279. $value = $data = $data[$key];
  1280. }
  1281. } else {
  1282. $value = isset($data[$key]) ? $data[$key] : null;
  1283. }
  1284. return $value;
  1285. }
  1286. /**
  1287. * 获取验证规则的错误提示信息
  1288. * @access protected
  1289. * @param string $attribute 字段英文名
  1290. * @param string $title 字段描述名
  1291. * @param string $type 验证规则名称
  1292. * @param mixed $rule 验证规则数据
  1293. * @return string
  1294. */
  1295. protected function getRuleMsg($attribute, $title, $type, $rule)
  1296. {
  1297. $lang = Container::get('lang');
  1298. if (isset($this->message[$attribute . '.' . $type])) {
  1299. $msg = $this->message[$attribute . '.' . $type];
  1300. } elseif (isset($this->message[$attribute][$type])) {
  1301. $msg = $this->message[$attribute][$type];
  1302. } elseif (isset($this->message[$attribute])) {
  1303. $msg = $this->message[$attribute];
  1304. } elseif (isset(self::$typeMsg[$type])) {
  1305. $msg = self::$typeMsg[$type];
  1306. } elseif (0 === strpos($type, 'require')) {
  1307. $msg = self::$typeMsg['require'];
  1308. } else {
  1309. $msg = $title . $lang->get('not conform to the rules');
  1310. }
  1311. if (!is_string($msg)) {
  1312. return $msg;
  1313. }
  1314. if (0 === strpos($msg, '{%')) {
  1315. $msg = $lang->get(substr($msg, 2, -1));
  1316. } elseif ($lang->has($msg)) {
  1317. $msg = $lang->get($msg);
  1318. }
  1319. if (is_scalar($rule) && false !== strpos($msg, ':')) {
  1320. // 变量替换
  1321. if (is_string($rule) && strpos($rule, ',')) {
  1322. $array = array_pad(explode(',', $rule), 3, '');
  1323. } else {
  1324. $array = array_pad([], 3, '');
  1325. }
  1326. $msg = str_replace(
  1327. [':attribute', ':1', ':2', ':3'],
  1328. [$title, $array[0], $array[1], $array[2]],
  1329. $msg);
  1330. if (strpos($msg, ':rule')) {
  1331. $msg = str_replace(':rule', (string) $rule, $msg);
  1332. }
  1333. }
  1334. return $msg;
  1335. }
  1336. /**
  1337. * 获取数据验证的场景
  1338. * @access protected
  1339. * @param string $scene 验证场景
  1340. * @return void
  1341. */
  1342. protected function getScene($scene = '')
  1343. {
  1344. if (empty($scene)) {
  1345. // 读取指定场景
  1346. $scene = $this->currentScene;
  1347. }
  1348. $this->only = $this->append = $this->remove = [];
  1349. if (empty($scene)) {
  1350. return;
  1351. }
  1352. if (method_exists($this, 'scene' . $scene)) {
  1353. call_user_func([$this, 'scene' . $scene]);
  1354. } elseif (isset($this->scene[$scene])) {
  1355. // 如果设置了验证适用场景
  1356. $scene = $this->scene[$scene];
  1357. if (is_string($scene)) {
  1358. $scene = explode(',', $scene);
  1359. }
  1360. $this->only = $scene;
  1361. }
  1362. }
  1363. /**
  1364. * 动态方法 直接调用is方法进行验证
  1365. * @access public
  1366. * @param string $method 方法名
  1367. * @param array $args 调用参数
  1368. * @return bool
  1369. */
  1370. public function __call($method, $args)
  1371. {
  1372. if ('is' == strtolower(substr($method, 0, 2))) {
  1373. $method = substr($method, 2);
  1374. }
  1375. array_push($args, lcfirst($method));
  1376. return call_user_func_array([$this, 'is'], $args);
  1377. }
  1378. }