- var_dump: documentation ( source)
- str_replace: documentation ( source)
- strtolower: documentation ( source)
- str_split: documentation ( source)
<?php
class PassphraseException extends Exception {}
class PassphraseTooShortException extends PassphraseException {}
class PassphraseTooLongException extends PassphraseException {}
class PassphraseRequiresLetterException extends PassphraseException {}
class PassphraseRequiresNumberOrSpecialException extends PassphraseException {}
class Passphrase {
static function validate($passphrase) {
$len = strlen($passphrase);
if ($len < 15) {
throw new PassphraseTooShortException('Jesus fucking christ, read the requirements you fuck!');
}
if ($len > 140) {
throw new PassphraseTooLongException('Are you fucking kidding me? How are you going to remember this shit...');
}
$no_alpha = str_replace(str_split('abcdefghijklmnopqrstuvwxyz'), '', strtolower($passphrase));
if ($no_alpha === $passphrase) {
throw new PassphraseRequiresLetterException('Fucking idiot.');
}
if ($no_alpha === '') {
throw new PassphraseRequiresNumberOrSpecialException('You fucking fuck, fuck you fucking fucker fuckface fuck.');
}
}
static function isValid($passphrase) {
try {
static::validate($passphrase);
} catch (PassphraseException $e) {
return false;
}
return true;
}
}
var_dump(Passphrase::isValid('It was the best of times, it was the worst of times.'));