当前位置: 首页>>代码示例>>PHP>>正文


PHP Validator::isLuhn方法代码示例

本文整理汇总了PHP中lithium\util\Validator::isLuhn方法的典型用法代码示例。如果您正苦于以下问题:PHP Validator::isLuhn方法的具体用法?PHP Validator::isLuhn怎么用?PHP Validator::isLuhn使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在lithium\util\Validator的用法示例。


在下文中一共展示了Validator::isLuhn方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: testCreditCardValidation


//.........这里部分代码省略.........
     $this->assertTrue(Validator::isCreditCard('4916933155767', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4024007159672', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4716935544871', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4929415177779', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4929748547896', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4929153468612', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4539397132104', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4485293435540', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4485799412720', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4916744757686', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4556475655426', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4539400441625', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4485437129173', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4716253605320', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4539366156589', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4916498061392', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4716127163779', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4024007183078', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4041553279654', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4532380121960', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4485906062491', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4539365115149', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4485146516702', 'visa'));
     /* Visa 16 digit */
     $this->assertTrue(Validator::isCreditCard('4916375389940009', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4929167481032610', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4485029969061519', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4485573845281759', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4485669810383529', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4929615806560327', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4556807505609535', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4532611336232890', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4532201952422387', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4485073797976290', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4024007157580969', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4053740470212274', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4716265831525676', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4024007100222966', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4539556148303244', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4532449879689709', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4916805467840986', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4532155644440233', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4467977802223781', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4539224637000686', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4556629187064965', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4532970205932943', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4821470132041850', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4916214267894485', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4024007169073284', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4716783351296122', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4556480171913795', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4929678411034997', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4682061913519392', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4916495481746474', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4929007108460499', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4539951357838586', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4716482691051558', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4916385069917516', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4929020289494641', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4532176245263774', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4556242273553949', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4481007485188614', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4716533372139623', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4929152038152632', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4539404037310550', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4532800925229140', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4916845885268360', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4394514669078434', 'visa'));
     $this->assertTrue(Validator::isCreditCard('4485611378115042', 'visa'));
     $this->assertFalse(Validator::isCreditCard('869940697287073', 'visa'));
     /* Visa Electron */
     $this->assertTrue(Validator::isCreditCard('4175003346287100', 'electron'));
     $this->assertTrue(Validator::isCreditCard('4913042516577228', 'electron'));
     $this->assertTrue(Validator::isCreditCard('4917592325659381', 'electron'));
     $this->assertTrue(Validator::isCreditCard('4917084924450511', 'electron'));
     $this->assertTrue(Validator::isCreditCard('4917994610643999', 'electron'));
     $this->assertTrue(Validator::isCreditCard('4175005933743585', 'electron'));
     $this->assertTrue(Validator::isCreditCard('4175008373425044', 'electron'));
     $this->assertTrue(Validator::isCreditCard('4913119763664154', 'electron'));
     $this->assertTrue(Validator::isCreditCard('4913189017481812', 'electron'));
     $this->assertTrue(Validator::isCreditCard('4913085104968622', 'electron'));
     $this->assertTrue(Validator::isCreditCard('4175008803122021', 'electron'));
     $this->assertTrue(Validator::isCreditCard('4913294453962489', 'electron'));
     $this->assertTrue(Validator::isCreditCard('4175009797419290', 'electron'));
     $this->assertTrue(Validator::isCreditCard('4175005028142917', 'electron'));
     $this->assertTrue(Validator::isCreditCard('4913940802385364', 'electron'));
     $this->assertFalse(Validator::isCreditCard('869940697287073', 'electron'));
     /* Voyager */
     $this->assertTrue(Validator::isCreditCard('869940697287073', 'voyager'));
     $this->assertTrue(Validator::isCreditCard('869934523596112', 'voyager'));
     $this->assertTrue(Validator::isCreditCard('869958670174621', 'voyager'));
     $this->assertTrue(Validator::isCreditCard('869921250068209', 'voyager'));
     $this->assertTrue(Validator::isCreditCard('869972521242198', 'voyager'));
     $this->assertFalse(Validator::isCreditCard('370482756063980', 'voyager'));
     $this->assertTrue(Validator::isLuhn('869972521242198'));
     $this->assertFalse(Validator::isLuhn(false));
     $this->assertFalse(Validator::isLuhn(null));
     $this->assertFalse(Validator::isLuhn(''));
     $this->assertFalse(Validator::isLuhn(true));
 }
开发者ID:nilamdoc,项目名称:KYCGlobal,代码行数:101,代码来源:ValidatorTest.php

示例2: __init

 /**
  * Initializes the list of default validation rules.
  *
  * @return void
  */
 public static function __init()
 {
     $alnum = '[A-Fa-f0-9]';
     $class = get_called_class();
     static::$_methodFilters[$class] = array();
     static::$_rules = array('alphaNumeric' => '/^[\\p{Ll}\\p{Lm}\\p{Lo}\\p{Lt}\\p{Lu}\\p{Nd}]+$/mu', 'blank' => '/[^\\s]/', 'creditCard' => array('amex' => '/^3[4|7]\\d{13}$/', 'bankcard' => '/^56(10\\d\\d|022[1-5])\\d{10}$/', 'diners' => '/^(?:3(0[0-5]|[68]\\d)\\d{11})|(?:5[1-5]\\d{14})$/', 'disc' => '/^(?:6011|650\\d)\\d{12}$/', 'electron' => '/^(?:417500|4917\\d{2}|4913\\d{2})\\d{10}$/', 'enroute' => '/^2(?:014|149)\\d{11}$/', 'jcb' => '/^(3\\d{4}|2100|1800)\\d{11}$/', 'maestro' => '/^(?:5020|6\\d{3})\\d{12}$/', 'mc' => '/^5[1-5]\\d{14}$/', 'solo' => '/^(6334[5-9][0-9]|6767[0-9]{2})\\d{10}(\\d{2,3})?$/', 'switch' => '/^(?:49(03(0[2-9]|3[5-9])|11(0[1-2]|7[4-9]|8[1-2])|36[0-9]{2})' . '\\d{10}(\\d{2,3})?)|(?:564182\\d{10}(\\d{2,3})?)|(6(3(33[0-4]' . '[0-9])|759[0-9]{2})\\d{10}(\\d{2,3})?)$/', 'visa' => '/^4\\d{12}(\\d{3})?$/', 'voyager' => '/^8699[0-9]{11}$/', 'fast' => '/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{12}|3' . '(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})$/'), 'date' => array('dmy' => '%^(?:(?:31(\\/|-|\\.|\\x20)(?:0?[13578]|1[02]))\\1|(?:(?:29|30)' . '(\\/|-|\\.|\\x20)(?:0?[1,3-9]|1[0-2])\\2))(?:(?:1[6-9]|[2-9]\\d)?' . '\\d{2})$|^(?:29(\\/|-|\\.|\\x20)0?2\\3(?:(?:(?:1[6-9]|[2-9]\\d)?' . '(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])' . '00))))$|^(?:0?[1-9]|1\\d|2[0-8])(\\/|-|\\.|\\x20)(?:(?:0?[1-9])|' . '(?:1[0-2]))\\4(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$%', 'mdy' => '%^(?:(?:(?:0?[13578]|1[02])(\\/|-|\\.|\\x20)31)\\1|(?:(?:0?[13-9]|' . '1[0-2])(\\/|-|\\.|\\x20)(?:29|30)\\2))(?:(?:1[6-9]|[2-9]\\d)?\\d' . '{2})$|^(?:0?2(\\/|-|\\.|\\x20)29\\3(?:(?:(?:1[6-9]|[2-9]\\d)?' . '(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])' . '00))))$|^(?:(?:0?[1-9])|(?:1[0-2]))(\\/|-|\\.|\\x20)(?:0?[1-9]|1' . '\\d|2[0-8])\\4(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$%', 'ymd' => '%^(?:(?:(?:(?:(?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579]' . '[26])|(?:(?:16|[2468][048]|[3579][26])00)))(\\/|-|\\.|\\x20)' . '(?:0?2\\1(?:29)))|(?:(?:(?:1[6-9]|[2-9]\\d)?\\d{2})(\\/|-|\\.|' . '\\x20)(?:(?:(?:0?[13578]|1[02])\\2(?:31))|(?:(?:0?[1,3-9]|1[0-2])' . '\\2(29|30))|(?:(?:0?[1-9])|(?:1[0-2]))\\2(?:0?[1-9]|1\\d|2[0-8]' . '))))$%', 'dMy' => '/^((31(?!\\ (Feb(ruary)?|Apr(il)?|June?|(Sep(?=\\b|t)t?|Nov)' . '(ember)?)))|((30|29)(?!\\ Feb(ruary)?))|(29(?=\\ Feb(ruary)?\\ ' . '(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468]' . '[048]|[3579][26])00)))))|(0?[1-9])|1\\d|2[0-8])\\ (Jan(uary)?|' . 'Feb(ruary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|' . 'Oct(ober)?|(Sep(?=\\b|t)t?|Nov|Dec)(ember)?)\\ ((1[6-9]|[2-9]' . '\\d)\\d{2})$/', 'Mdy' => '/^(?:(((Jan(uary)?|Ma(r(ch)?|y)|Jul(y)?|Aug(ust)?|Oct(ober)?' . '|Dec(ember)?)\\ 31)|((Jan(uary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)' . '|(ne?))|Aug(ust)?|Oct(ober)?|(Sept|Nov|Dec)(ember)?)\\ (0?[1-9]' . '|([12]\\d)|30))|(Feb(ruary)?\\ (0?[1-9]|1\\d|2[0-8]|(29(?=,?\\ ' . '((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468]' . '[048]|[3579][26])00)))))))\\,?\\ ((1[6-9]|[2-9]\\d)\\d{2}))$/', 'My' => '%^(Jan(uary)?|Feb(ruary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|' . 'Aug(ust)?|Oct(ober)?|(Sep(?=\\b|t)t?|Nov|Dec)(ember)?)[ /]((1[6-9]' . '|[2-9]\\d)\\d{2})$%', 'my' => '%^(((0[123456789]|10|11|12)([- /.])(([1][9][0-9][0-9])|([2][0-9]' . '[0-9][0-9]))))$%'), 'ip' => function ($value, $format = null, array $options = array()) {
         $options += array('flags' => array());
         return (bool) filter_var($value, FILTER_VALIDATE_IP, $options);
     }, 'money' => array('right' => '/^(?!0,?\\d)(?:\\d{1,3}(?:([, .])\\d{3})?(?:\\1\\d{3})*|(?:\\d+))' . '((?!\\1)[,.]\\d{2})?(?<!\\x{00a2})\\p{Sc}?$/u', 'left' => '/^(?!\\x{00a2})\\p{Sc}?(?!0,?\\d)(?:\\d{1,3}(?:([, .])\\d{3})?' . '(?:\\1\\d{3})*|(?:\\d+))((?!\\1)[,.]\\d{2})?$/u'), 'notEmpty' => '/[^\\s]+/m', 'phone' => '/^\\+?[0-9\\(\\)\\-]{10,20}$/', 'postalCode' => '/(^|\\A\\b)[A-Z0-9\\s\\-]{5,}($|\\b\\z)/i', 'regex' => '/^(?:([^[:alpha:]\\\\{<\\[\\(])(.+)(?:\\1))|(?:{(.+)})|(?:<(.+)>)|' . '(?:\\[(.+)\\])|(?:\\((.+)\\))[gimsxu]*$/', 'time' => '%^((0?[1-9]|1[012])(:[0-5]\\d){0,2}([AP]M|[ap]m))$|^([01]\\d|2[0-3])' . '(:[0-5]\\d){0,2}$%', 'boolean' => function ($value) {
         $bool = is_bool($value);
         $filter = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
         return $bool || $filter !== null || empty($value);
     }, 'decimal' => function ($value, $format = null, array $options = array()) {
         if (isset($options['precision'])) {
             $precision = strlen($value) - strrpos($value, '.') - 1;
             if ($precision !== (int) $options['precision']) {
                 return false;
             }
         }
         return filter_var($value, FILTER_VALIDATE_FLOAT, FILTER_NULL_ON_FAILURE) !== null;
     }, 'inList' => function ($value, $format, $options) {
         $options += array('list' => array());
         return in_array($value, $options['list']);
     }, 'lengthBetween' => function ($value, $format, $options) {
         $length = strlen($value);
         $options += array('min' => 1, 'max' => 255);
         return $length >= $options['min'] && $length <= $options['max'];
     }, 'luhn' => function ($value) {
         if (empty($value) || !is_string($value)) {
             return false;
         }
         $sum = 0;
         $length = strlen($value);
         for ($position = 1 - $length % 2; $position < $length; $position += 2) {
             $sum += $value[$position];
         }
         for ($position = $length % 2; $position < $length; $position += 2) {
             $number = $value[$position] * 2;
             $sum += $number < 10 ? $number : $number - 9;
         }
         return $sum % 10 == 0;
     }, 'numeric' => function ($value) {
         return is_numeric($value);
     }, 'inRange' => function ($value, $format, $options) {
         $defaults = array('upper' => null, 'lower' => null);
         $options += $defaults;
         if (!is_numeric($value)) {
             return false;
         }
         switch (true) {
             case !is_null($options['upper']) && !is_null($options['lower']):
                 return $value > $options['lower'] && $value < $options['upper'];
             case !is_null($options['upper']):
                 return $value < $options['upper'];
             case !is_null($options['lower']):
                 return $value > $options['lower'];
         }
         return is_finite($value);
     }, 'uuid' => "/^{$alnum}{8}-{$alnum}{4}-{$alnum}{4}-{$alnum}{4}-{$alnum}{12}\$/", 'email' => function ($value) {
         return filter_var($value, FILTER_VALIDATE_EMAIL);
     }, 'url' => function ($value, $format = null, array $options = array()) {
         $options += array('flags' => array());
         return (bool) filter_var($value, FILTER_VALIDATE_URL, $options);
     });
     $isEmpty = function ($self, $params, $chain) {
         extract($params);
         return empty($value) && $value != '0' ? false : $chain->next($self, $params, $chain);
     };
     static::$_methodFilters[$class]['alphaNumeric'] = array($isEmpty);
     static::$_methodFilters[$class]['notEmpty'] = array($isEmpty);
     static::$_methodFilters[$class]['creditCard'] = array(function ($self, $params, $chain) {
         extract($params);
         $options += array('deep' => false);
         if (strlen($value = str_replace(array('-', ' '), '', $value)) < 13) {
             return false;
         }
         if (!$chain->next($self, compact('value') + $params, $chain)) {
             return false;
         }
         return $options['deep'] ? Validator::isLuhn($value) : true;
     });
     static::$_methodFilters[$class]['email'] = array(function ($self, $params, $chain) {
         extract($params);
         $defaults = array('deep' => false);
         $options += $defaults;
         if (!$chain->next($self, $params, $chain)) {
             return false;
         }
         if (!$options['deep']) {
             return true;
         }
         list($prefix, $host) = explode('@', $params['value']);
         if (getmxrr($host, $mxhosts)) {
             return is_array($mxhosts);
         }
//.........这里部分代码省略.........
开发者ID:nashadalam,项目名称:lithium,代码行数:101,代码来源:Validator.php

示例3: __init

 /**
  * Initializes the list of default validation rules.
  *
  * @return void
  */
 public static function __init()
 {
     $alnum = '[A-Fa-f0-9]';
     $class = get_called_class();
     static::$_methodFilters[$class] = array();
     static::$_rules = array('alphaNumeric' => '/^[\\p{Ll}\\p{Lm}\\p{Lo}\\p{Lt}\\p{Lu}\\p{Nd}]+$/mu', 'blank' => '/[^\\s]/', 'creditCard' => array('amex' => '/^3[4|7]\\d{13}$/', 'bankcard' => '/^56(10\\d\\d|022[1-5])\\d{10}$/', 'diners' => '/^(?:3(0[0-5]|[68]\\d)\\d{11})|(?:5[1-5]\\d{14})$/', 'disc' => '/^(?:6011|650\\d)\\d{12}$/', 'electron' => '/^(?:417500|4917\\d{2}|4913\\d{2})\\d{10}$/', 'enroute' => '/^2(?:014|149)\\d{11}$/', 'jcb' => '/^(3\\d{4}|2100|1800)\\d{11}$/', 'maestro' => '/^(?:5020|6\\d{3})\\d{12}$/', 'mc' => '/^5[1-5]\\d{14}$/', 'solo' => '/^(6334[5-9][0-9]|6767[0-9]{2})\\d{10}(\\d{2,3})?$/', 'switch' => '/^(?:49(03(0[2-9]|3[5-9])|11(0[1-2]|7[4-9]|8[1-2])|36[0-9]{2})' . '\\d{10}(\\d{2,3})?)|(?:564182\\d{10}(\\d{2,3})?)|(6(3(33[0-4]' . '[0-9])|759[0-9]{2})\\d{10}(\\d{2,3})?)$/', 'visa' => '/^4\\d{12}(\\d{3})?$/', 'voyager' => '/^8699[0-9]{11}$/', 'fast' => '/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{12}|3' . '(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})$/'), 'date' => array('dmy' => '%^(?:(?:31(\\/|-|\\.|\\x20)(?:0?[13578]|1[02]))\\1|(?:(?:29|30)' . '(\\/|-|\\.|\\x20)(?:0?[1,3-9]|1[0-2])\\2))(?:(?:1[6-9]|[2-9]\\d)?' . '\\d{2})$|^(?:29(\\/|-|\\.|\\x20)0?2\\3(?:(?:(?:1[6-9]|[2-9]\\d)?' . '(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])' . '00))))$|^(?:0?[1-9]|1\\d|2[0-8])(\\/|-|\\.|\\x20)(?:(?:0?[1-9])|' . '(?:1[0-2]))\\4(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$%', 'mdy' => '%^(?:(?:(?:0?[13578]|1[02])(\\/|-|\\.|\\x20)31)\\1|(?:(?:0?[13-9]|' . '1[0-2])(\\/|-|\\.|\\x20)(?:29|30)\\2))(?:(?:1[6-9]|[2-9]\\d)?\\d' . '{2})$|^(?:0?2(\\/|-|\\.|\\x20)29\\3(?:(?:(?:1[6-9]|[2-9]\\d)?' . '(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])' . '00))))$|^(?:(?:0?[1-9])|(?:1[0-2]))(\\/|-|\\.|\\x20)(?:0?[1-9]|1' . '\\d|2[0-8])\\4(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$%', 'ymd' => '%^(?:(?:(?:(?:(?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579]' . '[26])|(?:(?:16|[2468][048]|[3579][26])00)))(\\/|-|\\.|\\x20)' . '(?:0?2\\1(?:29)))|(?:(?:(?:1[6-9]|[2-9]\\d)?\\d{2})(\\/|-|\\.|' . '\\x20)(?:(?:(?:0?[13578]|1[02])\\2(?:31))|(?:(?:0?[1,3-9]|1[0-2])' . '\\2(29|30))|(?:(?:0?[1-9])|(?:1[0-2]))\\2(?:0?[1-9]|1\\d|2[0-8]' . '))))$%', 'dMy' => '/^((31(?!\\ (Feb(ruary)?|Apr(il)?|June?|(Sep(?=\\b|t)t?|Nov)' . '(ember)?)))|((30|29)(?!\\ Feb(ruary)?))|(29(?=\\ Feb(ruary)?\\ ' . '(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468]' . '[048]|[3579][26])00)))))|(0?[1-9])|1\\d|2[0-8])\\ (Jan(uary)?|' . 'Feb(ruary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|' . 'Oct(ober)?|(Sep(?=\\b|t)t?|Nov|Dec)(ember)?)\\ ((1[6-9]|[2-9]' . '\\d)\\d{2})$/', 'Mdy' => '/^(?:(((Jan(uary)?|Ma(r(ch)?|y)|Jul(y)?|Aug(ust)?|Oct(ober)?' . '|Dec(ember)?)\\ 31)|((Jan(uary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)' . '|(ne?))|Aug(ust)?|Oct(ober)?|(Sept|Nov|Dec)(ember)?)\\ (0?[1-9]' . '|([12]\\d)|30))|(Feb(ruary)?\\ (0?[1-9]|1\\d|2[0-8]|(29(?=,?\\ ' . '((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468]' . '[048]|[3579][26])00)))))))\\,?\\ ((1[6-9]|[2-9]\\d)\\d{2}))$/', 'My' => '%^(Jan(uary)?|Feb(ruary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|' . 'Aug(ust)?|Oct(ober)?|(Sep(?=\\b|t)t?|Nov|Dec)(ember)?)[ /]((1[6-9]' . '|[2-9]\\d)\\d{2})$%', 'my' => '%^(((0[123456789]|10|11|12)([- /.])(([1][9][0-9][0-9])|([2][0-9]' . '[0-9][0-9]))))$%'), 'hostname' => '(?:[a-z0-9][-a-z0-9]*\\.)*(?:[a-z0-9][-a-z0-9]{0,62})\\.' . '(?:(?:[a-z]{2}\\.)?[a-z]{2,4}|museum|travel)', 'ip' => '(?:(?:25[0-5]|2[0-4][0-9]|(?:(?:1[0-9])?|[1-9]?)[0-9])\\.){3}' . '(?:25[0-5]|2[0-4][0-9]|(?:(?:1[0-9])?|[1-9]?)[0-9])', 'money' => array('right' => '/^(?!0,?\\d)(?:\\d{1,3}(?:([, .])\\d{3})?(?:\\1\\d{3})*|(?:\\d+))' . '((?!\\1)[,.]\\d{2})?(?<!\\x{00a2})\\p{Sc}?$/u', 'left' => '/^(?!\\x{00a2})\\p{Sc}?(?!0,?\\d)(?:\\d{1,3}(?:([, .])\\d{3})?' . '(?:\\1\\d{3})*|(?:\\d+))((?!\\1)[,.]\\d{2})?$/u'), 'notEmpty' => '/[^\\s]+/m', 'phone' => '/^\\+?[0-9\\(\\)\\-]{10,20}$/', 'postalCode' => '/(^|\\A\\b)[A-Z0-9\\s\\-]{5,}($|\\b\\z)/i', 'regex' => '/^\\/(.+)\\/[gimsxu]*$/', 'time' => '%^((0?[1-9]|1[012])(:[0-5]\\d){0,2}([AP]M|[ap]m))$|^([01]\\d|2[0-3])' . '(:[0-5]\\d){0,2}$%', 'boolean' => function ($value) {
         return in_array($value, array(0, 1, '0', '1', true, false), true);
     }, 'decimal' => function ($value, $format = null, $options = array()) {
         $defaults = array('precision' => null);
         $options += $defaults;
         $precision = '+(?:[eE][-+]?[0-9]+)?';
         $precision = $options['precision'] ? '{' . $options['precision'] . '}' : $precision;
         return (bool) preg_match("/^[-+]?[0-9]*\\.{1}[0-9]{$precision}\$/", (string) $value);
     }, 'inList' => function ($value, $format, $options) {
         $options += array('list' => array());
         return in_array($value, $options['list']);
     }, 'lengthBetween' => function ($value, $format, $options) {
         $length = strlen($value);
         $options += array('min' => 1, 'max' => 255);
         return $length >= $options['min'] && $length <= $options['max'];
     }, 'luhn' => function ($value) {
         if (empty($value) || !is_string($value)) {
             return false;
         }
         $sum = 0;
         $length = strlen($value);
         for ($position = 1 - $length % 2; $position < $length; $position += 2) {
             $sum += $value[$position];
         }
         for ($position = $length % 2; $position < $length; $position += 2) {
             $number = $value[$position] * 2;
             $sum += $number < 10 ? $number : $number - 9;
         }
         return $sum % 10 == 0;
     }, 'numeric' => function ($value) {
         return is_numeric($value);
     }, 'inRange' => function ($value, $format, $options) {
         $defaults = array('upper' => null, 'lower' => null);
         $options += $defaults;
         if (!is_numeric($value)) {
             return false;
         }
         switch (true) {
             case !is_null($options['upper']) && !is_null($options['lower']):
                 return $value > $options['lower'] && $value < $options['upper'];
             case !is_null($options['upper']):
                 return $value < $options['upper'];
             case !is_null($options['lower']):
                 return $value > $options['lower'];
         }
         return is_finite($value);
     }, 'uuid' => "/{$alnum}{8}-{$alnum}{4}-{$alnum}{4}-{$alnum}{4}-{$alnum}{12}/");
     static::$_rules['email'] = '/^[a-z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&\'*+\\/=?^_`';
     static::$_rules['email'] .= '{|}~-]+)*@' . static::$_rules['hostname'] . '$/i';
     $urlChars = '([' . preg_quote('!"$&\'()*+,-.@_:;=') . '\\/0-9a-z]|(%[0-9a-f]{2}))';
     $url = '/^(?:(?:https?|ftps?|file|news|gopher):\\/\\/)__strict__';
     $url .= '(?:' . static::$_rules['ip'] . '|' . static::$_rules['hostname'] . ')';
     $url .= '(?::[1-9][0-9]{0,3})?(?:\\/?|\\/' . $urlChars . '*)?(?:\\?' . $urlChars . '*)?';
     $url .= '(?:#' . $urlChars . '*)?$/i';
     static::$_rules['url'] = array('strict' => str_replace('__strict__', '', $url), 'loose' => str_replace('__strict__', '?', $url));
     $emptyCheck = function ($self, $params, $chain) {
         extract($params);
         return empty($value) && $value != '0' ? false : $chain->next($self, $params, $chain);
     };
     static::$_methodFilters[$class]['alphaNumeric'] = array($emptyCheck);
     static::$_methodFilters[$class]['notEmpty'] = array($emptyCheck);
     static::$_methodFilters[$class]['creditCard'] = array(function ($self, $params, $chain) {
         extract($params);
         $options += array('deep' => false);
         if (strlen($value = str_replace(array('-', ' '), '', $value)) < 13) {
             return false;
         }
         if (!$chain->next($self, compact('value') + $params, $chain)) {
             return false;
         }
         return $options['deep'] ? Validator::isLuhn($value) : true;
     });
     $host = static::$_rules['hostname'];
     static::$_methodFilters[$class]['email'] = array(function ($self, $params, $chain) use($host) {
         extract($params);
         $defaults = array('deep' => false);
         $options += $defaults;
         if (!$chain->next($self, $params, $chain)) {
             return false;
         }
         if (!$options['deep']) {
             return true;
         }
         if (preg_match('/@(' . $host . ')$/i', $value, $regs)) {
             if (getmxrr($regs[1], $mxhosts)) {
                 return is_array($mxhosts);
             }
         }
         return false;
     });
//.........这里部分代码省略.........
开发者ID:kdambekalns,项目名称:framework-benchs,代码行数:101,代码来源:Validator.php


注:本文中的lithium\util\Validator::isLuhn方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。