From 94fbe9cc2047f814ffef37e21e3645c0a3bf9827 Mon Sep 17 00:00:00 2001 From: designbycode Date: Mon, 17 Jun 2024 02:49:10 +0200 Subject: [PATCH] update formating --- count.php | 7 +- src/BusinessNameGenerator.php | 362 ++++++++++++++-------------- tests/BusinessNameGeneratorTest.php | 14 +- 3 files changed, 193 insertions(+), 190 deletions(-) diff --git a/count.php b/count.php index 498722d..9e03d33 100644 --- a/count.php +++ b/count.php @@ -1,9 +1,8 @@
- generate()->randomArray(17) as $name) : ?> + generate()->randomArray(17) as $name) { ?> - +
diff --git a/src/BusinessNameGenerator.php b/src/BusinessNameGenerator.php index 1f811c2..61d4b8a 100644 --- a/src/BusinessNameGenerator.php +++ b/src/BusinessNameGenerator.php @@ -1,194 +1,204 @@ adjectives = $adjectives?: (new Adjectives)->default(); - $this->nouns = $nouns?: (new Nouns)->default(); - $this->amount = 1; - } + $this->adjectives = $adjectives ?: (new Adjectives)->default(); + $this->nouns = $nouns ?: (new Nouns)->default(); + $this->amount = 1; + } - /** - * @param array|string $adjectivesList - * @return $this - */ - public function setAdjectives(array|string $adjectivesList): self - { - $this->adjectives = is_array($adjectivesList) ? $adjectivesList : $this->adjectivesList($adjectivesList); - return $this; - } + /** + * @param array|string $adjectivesList + * @return $this + */ + public function setAdjectives(array|string $adjectivesList): self + { + $this->adjectives = is_array($adjectivesList) ? $adjectivesList : $this->adjectivesList($adjectivesList); - /** - * @param array|string $nounsList - * @return $this - */ - public function setNouns(array|string $nounsList): self - { - $this->nouns = is_array($nounsList) ? $nounsList : $this->nounsList($nounsList); - return $this; - } + return $this; + } - /** - * @param int $amount - * @return $this - */ - public function amount(int $amount): self - { - $this->amount = $amount; - return $this; - } + /** + * @param array|string $nounsList + * @return $this + */ + public function setNouns(array|string $nounsList): self + { + $this->nouns = is_array($nounsList) ? $nounsList : $this->nounsList($nounsList); - /** - * @param string $adjectivesCategory - * @param string $nounsCategory - * @return BusinessNameGenerator - */ - public function generate(string $adjectivesCategory = 'default', string $nounsCategory = 'default'): self - { - $adjectives = $this->adjectivesList($adjectivesCategory); - $nouns = $this->nounsList($nounsCategory); - - for ($i = 0; $i < $this->amount; $i++) { - $adjective = $adjectives[array_rand($adjectives)]; - $noun = $nouns[array_rand($nouns)]; - $this->names[] = $adjective.' '.$noun; - } - - return $this; - } + return $this; + } - /** - * Returns an array of generated business names. - * - * @return array - * @example - * $generator = new BusinessNameGenerator(); - * $generator->generate(); - * $names = $generator->get(); // returns an array of generated business names - */ - public function get(): array - { - return $this->names; - } + /** + * @param int $amount + * @return $this + */ + public function amount(int $amount): self + { + $this->amount = $amount; - /** - * Returns the last generated business name. - * - * @return string - * @example - * $generator = new BusinessNameGenerator(); - * $generator->generate(); - * $last_name = $generator->last(); // returns the last generated business name - */ - public function last(): string - { - return array_slice($this->names, -1)[0]; - } + return $this; + } - /** - * Returns the first generated business name. - * - * @return string - * @example - * $generator = new BusinessNameGenerator(); - * $generator->generate(); - * $first_name = $generator->first(); // returns the first generated business name - */ - public function first(): string - { - return reset($this->names); - } + /** + * @param string $adjectivesCategory + * @param string $nounsCategory + * @return BusinessNameGenerator + */ + public function generate(string $adjectivesCategory = 'default', string $nounsCategory = 'default'): self + { + $adjectives = $this->adjectivesList($adjectivesCategory); + $nouns = $this->nounsList($nounsCategory); - /** - * Returns a random generated business name. - * - * @return string - * @example - * $generator = new BusinessNameGenerator(); - * $generator->generate(); - * $random_name = $generator->random(); // returns a random generated business name - */ - public function random(): string - { - return $this->names[array_rand($this->names)]; + for ($i = 0; $i < $this->amount; $i++) { + $adjective = $adjectives[array_rand($adjectives)]; + $noun = $nouns[array_rand($nouns)]; + $this->names[] = $adjective.' '.$noun; } - /** - * Returns an array of random generated business names. - * - * @param int $number - * @return array - * @example - * $generator = new BusinessNameGenerator(); - * $generator->generate(); - * $randomNames = $generator->randomArray(3); // returns an array of 3 random generated business names - */ - public function randomArray(int $number = 1): array - { - $array = $this->names; - // Get random keys - $randomKeys = array_rand($array, $number); - - // If you want to get the actual values, you can use array_map or array slicing - $this->names = array_map(function($key) use ($array) { - return $array[$key]; - }, $randomKeys); - - return $this->names; - } + return $this; + } - /** - * Returns a string representation of the generated business names, separated by a given separator. - * - * @param string $separator - * @return string - * @example - * $generator = new BusinessNameGenerator(); - * $generator->generate(); - * $names_string = $generator->toString(', '); // returns a string representation of the generated business names, separated by commas - */ - public function toString($separator = ', '): string - { - return implode($separator, $this->names); - } + /** + * Returns an array of generated business names. + * + * @return array + * + * @example + * $generator = new BusinessNameGenerator(); + * $generator->generate(); + * $names = $generator->get(); // returns an array of generated business names + */ + public function get(): array + { + return $this->names; + } - /** - * @param string $adjectivesCategory - * @return array - */ - private function adjectivesList(string $adjectivesCategory): array - { - return match (strtolower($adjectivesCategory)) { - 'funny' => (new Adjectives)->funny(), - 'playful' => (new Adjectives)->playful(), - 'color' => (new Adjectives)->color(), - 'all' => (new Adjectives)->all(), - default => $this->adjectives, - }; - } + /** + * Returns the last generated business name. + * + * @return string + * + * @example + * $generator = new BusinessNameGenerator(); + * $generator->generate(); + * $last_name = $generator->last(); // returns the last generated business name + */ + public function last(): string + { + return array_slice($this->names, -1)[0]; + } - /** - * @param string $nounsCategory - * @return array - */ - private function nounsList(string $nounsCategory): array - { - return match (strtolower($nounsCategory)) { - 'funny' => (new Nouns)->funny(), - 'playful' => (new Nouns)->playful(), - 'color' => (new Nouns)->color(), - 'all' => (new Nouns)->all(), - default => $this->nouns, - }; - } + /** + * Returns the first generated business name. + * + * @return string + * + * @example + * $generator = new BusinessNameGenerator(); + * $generator->generate(); + * $first_name = $generator->first(); // returns the first generated business name + */ + public function first(): string + { + return reset($this->names); + } + + /** + * Returns a random generated business name. + * + * @return string + * + * @example + * $generator = new BusinessNameGenerator(); + * $generator->generate(); + * $random_name = $generator->random(); // returns a random generated business name + */ + public function random(): string + { + return $this->names[array_rand($this->names)]; + } + + /** + * Returns an array of random generated business names. + * + * @param int $number + * @return array + * + * @example + * $generator = new BusinessNameGenerator(); + * $generator->generate(); + * $randomNames = $generator->randomArray(3); // returns an array of 3 random generated business names + */ + public function randomArray(int $number = 1): array + { + $array = $this->names; + // Get random keys + $randomKeys = array_rand($array, $number); + + // If you want to get the actual values, you can use array_map or array slicing + $this->names = array_map(function ($key) use ($array) { + return $array[$key]; + }, $randomKeys); + + return $this->names; + } + + /** + * Returns a string representation of the generated business names, separated by a given separator. + * + * @param string $separator + * @return string + * + * @example + * $generator = new BusinessNameGenerator(); + * $generator->generate(); + * $names_string = $generator->toString(', '); // returns a string representation of the generated business names, separated by commas + */ + public function toString($separator = ', '): string + { + return implode($separator, $this->names); + } + + /** + * @param string $adjectivesCategory + * @return array + */ + private function adjectivesList(string $adjectivesCategory): array + { + return match (strtolower($adjectivesCategory)) { + 'funny' => (new Adjectives)->funny(), + 'playful' => (new Adjectives)->playful(), + 'color' => (new Adjectives)->color(), + 'all' => (new Adjectives)->all(), + default => $this->adjectives, + }; + } + + /** + * @param string $nounsCategory + * @return array + */ + private function nounsList(string $nounsCategory): array + { + return match (strtolower($nounsCategory)) { + 'funny' => (new Nouns)->funny(), + 'playful' => (new Nouns)->playful(), + 'color' => (new Nouns)->color(), + 'all' => (new Nouns)->all(), + default => $this->nouns, + }; } +} diff --git a/tests/BusinessNameGeneratorTest.php b/tests/BusinessNameGeneratorTest.php index d5f0af3..412c637 100644 --- a/tests/BusinessNameGeneratorTest.php +++ b/tests/BusinessNameGeneratorTest.php @@ -10,7 +10,6 @@ $this->generator = new BusinessNameGenerator($this->adjectives, $this->nouns); }); - it('can generates a business name with custom adjectives and nouns', function () { $businessName = $this->generator->generate()->first(); $parts = explode(' ', $businessName); @@ -19,7 +18,6 @@ ->and($this->nouns)->toContain($parts[1]); }); - it('can generates a business name using funny adjectives and nouns', function () { $parts = explode(' ', $this->generator->generate('funny', 'funny')->first()); @@ -80,8 +78,6 @@ } }); - - it('can generate single name', function () { expect($this->generator->generate()->first()) ->toBeString(); @@ -109,13 +105,11 @@ }); it('can use string as parameter for Adjectives', function () { - expect($this->generator->setAdjectives('color')->generate()->first()) - ->toBeString(); + expect($this->generator->setAdjectives('color')->generate()->first()) + ->toBeString(); }); it('can use array as parameter for Adjectives', function () { - expect($this->generator->setAdjectives(['silver', 'gold'])->generate()->first()) - ->toBeString(); + expect($this->generator->setAdjectives(['silver', 'gold'])->generate()->first()) + ->toBeString(); }); - -