gregor-j/correct-horse

View on GitHub
src/Generators/DictionaryWordTrait.php

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
<?php

namespace GregorJ\CorrectHorse\Generators;

use GregorJ\CorrectHorse\DictionaryInterface;

/**
 * Trait DictionaryWordTrait
 */
trait DictionaryWordTrait
{
    /**
     * @var DictionaryInterface
     */
    private $dictionary;

    /**
     * @param  DictionaryInterface  $dictionary
     */
    public function __construct(DictionaryInterface $dictionary)
    {
        $this->dictionary = $dictionary;
        $this->reset();
    }

    /**
     * Has a random number already been generated?
     * @param  string  $word
     * @return bool
     */
    private function hasWord(string $word): bool
    {
        return in_array($word, $this->items, true);
    }

    /**
     * Add a randomly generated word.
     * @return void
     */
    public function add(): void
    {
        do {
            $word = $this->getRandomWord();
        } while ($this->hasWord($word));
        $this->items[] = $word;
    }

    // PHP > 8.0
    //abstract private function getRandomWord(): string;
}