Overview

Namespaces

  • None
  • PHP
  • Wei
    • Validator

Classes

  • All
  • AllOf
  • Alnum
  • Alpha
  • BaseValidator
  • Between
  • Blank
  • Callback
  • CharLength
  • Chinese
  • Color
  • Contains
  • CreditCard
  • Date
  • DateTime
  • Decimal
  • Digit
  • Dir
  • DivisibleBy
  • DoubleByte
  • Email
  • EndsWith
  • EqualTo
  • Exists
  • FieldExists
  • File
  • GreaterThan
  • GreaterThanOrEqual
  • IdCardCn
  • IdCardHk
  • IdCardMo
  • IdCardTw
  • IdenticalTo
  • Image
  • In
  • Ip
  • Length
  • LessThan
  • LessThanOrEqual
  • Lowercase
  • Luhn
  • MaxLength
  • MinLength
  • MobileCn
  • NaturalNumber
  • NoneOf
  • Null
  • Number
  • OneOf
  • Password
  • Phone
  • PhoneCn
  • PlateNumberCn
  • PositiveInteger
  • PostcodeCn
  • Present
  • QQ
  • RecordExists
  • Regex
  • Required
  • SomeOf
  • StartsWith
  • Time
  • Tld
  • Type
  • Uppercase
  • Url
  • Uuid
  • Overview
  • Namespace
  • Class
  • Tree
 1: <?php
 2: /**
 3:  * Wei Framework
 4:  *
 5:  * @copyright   Copyright (c) 2008-2013 Twin Huang
 6:  * @license     http://opensource.org/licenses/mit-license.php MIT License
 7:  */
 8: 
 9: namespace Wei\Validator;
10: 
11: /**
12:  * Check if the input is valid by the Luhn algorithm
13:  *
14:  * @author      Twin Huang <twinhuang@qq.com>
15:  */
16: class Luhn extends BaseValidator
17: {
18:     protected $invalidMessage = '%name% is not a valid number';
19: 
20:     /**
21:      * {@inheritdoc}
22:      */
23:     protected function doValidate($input)
24:     {
25:         if (!$this->isString($input)) {
26:             $this->addError('notString');
27:             return false;
28:         }
29: 
30:         $checksum = $this->getChecksum(substr($input, 0, -1));
31:         if ($checksum != substr($input, -1)) {
32:             return false;
33:         }
34: 
35:         return true;
36:     }
37: 
38:     /**
39:      * Return the checksum char of luhn algorithm
40:      *
41:      * @param string $string
42:      * @return string
43:      */
44:     protected function getChecksum($string)
45:     {
46:         $checksum = '';
47:         foreach (str_split(strrev($string)) as $i => $d) {
48:             $checksum .= ($i % 2 === 0) ? ($d * 2) : $d;
49:         }
50:         return (10 - array_sum(str_split($checksum)) % 10) % 10;
51:     }
52: }
53: 
Wei Framework API documentation generated by ApiGen