1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87:
<?php
namespace Coast\Validator\Rule;
use Coast\Validator\Rule;
class Password extends Rule
{
const LOWER = 'lower';
const UPPER = 'upper';
const DIGIT = 'digit';
const SPECIAL = 'special';
protected $_lower = 0;
protected $_upper = 0;
protected $_digit = 0;
protected $_special = 0;
public function __construct($lower = 0, $upper = 0, $digit = 0, $special = 0)
{
$this->lower($lower);
$this->upper($upper);
$this->digit($digit);
$this->special($special);
}
public function lower($lower = null)
{
if (func_num_args() > 0) {
$this->_lower = $lower;
return $this;
}
return $this->_lower;
}
public function upper($upper = null)
{
if (func_num_args() > 0) {
$this->_upper = $upper;
return $this;
}
return $this->_upper;
}
public function digit($digit = null)
{
if (func_num_args() > 0) {
$this->_digit = $digit;
return $this;
}
return $this->_digit;
}
public function special($special = null)
{
if (func_num_args() > 0) {
$this->_special = $special;
return $this;
}
return $this->_special;
}
protected function _validate($value)
{
preg_match_all('/[a-z]/', $value, $matches);
if (count($matches[0]) < $this->_lower) {
$this->error(self::LOWER);
}
preg_match_all('/[A-Z]/', $value, $matches);
if (count($matches[0]) < $this->_upper) {
$this->error(self::UPPER);
}
preg_match_all('/[0-9]/', $value, $matches);
if (count($matches[0]) < $this->_digit) {
$this->error(self::DIGIT);
}
preg_match_all('/[^a-zA-Z0-9]/', $value, $matches);
if (count($matches[0]) < $this->_special) {
$this->error(self::SPECIAL);
}
}
}