Skip to content

Commit

Permalink
MAINT: return values and php8 support
Browse files Browse the repository at this point in the history
  • Loading branch information
Iliain committed Oct 16, 2024
1 parent 63027c8 commit 5ab08c2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 18 deletions.
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"description": "A PHP version of bryanmylee's zoo-ids, originally written in JavaScript",
"license": "BSD-3-Clause",
"keywords": [
"zoo-ids",
"usernames",
"random"
"zoo-ids",
"usernames",
"random"
],
"authors": [
{
Expand All @@ -14,7 +14,7 @@
],
"homepage": "http://github.com/Iliain/php-zoo-ids",
"require": {
"php": "^7"
"php": "^7 || ^8"
},
"autoload": {
"psr-4": {
Expand Down
6 changes: 6 additions & 0 deletions src/RandomizerArrays.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

/**
* @var array $nounArray
*/
$nounArray = [
'aardvark',
'albatross',
Expand Down Expand Up @@ -224,6 +227,9 @@
'zebra',
];

/*
* @var array $adjectiveArray
*/
$adjectiveArray = [
'abandoned',
'able',
Expand Down
48 changes: 34 additions & 14 deletions src/UsernameRandomizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,26 @@

/**
* Class UsernameRandomizer
* Generates a random username comprised of multiple adjectives and one noun. Can be customized with custom word lists.
* @package Iliain\ZooIDs
*/
class UsernameRandomizer
{
/**
* @var array
*/
static $adjectiveList;

/**
* @var array
*/
static $nounList;

/**
* UsernameRandomizer constructor.
* @param null|array $customAdjectives
* @param null|array $customNouns
*/
public function __construct($customAdjectives = null, $customNouns = null)
{
include __DIR__ . '/RandomizerArrays.php';
Expand All @@ -29,25 +41,31 @@ public function __construct($customAdjectives = null, $customNouns = null)
}
}

public function setAdjectives($customAdjectives)
/**
* @param array $customAdjectives
*/
public function setAdjectives($customAdjectives): void
{
self::$adjectiveList = $customAdjectives;
}

public function setNouns($customNouns)
/**
* @param array $customNouns
*/
public function setNouns($customNouns): void
{
self::$nounList = $customNouns;
}

/**
* Generates a random username comprised of multiple adjectives and one noun
* @param $seed string|int|null The seed to use for the randomization, defaults to current time
* @param $numAdjectives int The number of adjectives to use in the username
* @param $delimiter string The delimiter to use between the adjectives and the noun
* @param $caseStyle string The case style to use for the username
* @param string|int|null $seed The seed to use for the randomization, defaults to current time
* @param int $numAdjectives The number of adjectives to use in the username
* @param string $delimiter The delimiter to use between the adjectives and the noun
* @param string $caseStyle The case style to use for the username
* @return string
*/
public static function generateID($seed = null, $numAdjectives = 2, $delimiter = '', $caseStyle = 'titlecase')
public static function generateID($seed = null, $numAdjectives = 2, $delimiter = '', $caseStyle = 'titlecase'): string
{
if (!$seed) {
$seed = (string)strtotime('now');
Expand Down Expand Up @@ -86,21 +104,21 @@ public static function generateID($seed = null, $numAdjectives = 2, $delimiter =

/**
* Generates a random number between 0 and the length of the provided array, then selects the word at that index
* @param $word string The word to format
* @param $words array The array of words to select from
* @param string $word The word to format
* @param array $words The array of words to select from
*/
public static function getRandomElement($words = [])
public static function getRandomElement($words = []): string
{
$index = rand(0, count($words) - 1);
return $words[$index];
}

/**
* Formats the provided word according to the provided options
* @param $word string
* @param $options array
* @param string $word
* @param array $options
*/
public static function getFormattedWord($word, $options)
public static function getFormattedWord($word, $options): string
{
if (strpos($word, '-') !== false) {
$wordArray = explode('-', $word);
Expand All @@ -110,6 +128,7 @@ public static function getFormattedWord($word, $options)

return implode('-', $wordArray);
}

switch ($options['caseStyle']) {
case 'titlecase':
case 'camelcase':
Expand All @@ -125,9 +144,10 @@ public static function getFormattedWord($word, $options)
}

/**
* Toggles the case of each letter in the provided word. E.g. "hello" becomes "HeLlO"
* @param $word string
*/
public static function getToggleCaseWord($word)
public static function getToggleCaseWord($word): string
{
$wordArray = str_split($word);
$wordArray = array_map(function($letter, $index) {
Expand Down

0 comments on commit 5ab08c2

Please sign in to comment.