-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pretty much a complete rewrite, but it's only one class so I won't as…
…sume you're very impressed
- Loading branch information
1 parent
6568a40
commit 3af2714
Showing
8 changed files
with
1,125 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
<?php | ||
|
||
require_once(__DIR__.'/src/Core.php'); | ||
|
||
if (!function_exists('random_factor')) { | ||
function random_factor($lang = 'en', $spacing = ' ', $adjectives = 1, $nouns = 1) { | ||
return RandomFactor\Core::generate($lang, $spacing, $adjectives, $nouns); | ||
} | ||
} | ||
function random_factor($lang = 'en', $spacing = ' ', $adjectives = 1, $nouns = 1) | ||
{ | ||
return marcuspi\RandomFactor\Words::create() | ||
->language($lang) | ||
->spacing($spacing) | ||
->adjectives($adjectives) | ||
->nouns($nouns) | ||
->generate(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace marcuspi\RandomFactor; | ||
|
||
class Words | ||
{ | ||
private static $languages = [ | ||
'no' => 'no-nb', | ||
'en' => 'en' | ||
]; | ||
|
||
private $language; | ||
private $spacing; | ||
private $nouns; | ||
private $adjectives; | ||
|
||
/** | ||
* Creates new Words. | ||
* | ||
* @access public | ||
* @param string $language The language to use (default: "en") | ||
* @param string $spacing Character(s) to used to split the words (default: " ") | ||
* @param int $nouns Number of nouns (default: 1) | ||
* @param int $adjectives Number of adjectives (default: 1) | ||
*/ | ||
public function __construct($language = "en", $spacing = " ", $nouns = 1, $adjectives = 1) | ||
{ | ||
$this->language($language); | ||
$this->spacing($spacing); | ||
$this->nouns($nouns); | ||
$this->adjectives($adjectives); | ||
} | ||
|
||
/** | ||
* Sets which language that should be used | ||
* | ||
* @access public | ||
* @param string $language One of "no", "en". | ||
* @return self | ||
*/ | ||
public function language(string $language): Words | ||
{ | ||
if (!array_key_exists(mb_strtolower($language), static::$languages)) { | ||
throw new \Exception('Language \'' . $language . '\' not found'); | ||
} | ||
|
||
$this->language = mb_strtolower($language); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Sets the character(s) to used to split the words | ||
* | ||
* @access public | ||
* @param string $spacing | ||
* @return self | ||
*/ | ||
public function spacing(string $spacing): Words | ||
{ | ||
$this->spacing = $spacing; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Sets the number of nouns | ||
* | ||
* @access public | ||
* @param int $nouns A positive integer | ||
* @return self | ||
*/ | ||
public function nouns(int $nouns): Words | ||
{ | ||
if ($nouns < 0) { | ||
throw new \Exception('Number of nouns must be a positive integer'); | ||
} | ||
|
||
$this->nouns = $nouns; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Sets the number of adjectives | ||
* | ||
* @access public | ||
* @param int $adjectives A positive integer | ||
* @return self | ||
*/ | ||
public function adjectives(int $adjectives): Words | ||
{ | ||
if ($adjectives < 0) { | ||
throw new \Exception('Number of adjectives must be a positive integer'); | ||
} | ||
|
||
$this->adjectives = $adjectives; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Static creation. | ||
* | ||
* @access public | ||
* @static | ||
* @return self | ||
*/ | ||
public static function create() | ||
{ | ||
return new static; | ||
} | ||
|
||
/** | ||
* Generates the random words. | ||
* | ||
* @access public | ||
* @return string | ||
*/ | ||
public function generate(): string | ||
{ | ||
|
||
$wordlist = $this->readWordList(); | ||
|
||
$words = []; | ||
|
||
for ($i = 0; $i < $this->adjectives; $i++) { | ||
$words[] = $wordlist['adjectives'][random_int(0, count($wordlist['adjectives']) - 1)]; | ||
} | ||
|
||
for ($i = 0; $i < $this->nouns; $i++) { | ||
$words[] = $wordlist['nouns'][random_int(0, count($wordlist['nouns']) - 1)]; | ||
} | ||
|
||
return implode($this->spacing, $words); | ||
} | ||
|
||
/** | ||
* Alias for self::generate() | ||
* | ||
* @access public | ||
* @return string | ||
*/ | ||
public function __toString(): string | ||
{ | ||
return $this->generate(); | ||
} | ||
|
||
private function readWordList(): array | ||
{ | ||
$wordfile = __DIR__ . '/lang/' . static::$languages[$this->language] . '.json'; | ||
|
||
if (!file_exists($wordfile)) { | ||
throw new \Exception('Language file \'' . $wordfile . '\' not found'); | ||
} | ||
|
||
return json_decode(file_get_contents($wordfile), true); | ||
} | ||
} |
Oops, something went wrong.