Skip to content

Commit

Permalink
Pretty much a complete rewrite, but it's only one class so I won't as…
Browse files Browse the repository at this point in the history
…sume you're very impressed
  • Loading branch information
marcusirgens committed Feb 22, 2018
1 parent 6568a40 commit 3af2714
Show file tree
Hide file tree
Showing 8 changed files with 1,125 additions and 64 deletions.
16 changes: 10 additions & 6 deletions RandomFactor.php
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();
}
}
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
"issues": "https:\/\/github.com\/marcusirgens\/RandomFactor\/issues"
},
"autoload": {
"psr-4": {
"marcuspi\\RandomFactor\\": "src/marcuspi/RandomFactor/"
},
"files": [
"RandomFactor.php"
]
Expand Down
56 changes: 0 additions & 56 deletions src/Core.php

This file was deleted.

1 change: 0 additions & 1 deletion src/lang/en.json

This file was deleted.

1 change: 0 additions & 1 deletion src/lang/no.json

This file was deleted.

160 changes: 160 additions & 0 deletions src/marcuspi/RandomFactor/Words.php
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);
}
}
Loading

0 comments on commit 3af2714

Please # to comment.