<?php
require 'PasswordGenerator.php';
require 'PasswordChecker.php';
$passwd_gen = new PasswordGenerator;
$passwd = $passwd_gen->length(6, 40)
->ruleRange('a', 'z')
->ruleRange('A', 'Z', 2)
->ruleRange('0', '9', 2)
->ruleAny('~!@#%*_+', 1)
->generate();
//var_dump($passwd_gen);
$passwd_check = new PasswordChecker;
$valid = $passwd_check->length(6, 40)
->ruleRange('a', 'z')
->ruleRange('A', 'Z', 2)
->ruleRange('0', '9', 2)
->ruleAny('~!@#%*_+', 1)
->check($passwd);
var_dump($valid);
<?php
class PasswordChecker
{
private $rules = [];
private $password;
private $length;
private $result = [];
public function setPassword($password)
{
$this->password = str_split($password);
}
public function getPassword()
{
return implode($this->password);
}
public function length($min, $max)
{
$this->length = [$min, $max];
return $this;
}
public function check($password)
{
$this->setPassword($password);
$passwd_length = count($this->password);
if ($passwd_length < $this->length[0] || $passwd_length > $this->length[1]) {
$this->result[] = 'Wrong length';
}
foreach ($this->rules as &$rule) {
$required_count = array_pop($rule);
$method = count($rule) > 3 ? 'checkAnyChar' : 'checkCharRange';
$this->result[] = $this->$method($rule, $required_count);
}
return empty($this->result) ? true : $this->result;
}
public function ruleRange($start, $end, $count = 1)
{
$this->rules[] = [ord($start), ord($end), $count];
return $this;
}
public function ruleAny($chars, $count = 1)
{
$chars = str_split($chars);
$chars[] = $count;
$this->rules[] = $chars;
return $this;
}
private function checkChar($char)
{
if (is_numeric($char)) {
$char = chr($char);
}
return in_array($char, $this->password);
}
private function checkAnyChar($chars, $required_count)
{
foreach ($chars as $char) {
if ($this->checkChar($char) && ++$count == $required_count) {
return true;
}
}
return false;
}
private function checkCharRange($range, $required_count)
{
foreach ($this->password as $char) {
if ($this->charInRange($char, $range) && ++$count == $required_count) {
return true;
}
}
return false;
}
private function charInRange($char, $range) {
$char_code = ord($char);
return $char_code >= $range[0] && $char_code <= $range[1];
}
}
<?php
class PasswordGenerator
{
private $required_chars = [];
private $chars = [];
private $min;
private $max;
public function __construct($min = 6, $max = 8)
{
$this->min = $min;
$this->max = $max;
}
public function length($min, $max)
{
$this->min = $min;
$this->max = $max;
return $this;
}
public function ruleRange($start, $end, $min = false)
{
$this->addRule([ord($start), ord($end)], $min);
return $this;
}
public function ruleAny($chars, $min = false)
{
$char_codes = array_map(function ($char) {
return ord($char);
}, str_split($chars));
$this->addRule($char_codes, $min);
return $this;
}
public function generate()
{
$passwd = [];
foreach ($this->required_chars as $char) {
$i = 0;
while ($i++ < $char[count($char) - 1]) {
if (count($char) == 3) {
$passwd[] = chr(rand($char[0], $char[1]));
} else {
$index = rand(0, count($char) - 2);
$passwd[] = chr($char[$index]);
}
}
}
$i = $this->min + count($passwd);
while ($i++ <= $this->max) {
$index = rand(0, count($this->chars) - 1);
$passwd[] = chr(rand($this->chars[$index][0], $this->chars[$index][1]));
}
shuffle($passwd);
return implode('', $passwd);
}
private function addRule($char, $requirements = false)
{
if ($requirements) {
$char[] = $requirements;
$this->required_chars[] = $char;
} else {
$this->chars[] = $char;
}
}
}