Skip to content

Commit

Permalink
extract generator methods (#130)
Browse files Browse the repository at this point in the history
Co-authored-by: Jan Teriete <[email protected]>
  • Loading branch information
LeisureLarry and Jan Teriete committed Sep 29, 2021
1 parent 434f3f4 commit 9207253
Showing 1 changed file with 30 additions and 13 deletions.
43 changes: 30 additions & 13 deletions src/Generator/DefaultGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,9 @@ public function make($name, $length = 2, $uppercase = false, $ascii = false, $rt

// if name contains single word, use first N character
if ($words->count() === 1) {
$initial = (string) $words->first();

if (strlen($this->name) >= $length) {
$initial = Str::substr($this->name, 0, $length);
}
$initial = $this->getInitialFromOneWord($words, $length);
} else {
// otherwise, use initial char from each word
$initials = new Collection();
$words->each(function ($word) use ($initials) {
$initials->push(Str::substr($word, 0, 1));
});

$initial = $initials->slice(0, $length)->implode('');
$initial = $this->getInitialFromMultipleWords($words, $length);
}

if ($uppercase) {
Expand All @@ -43,7 +33,7 @@ public function make($name, $length = 2, $uppercase = false, $ascii = false, $rt
return $initial;
}

private function setName($name, $ascii)
protected function setName($name, $ascii)
{
if (is_array($name)) {
throw new \InvalidArgumentException(
Expand All @@ -66,4 +56,31 @@ private function setName($name, $ascii)

$this->name = $name;
}

protected function getInitialFromOneWord($words, $length)
{
$initial = (string)$words->first();

if (strlen($this->name) >= $length) {
$initial = Str::substr($this->name, 0, $length);
}

return $initial;
}

protected function getInitialFromMultipleWords($words, $length)
{
// otherwise, use initial char from each word
$initials = new Collection();
$words->each(function ($word) use ($initials) {
$initials->push(Str::substr($word, 0, 1));
});

return $this->selectInitialFromMultipleInitials($initials, $length);
}

protected function selectInitialFromMultipleInitials($initials, $length)
{
return $initials->slice(0, $length)->implode('');
}
}

0 comments on commit 9207253

Please sign in to comment.