Validate.php 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556
  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. 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::get('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. try {
  894. $db = Container::get('app')->model($rule[0]);
  895. } catch (ClassNotFoundException $e) {
  896. $db = Db::name($rule[0]);
  897. }
  898. }
  899. $key = isset($rule[1]) ? $rule[1] : $field;
  900. if (strpos($key, '^')) {
  901. // 支持多个字段验证
  902. $fields = explode('^', $key);
  903. foreach ($fields as $key) {
  904. if (isset($data[$key])) {
  905. $map[] = [$key, '=', $data[$key]];
  906. }
  907. }
  908. } elseif (strpos($key, '=')) {
  909. parse_str($key, $map);
  910. } elseif (isset($data[$field])) {
  911. $map[] = [$key, '=', $data[$field]];
  912. } else {
  913. $map = [];
  914. }
  915. $pk = !empty($rule[3]) ? $rule[3] : $db->getPk();
  916. if (is_string($pk)) {
  917. if (isset($rule[2])) {
  918. $map[] = [$pk, '<>', $rule[2]];
  919. } elseif (isset($data[$pk])) {
  920. $map[] = [$pk, '<>', $data[$pk]];
  921. }
  922. }
  923. if ($db->where($map)->field($pk)->find()) {
  924. return false;
  925. }
  926. return true;
  927. }
  928. /**
  929. * 使用行为类验证
  930. * @access public
  931. * @param mixed $value 字段值
  932. * @param mixed $rule 验证规则
  933. * @param array $data 数据
  934. * @return mixed
  935. */
  936. public function behavior($value, $rule, $data)
  937. {
  938. return Container::get('hook')->exec($rule, $data);
  939. }
  940. /**
  941. * 使用filter_var方式验证
  942. * @access public
  943. * @param mixed $value 字段值
  944. * @param mixed $rule 验证规则
  945. * @return bool
  946. */
  947. public function filter($value, $rule)
  948. {
  949. if (is_string($rule) && strpos($rule, ',')) {
  950. list($rule, $param) = explode(',', $rule);
  951. } elseif (is_array($rule)) {
  952. $param = isset($rule[1]) ? $rule[1] : null;
  953. $rule = $rule[0];
  954. } else {
  955. $param = null;
  956. }
  957. return false !== filter_var($value, is_int($rule) ? $rule : filter_id($rule), $param);
  958. }
  959. /**
  960. * 验证某个字段等于某个值的时候必须
  961. * @access public
  962. * @param mixed $value 字段值
  963. * @param mixed $rule 验证规则
  964. * @param array $data 数据
  965. * @return bool
  966. */
  967. public function requireIf($value, $rule, $data)
  968. {
  969. list($field, $val) = explode(',', $rule);
  970. if ($this->getDataValue($data, $field) == $val) {
  971. return !empty($value) || '0' == $value;
  972. }
  973. return true;
  974. }
  975. /**
  976. * 通过回调方法验证某个字段是否必须
  977. * @access public
  978. * @param mixed $value 字段值
  979. * @param mixed $rule 验证规则
  980. * @param array $data 数据
  981. * @return bool
  982. */
  983. public function requireCallback($value, $rule, $data)
  984. {
  985. $result = call_user_func_array([$this, $rule], [$value, $data]);
  986. if ($result) {
  987. return !empty($value) || '0' == $value;
  988. }
  989. return true;
  990. }
  991. /**
  992. * 验证某个字段有值的情况下必须
  993. * @access public
  994. * @param mixed $value 字段值
  995. * @param mixed $rule 验证规则
  996. * @param array $data 数据
  997. * @return bool
  998. */
  999. public function requireWith($value, $rule, $data)
  1000. {
  1001. $val = $this->getDataValue($data, $rule);
  1002. if (!empty($val)) {
  1003. return !empty($value) || '0' == $value;
  1004. }
  1005. return true;
  1006. }
  1007. /**
  1008. * 验证是否在范围内
  1009. * @access public
  1010. * @param mixed $value 字段值
  1011. * @param mixed $rule 验证规则
  1012. * @return bool
  1013. */
  1014. public function in($value, $rule)
  1015. {
  1016. return in_array($value, is_array($rule) ? $rule : explode(',', $rule));
  1017. }
  1018. /**
  1019. * 验证是否不在某个范围
  1020. * @access public
  1021. * @param mixed $value 字段值
  1022. * @param mixed $rule 验证规则
  1023. * @return bool
  1024. */
  1025. public function notIn($value, $rule)
  1026. {
  1027. return !in_array($value, is_array($rule) ? $rule : explode(',', $rule));
  1028. }
  1029. /**
  1030. * between验证数据
  1031. * @access public
  1032. * @param mixed $value 字段值
  1033. * @param mixed $rule 验证规则
  1034. * @return bool
  1035. */
  1036. public function between($value, $rule)
  1037. {
  1038. if (is_string($rule)) {
  1039. $rule = explode(',', $rule);
  1040. }
  1041. list($min, $max) = $rule;
  1042. return $value >= $min && $value <= $max;
  1043. }
  1044. /**
  1045. * 使用notbetween验证数据
  1046. * @access public
  1047. * @param mixed $value 字段值
  1048. * @param mixed $rule 验证规则
  1049. * @return bool
  1050. */
  1051. public function notBetween($value, $rule)
  1052. {
  1053. if (is_string($rule)) {
  1054. $rule = explode(',', $rule);
  1055. }
  1056. list($min, $max) = $rule;
  1057. return $value < $min || $value > $max;
  1058. }
  1059. /**
  1060. * 验证数据长度
  1061. * @access public
  1062. * @param mixed $value 字段值
  1063. * @param mixed $rule 验证规则
  1064. * @return bool
  1065. */
  1066. public function length($value, $rule)
  1067. {
  1068. if (is_array($value)) {
  1069. $length = count($value);
  1070. } elseif ($value instanceof File) {
  1071. $length = $value->getSize();
  1072. } else {
  1073. $length = mb_strlen((string) $value);
  1074. }
  1075. if (strpos($rule, ',')) {
  1076. // 长度区间
  1077. list($min, $max) = explode(',', $rule);
  1078. return $length >= $min && $length <= $max;
  1079. }
  1080. // 指定长度
  1081. return $length == $rule;
  1082. }
  1083. /**
  1084. * 验证数据最大长度
  1085. * @access public
  1086. * @param mixed $value 字段值
  1087. * @param mixed $rule 验证规则
  1088. * @return bool
  1089. */
  1090. public function max($value, $rule)
  1091. {
  1092. if (is_array($value)) {
  1093. $length = count($value);
  1094. } elseif ($value instanceof File) {
  1095. $length = $value->getSize();
  1096. } else {
  1097. $length = mb_strlen((string) $value);
  1098. }
  1099. return $length <= $rule;
  1100. }
  1101. /**
  1102. * 验证数据最小长度
  1103. * @access public
  1104. * @param mixed $value 字段值
  1105. * @param mixed $rule 验证规则
  1106. * @return bool
  1107. */
  1108. public function min($value, $rule)
  1109. {
  1110. if (is_array($value)) {
  1111. $length = count($value);
  1112. } elseif ($value instanceof File) {
  1113. $length = $value->getSize();
  1114. } else {
  1115. $length = mb_strlen((string) $value);
  1116. }
  1117. return $length >= $rule;
  1118. }
  1119. /**
  1120. * 验证日期
  1121. * @access public
  1122. * @param mixed $value 字段值
  1123. * @param mixed $rule 验证规则
  1124. * @param array $data 数据
  1125. * @return bool
  1126. */
  1127. public function after($value, $rule, $data)
  1128. {
  1129. return strtotime($value) >= strtotime($rule);
  1130. }
  1131. /**
  1132. * 验证日期
  1133. * @access public
  1134. * @param mixed $value 字段值
  1135. * @param mixed $rule 验证规则
  1136. * @param array $data 数据
  1137. * @return bool
  1138. */
  1139. public function before($value, $rule, $data)
  1140. {
  1141. return strtotime($value) <= strtotime($rule);
  1142. }
  1143. /**
  1144. * 验证日期字段
  1145. * @access protected
  1146. * @param mixed $value 字段值
  1147. * @param mixed $rule 验证规则
  1148. * @param array $data 数据
  1149. * @return bool
  1150. */
  1151. protected function afterWith($value, $rule, $data)
  1152. {
  1153. $rule = $this->getDataValue($data, $rule);
  1154. return !is_null($rule) && strtotime($value) >= strtotime($rule);
  1155. }
  1156. /**
  1157. * 验证日期字段
  1158. * @access protected
  1159. * @param mixed $value 字段值
  1160. * @param mixed $rule 验证规则
  1161. * @param array $data 数据
  1162. * @return bool
  1163. */
  1164. protected function beforeWith($value, $rule, $data)
  1165. {
  1166. $rule = $this->getDataValue($data, $rule);
  1167. return !is_null($rule) && strtotime($value) <= strtotime($rule);
  1168. }
  1169. /**
  1170. * 验证有效期
  1171. * @access public
  1172. * @param mixed $value 字段值
  1173. * @param mixed $rule 验证规则
  1174. * @return bool
  1175. */
  1176. public function expire($value, $rule)
  1177. {
  1178. if (is_string($rule)) {
  1179. $rule = explode(',', $rule);
  1180. }
  1181. list($start, $end) = $rule;
  1182. if (!is_numeric($start)) {
  1183. $start = strtotime($start);
  1184. }
  1185. if (!is_numeric($end)) {
  1186. $end = strtotime($end);
  1187. }
  1188. return $_SERVER['REQUEST_TIME'] >= $start && $_SERVER['REQUEST_TIME'] <= $end;
  1189. }
  1190. /**
  1191. * 验证IP许可
  1192. * @access public
  1193. * @param string $value 字段值
  1194. * @param mixed $rule 验证规则
  1195. * @return mixed
  1196. */
  1197. public function allowIp($value, $rule)
  1198. {
  1199. return in_array($value, is_array($rule) ? $rule : explode(',', $rule));
  1200. }
  1201. /**
  1202. * 验证IP禁用
  1203. * @access public
  1204. * @param string $value 字段值
  1205. * @param mixed $rule 验证规则
  1206. * @return mixed
  1207. */
  1208. public function denyIp($value, $rule)
  1209. {
  1210. return !in_array($value, is_array($rule) ? $rule : explode(',', $rule));
  1211. }
  1212. /**
  1213. * 使用正则验证数据
  1214. * @access public
  1215. * @param mixed $value 字段值
  1216. * @param mixed $rule 验证规则 正则规则或者预定义正则名
  1217. * @return bool
  1218. */
  1219. public function regex($value, $rule)
  1220. {
  1221. if (isset($this->regex[$rule])) {
  1222. $rule = $this->regex[$rule];
  1223. } elseif (isset($this->defaultRegex[$rule])) {
  1224. $rule = $this->defaultRegex[$rule];
  1225. }
  1226. if (0 !== strpos($rule, '/') && !preg_match('/\/[imsU]{0,4}$/', $rule)) {
  1227. // 不是正则表达式则两端补上/
  1228. $rule = '/^' . $rule . '$/';
  1229. }
  1230. return is_scalar($value) && 1 === preg_match($rule, (string) $value);
  1231. }
  1232. /**
  1233. * 验证表单令牌
  1234. * @access public
  1235. * @param mixed $value 字段值
  1236. * @param mixed $rule 验证规则
  1237. * @param array $data 数据
  1238. * @return bool
  1239. */
  1240. public function token($value, $rule, $data)
  1241. {
  1242. $rule = !empty($rule) ? $rule : '__token__';
  1243. $session = Container::get('session');
  1244. if (!isset($data[$rule]) || !$session->has($rule)) {
  1245. // 令牌数据无效
  1246. return false;
  1247. }
  1248. // 令牌验证
  1249. if (isset($data[$rule]) && $session->get($rule) === $data[$rule]) {
  1250. // 防止重复提交
  1251. $session->delete($rule); // 验证完成销毁session
  1252. return true;
  1253. }
  1254. // 开启TOKEN重置
  1255. $session->delete($rule);
  1256. return false;
  1257. }
  1258. // 获取错误信息
  1259. public function getError()
  1260. {
  1261. return $this->error;
  1262. }
  1263. /**
  1264. * 获取数据值
  1265. * @access protected
  1266. * @param array $data 数据
  1267. * @param string $key 数据标识 支持多维
  1268. * @return mixed
  1269. */
  1270. protected function getDataValue($data, $key)
  1271. {
  1272. if (is_numeric($key)) {
  1273. $value = $key;
  1274. } elseif (strpos($key, '.')) {
  1275. // 支持多维数组验证
  1276. foreach (explode('.', $key) as $key) {
  1277. if (!isset($data[$key])) {
  1278. $value = null;
  1279. break;
  1280. }
  1281. $value = $data = $data[$key];
  1282. }
  1283. } else {
  1284. $value = isset($data[$key]) ? $data[$key] : null;
  1285. }
  1286. return $value;
  1287. }
  1288. /**
  1289. * 获取验证规则的错误提示信息
  1290. * @access protected
  1291. * @param string $attribute 字段英文名
  1292. * @param string $title 字段描述名
  1293. * @param string $type 验证规则名称
  1294. * @param mixed $rule 验证规则数据
  1295. * @return string
  1296. */
  1297. protected function getRuleMsg($attribute, $title, $type, $rule)
  1298. {
  1299. $lang = Container::get('lang');
  1300. if (isset($this->message[$attribute . '.' . $type])) {
  1301. $msg = $this->message[$attribute . '.' . $type];
  1302. } elseif (isset($this->message[$attribute][$type])) {
  1303. $msg = $this->message[$attribute][$type];
  1304. } elseif (isset($this->message[$attribute])) {
  1305. $msg = $this->message[$attribute];
  1306. } elseif (isset(self::$typeMsg[$type])) {
  1307. $msg = self::$typeMsg[$type];
  1308. } elseif (0 === strpos($type, 'require')) {
  1309. $msg = self::$typeMsg['require'];
  1310. } else {
  1311. $msg = $title . $lang->get('not conform to the rules');
  1312. }
  1313. if (!is_string($msg)) {
  1314. return $msg;
  1315. }
  1316. if (0 === strpos($msg, '{%')) {
  1317. $msg = $lang->get(substr($msg, 2, -1));
  1318. } elseif ($lang->has($msg)) {
  1319. $msg = $lang->get($msg);
  1320. }
  1321. if (is_scalar($rule) && false !== strpos($msg, ':')) {
  1322. // 变量替换
  1323. if (is_string($rule) && strpos($rule, ',')) {
  1324. $array = array_pad(explode(',', $rule), 3, '');
  1325. } else {
  1326. $array = array_pad([], 3, '');
  1327. }
  1328. $msg = str_replace(
  1329. [':attribute', ':1', ':2', ':3'],
  1330. [$title, $array[0], $array[1], $array[2]],
  1331. $msg);
  1332. if (strpos($msg, ':rule')) {
  1333. $msg = str_replace(':rule', (string) $rule, $msg);
  1334. }
  1335. }
  1336. return $msg;
  1337. }
  1338. /**
  1339. * 获取数据验证的场景
  1340. * @access protected
  1341. * @param string $scene 验证场景
  1342. * @return void
  1343. */
  1344. protected function getScene($scene = '')
  1345. {
  1346. if (empty($scene)) {
  1347. // 读取指定场景
  1348. $scene = $this->currentScene;
  1349. }
  1350. $this->only = $this->append = $this->remove = [];
  1351. if (empty($scene)) {
  1352. return;
  1353. }
  1354. if (method_exists($this, 'scene' . $scene)) {
  1355. call_user_func([$this, 'scene' . $scene]);
  1356. } elseif (isset($this->scene[$scene])) {
  1357. // 如果设置了验证适用场景
  1358. $scene = $this->scene[$scene];
  1359. if (is_string($scene)) {
  1360. $scene = explode(',', $scene);
  1361. }
  1362. $this->only = $scene;
  1363. }
  1364. }
  1365. /**
  1366. * 动态方法 直接调用is方法进行验证
  1367. * @access public
  1368. * @param string $method 方法名
  1369. * @param array $args 调用参数
  1370. * @return bool
  1371. */
  1372. public function __call($method, $args)
  1373. {
  1374. if ('is' == strtolower(substr($method, 0, 2))) {
  1375. $method = substr($method, 2);
  1376. }
  1377. array_push($args, lcfirst($method));
  1378. return call_user_func_array([$this, 'is'], $args);
  1379. }
  1380. }