public function generatePassword()
{
$wordList = $this->generateWordList();
$words = \count($wordList);
if (!$words) {
throw new WordsNotFoundException('No words selected.');
}
$password = '';
$wordCount = $this->getWordCount();
if ($this->getLength() > 0 && ($this->getMinPasswordLength() > $this->getLength() || $this->getMaxPasswordLength() < $this->getLength())) {
throw new ImpossiblePasswordLengthException();
}
if (!$this->getLength()) {
for ($i = 0; $i < $wordCount; $i++) {
if ($i) {
$password .= $this->getWordSeparator();
}
$password .= $this->randomWord();
}
return $password;
}
while (--$wordCount) {
$thisMin = $this->getLength() - strlen($password) - $wordCount * $this->getMaxWordLength() - strlen($this->getWordSeparator()) * $wordCount;
$thisMax = $this->getLength() - strlen($password) - $wordCount * $this->getMinWordLength() - strlen($this->getWordSeparator()) * $wordCount;
if ($thisMin < 1) {
$thisMin = $this->getMinWordLength();
}
if ($thisMax > $this->getMaxWordLength()) {
$thisMax = $this->getMaxWordLength();
}
$length = $this->randomInteger($thisMin, $thisMax);
$password .= $this->randomWord($length, $length);
if ($wordCount) {
$password .= $this->getWordSeparator();
}
}
$desiredLength = $this->getLength() - strlen($password);
$password .= $this->randomWord($desiredLength, $desiredLength);
return $password;
}