Skip to content

Commit

Permalink
refactor: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
gere-lajos committed Apr 19, 2024
1 parent 3c15d5f commit 974b199
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 48 deletions.
61 changes: 25 additions & 36 deletions src/Meow.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,64 +13,53 @@ public function __construct(
$this->dictionary = new Dictionary();
}

public function name(): string
public function name(int $count = 1): string|array
{
return $this->pickRandom($this->dictionary->names());
if($count === 1) {
return $this->pickRandom($this->dictionary->names());
}

return $this->pickRandomToArray(fn () => $this->name(), $count);
}

public function names(int $count = 1): array
public function word(int $count = 1): string|array
{
$names = [];
for ($i = 0; $i < $count; $i++) {
$names[] = $this->name();
if($count === 1) {
return $this->pickRandom($this->dictionary->words());
}
return $names;
}

public function word(): string
{
return $this->pickRandom($this->dictionary->words());
return $this->pickRandomToArray(fn () => $this->word(), $count);
}

public function words(int $count = 1): array
public function sentence(int $count = 1, int $minWords = Config::MIN_WORDS, int $maxWords = Config::MAX_WORDS): string|array
{
$words = [];
for ($i = 0; $i < $count; $i++) {
$words[] = $this->word();
if($count === 1) {
return ucfirst(implode(' ', $this->words(mt_rand($minWords, $maxWords)))) . '.';
}
return $words;
}

public function sentence(int $minWords = Config::MIN_WORDS, int $maxWords = Config::MAX_WORDS): string
{
return ucfirst(implode(' ', $this->words(mt_rand($minWords, $maxWords)))) . '.';
return $this->pickRandomToArray(fn () => $this->sentence(), $count);
}

public function sentences(int $count = 1): array
public function paragraph(int $count = 1, int $minSentences = Config::MIN_SENTENCES, int $maxSentences = Config::MAX_SENTENCES): string|array
{
$sentences = [];
for ($i = 0; $i < $count; $i++) {
$sentences[] = $this->sentence();
if($count === 1) {
return implode(' ', $this->sentence(mt_rand($minSentences, $maxSentences)));
}
return $sentences;

return $this->pickRandomToArray(fn () => $this->paragraph(), $count);
}

public function paragraph(int $minSentences = Config::MIN_SENTENCES, int $maxSentences = Config::MAX_SENTENCES): string
private function pickRandom(array $options): string
{
return implode(' ', $this->sentences(mt_rand($minSentences, $maxSentences)));
return $options[array_rand($options)];
}

public function paragraphs(int $count = 1): array
private function pickRandomToArray(callable $callback, int $count): array
{
$paragraphs = [];
$items = [];
for ($i = 0; $i < $count; $i++) {
$paragraphs[] = $this->paragraph();
$items[] = $callback();
}
return $paragraphs;
}

private function pickRandom(array $options): string
{
return $options[array_rand($options)];
return $items;
}
}
8 changes: 4 additions & 4 deletions src/playground.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@

// Generate a name
echo 'A name: ' . $meow->name() . PHP_EOL;
echo 'More names: ' . implode(', ', $meow->names(10)) . PHP_EOL;
echo 'More names: ' . implode(', ', $meow->name(10)) . PHP_EOL;
echo $separator . PHP_EOL;

// Generate a word
echo 'A word: ' . $meow->word() . PHP_EOL;
echo 'More words: ' . implode(', ', $meow->words(10)) . PHP_EOL;
echo 'More words: ' . implode(', ', $meow->word(10)) . PHP_EOL;
echo $separator . PHP_EOL;

// Generate a sentence
echo 'A sentence: ' . $meow->sentence() . PHP_EOL;
echo 'More sentences: ' . implode(', ', $meow->sentences(10)) . PHP_EOL;
echo 'More sentences: ' . implode(', ', $meow->sentence(10)) . PHP_EOL;
echo $separator . PHP_EOL;

// Generate a paragraph
echo 'A paragraph: ' . $meow->paragraph() . PHP_EOL;
echo 'More paragraphs: ' . implode(PHP_EOL, $meow->paragraphs(10)) . PHP_EOL;
echo 'More paragraphs: ' . implode(PHP_EOL, $meow->paragraph(10)) . PHP_EOL;
16 changes: 8 additions & 8 deletions tests/MeowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public function testItCanGenerateAName(): void

public function testItCanGenerateMoreNames(): void
{
$this->assertIsArray($this->meow->names(10));
$this->assertCount(10, $this->meow->names(10));
$this->assertIsArray($this->meow->name(10));
$this->assertCount(10, $this->meow->name(10));
}

public function testItCanGenerateWord(): void
Expand All @@ -37,8 +37,8 @@ public function testItCanGenerateWord(): void

public function testItCanGenerateMoreWords(): void
{
$this->assertIsArray($this->meow->words(10));
$this->assertCount(10, $this->meow->words(10));
$this->assertIsArray($this->meow->word(10));
$this->assertCount(10, $this->meow->word(10));
}

public function testItCanGenerateSentence(): void
Expand All @@ -55,8 +55,8 @@ public function testSentenceHasCorrectFormat(): void

public function testItCanGenerateMoreSentences(): void
{
$this->assertIsArray($this->meow->sentences(10));
$this->assertCount(10, $this->meow->sentences(10));
$this->assertIsArray($this->meow->sentence(10));
$this->assertCount(10, $this->meow->sentence(10));
}

public function testItCanGenerateParagraph(): void
Expand All @@ -66,7 +66,7 @@ public function testItCanGenerateParagraph(): void

public function testItCanGenerateMoreParagraphs(): void
{
$this->assertIsArray($this->meow->paragraphs(10));
$this->assertCount(10, $this->meow->paragraphs(10));
$this->assertIsArray($this->meow->paragraph(10));
$this->assertCount(10, $this->meow->paragraph(10));
}
}

0 comments on commit 974b199

Please sign in to comment.