Overview

Namespaces

  • None
  • Wei
    • Validator

Classes

  • Wei\Validator\All
  • Wei\Validator\AllOf
  • Wei\Validator\Alnum
  • Wei\Validator\Alpha
  • Wei\Validator\BaseValidator
  • Wei\Validator\Between
  • Wei\Validator\Blank
  • Wei\Validator\Callback
  • Wei\Validator\CharLength
  • Wei\Validator\Chinese
  • Wei\Validator\Color
  • Wei\Validator\Contains
  • Wei\Validator\CreditCard
  • Wei\Validator\Date
  • Wei\Validator\DateTime
  • Wei\Validator\Decimal
  • Wei\Validator\Digit
  • Wei\Validator\Dir
  • Wei\Validator\DivisibleBy
  • Wei\Validator\DoubleByte
  • Wei\Validator\Email
  • Wei\Validator\EndsWith
  • Wei\Validator\EqualTo
  • Wei\Validator\Exists
  • Wei\Validator\FieldExists
  • Wei\Validator\File
  • Wei\Validator\GreaterThan
  • Wei\Validator\GreaterThanOrEqual
  • Wei\Validator\IdCardCn
  • Wei\Validator\IdCardHk
  • Wei\Validator\IdCardMo
  • Wei\Validator\IdCardTw
  • Wei\Validator\IdenticalTo
  • Wei\Validator\Image
  • Wei\Validator\In
  • Wei\Validator\Ip
  • Wei\Validator\Length
  • Wei\Validator\LessThan
  • Wei\Validator\LessThanOrEqual
  • Wei\Validator\Lowercase
  • Wei\Validator\Luhn
  • Wei\Validator\MaxLength
  • Wei\Validator\MinLength
  • Wei\Validator\MobileCn
  • Wei\Validator\NaturalNumber
  • Wei\Validator\NoneOf
  • Wei\Validator\Null
  • Wei\Validator\Number
  • Wei\Validator\OneOf
  • Wei\Validator\Password
  • Wei\Validator\Phone
  • Wei\Validator\PhoneCn
  • Wei\Validator\PlateNumberCn
  • Wei\Validator\PositiveInteger
  • Wei\Validator\PostcodeCn
  • Wei\Validator\Present
  • Wei\Validator\QQ
  • Wei\Validator\RecordExists
  • Wei\Validator\Regex
  • Wei\Validator\Required
  • Wei\Validator\SomeOf
  • Wei\Validator\StartsWith
  • Wei\Validator\Time
  • Wei\Validator\Tld
  • Wei\Validator\Type
  • Wei\Validator\Uppercase
  • Wei\Validator\Url
  • Wei\Validator\Uuid
  • Overview
  • Namespace
  • Function
  1: <?php
  2: /**
  3:  * Wei Framework
  4:  *
  5:  * @copyright   Copyright (c) 2008-2015 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 password is secure enough
 13:  *
 14:  * @author      Twin Huang <twinhuang@qq.com>
 15:  */
 16: class Password extends BaseValidator
 17: {
 18:     protected $lengthTooShortMessage = '%name% must have a length greater than %minLength%';
 19: 
 20:     protected $lengthTooLongMessage = '%name% must have a length lower than %maxLength%';
 21: 
 22:     protected $missingCharTypeMessage = '%name% must contains %missingType%';
 23: 
 24:     protected $missingCharMessage = '%name% must contains %missingCount% of these characters : %missingType%';
 25: 
 26:     /**
 27:      * The name display in error message
 28:      *
 29:      * @var string
 30:      */
 31:     protected $name = 'Password';
 32: 
 33:     /**
 34:      * The names of character type
 35:      *
 36:      * @var array
 37:      */
 38:     protected $typeNames = array(
 39:         'digit' => 'digits (0-9)',
 40:         'letter' => 'letters (a-z)',
 41:         'lower' => 'lowercase letters (a-z)',
 42:         'upper' => 'uppercase letters (A-Z)',
 43:         'nonAlnum' => 'non-alphanumeric (For example: !, @, or #) characters'
 44:     );
 45: 
 46:     /**
 47:      * @var int
 48:      */
 49:     protected $minLength;
 50: 
 51:     /**
 52:      * @var int
 53:      */
 54:     protected $maxLength;
 55: 
 56:     /**
 57:      * @var bool
 58:      */
 59:     protected $needDigit = false;
 60: 
 61:     /**
 62:      * @var bool
 63:      */
 64:     protected $needLetter = false;
 65: 
 66:     /**
 67:      * @var bool
 68:      */
 69:     protected $needNonAlnum = false;
 70: 
 71:     /**
 72:      * @var int
 73:      */
 74:     protected $atLeastPresent = 0;
 75: 
 76:     /**
 77:      * @var array
 78:      */
 79:     protected $regexMap = array(
 80:         'digit' => '0-9',
 81:         'letter' => 'a-zA-Z',
 82:         'lower' => 'a-z',
 83:         'upper' => 'A-Z',
 84:         'nonAlnum' => '^0-9a-zA-Z'
 85:     );
 86: 
 87:     /**
 88:      * The parameter for $missingCharMessage
 89:      *
 90:      * @var int
 91:      */
 92:     protected $missingCount;
 93: 
 94:     /**
 95:      * The parameter for $missingCharTypeMessage and $missingCharMessage
 96:      *
 97:      * @var string
 98:      */
 99:     protected $missingType;
100: 
101:     /**
102:      * The temp variable for message parameter
103:      *
104:      * @var array
105:      */
106:     protected $missingTypes = array();
107: 
108:     /**
109:      * {@inheritdoc}
110:      */
111:     public function __invoke($input, array $options = array())
112:     {
113:         $options && $this->storeOption($options);
114:         return $this->isValid($input);
115:     }
116: 
117:     /**
118:      * {@inheritdoc}
119:      */
120:     protected function doValidate($input)
121:     {
122:         if (!$this->isString($input)) {
123:             $this->addError('notString');
124:             return false;
125:         }
126: 
127:         $length = strlen($input);
128: 
129:         if ($this->minLength && $length < $this->minLength) {
130:             $this->addError('lengthTooShort');
131:             return false;
132:         }
133: 
134:         if ($this->maxLength && $length > $this->maxLength) {
135:             $this->addError('lengthTooLong');
136:             return false;
137:         }
138: 
139:         // Find out what kind of characters are missing
140:         $missing = array();
141:         foreach ($this->regexMap as $type => $regex) {
142:             if (!preg_match('/[' . $regex . ']/', $input)) {
143:                 $missing[$type] = true;
144:             }
145:         }
146: 
147:         $needTypes = array();
148:         foreach (array('digit', 'letter', 'nonAlnum') as $type) {
149:             $propertyName = 'need' . ucfirst($type);
150:             if ($this->$propertyName && isset($missing[$type])) {
151:                 $needTypes[] = $type;
152:             }
153:         }
154: 
155:         if (count($needTypes)) {
156:             $this->missingTypes = array_intersect_key($this->typeNames, array_flip($needTypes));
157:             $this->addError('missingCharType');
158:             return false;
159:         }
160: 
161:         if ($this->atLeastPresent) {
162:             unset($missing['letter']);
163:             // 'digit', 'lower', 'upper', 'nonAlnum'
164:             $total = 4;
165:             $remains = count($missing);
166:             $needPresent = $this->atLeastPresent - ($total - $remains);
167: 
168:             if ($needPresent > 0) {
169:                 $this->missingTypes = array_intersect_key($this->typeNames, $missing);
170:                 if (count($missing) == $needPresent) {
171:                     $this->addError('missingCharType');
172:                 } else {
173:                     $this->missingCount = $needPresent;
174:                     $this->addError('missingChar');
175:                 }
176:                 return false;
177:             }
178:         }
179: 
180:         return true;
181:     }
182: 
183:     /**
184:      * {@inheritdoc}
185:      */
186:     public function getMessages($name = null)
187:     {
188:         if ($this->missingTypes) {
189:             $this->loadTranslationMessages();
190:             $types = array();
191:             foreach ($this->missingTypes as $type) {
192:                 $types[] = $this->t($type);
193:             }
194:             $this->missingType = implode(', ', $types);
195:         }
196:         return parent::getMessages($name);
197:     }
198: }
199: 
Wei Framework API documentation generated by ApiGen