これは問題のかわいい解決策だと思います(エラーを収集して、ユーザーにわかりやすいメッセージを表示することもできます):
const SECURELEVEL_LOW = 1;
const SECURELEVEL_MEDIUM = 2;
const SECURELEVEL_HIGH = 3;
const SECURELEVEL_SERVICE = 4;
const CONTAINS_LETTERS = 1;
const CONTAINS_DIGITS = 2;
const CONTAINS_CASESENSITIVELETTERS = 4;
const CONTAINS_SPECIALCHARS = 8;
public function _isValid($value) {
$valid = true;
switch ($this->_options['secureLevel']) {
case self::SECURELEVEL_SERVICE:
$minLength = 20;
$hasToContain = self::CONTAINS_LETTERS + self::CONTAINS_DIGITS + self::CONTAINS_CASESENSITIVELETTERS + self::CONTAINS_SPECIALCHARS;
break;
case self::SECURELEVEL_HIGH:
$minLength = 8;
$hasToContain = self::CONTAINS_LETTERS + self::CONTAINS_DIGITS + self::CONTAINS_CASESENSITIVELETTERS;
break;
case self::SECURELEVEL_LOW:
$minLength = 4;
$hasToContain = 0;
break;
case self::SECURELEVEL_MEDIUM:
default:
$minLength = 5;
$hasToContain = self::CONTAINS_LETTERS + self::CONTAINS_DIGITS;
break;
}
if (strlen($value) < $minLength) {
$valid = false;
}
if (strpos($value, ' ') !== false) {
$valid = false;
}
if ($hasToContain & self::CONTAINS_LETTERS) {
// Password has to contain letters
if (!preg_match('/[a-z]/i', $value)) {
$valid = false;
}
}
if ($hasToContain & self::CONTAINS_DIGITS) {
// Password has to contain numbers
if (!preg_match('/[0-9]/', $value)) {
$valid = false;
}
}
if ($hasToContain & self::CONTAINS_CASESENSITIVELETTERS) {
// Password has to contain small and capital letters
if (!preg_match('/[a-z]/', $value) || !preg_match('/[A-Z]/', $value)) {
$valid = false;
}
}
if ($hasToContain & self::CONTAINS_SPECIALCHARS) {
// Password has to contain a special character
if (!preg_match('/[^0-9a-zA-Z]/', $value)) {
$valid = false;
}
}
return $valid;
}