Skip to content

Commit 965ddd7

Browse files
committed
Merge pull request #16 from nubs/alter-all-generator-interface
Update all generator API to use generator instances.
2 parents 6fec7d3 + 22d7285 commit 965ddd7

File tree

3 files changed

+34
-12
lines changed

3 files changed

+34
-12
lines changed

README.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,22 @@ use them to generate a random name using their functionality.
3232

3333
#### Usage
3434
```php
35-
$generator = new \Nubs\RandomNameGenerator\All();
35+
$generator = \Nubs\RandomNameGenerator\All::create();
3636
echo $generator->getName();
3737
```
3838

39+
Alternatively, if you want to configure/build the generators to use instead of
40+
using all of the available generators, you can construct them yourself:
41+
42+
```php
43+
$generator = new \Nubs\RandomNameGenerator\All(
44+
[
45+
new \Nubs\RandomNameGenerator\Alliteration(),
46+
new \Nubs\RandomNameGenerator\Vgng()
47+
]
48+
);
49+
```
50+
3951
### Video Game Names
4052
The video game name generator is based off of [prior](http://videogamena.me/)
4153
[art](https://github.com/nullpuppy/vgng). It will generate unique names based

src/All.php

+16-8
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,30 @@ class All implements Generator
1515
protected $_randomizer;
1616

1717
/**
18-
* Initializes the All Generator with the default generator list.
18+
* Initializes the All Generator with the list of generators to choose from.
1919
*
2020
* @api
2121
* @param array $generators The random generators to use.
2222
* @param \Cinam\Randomizer\Randomizer $randomizer The random number generator.
2323
*/
24-
public function __construct(array $generators = [], Randomizer $randomizer = null)
24+
public function __construct(array $generators, Randomizer $randomizer = null)
2525
{
26-
$this->_generators = empty($generators) ? ['\Nubs\RandomNameGenerator\Alliteration', '\Nubs\RandomNameGenerator\Vgng'] : $generators;
26+
$this->_generators = $generators;
2727
$this->_randomizer = $randomizer;
2828
}
2929

30+
/**
31+
* Constructs an All Generator using the default list of generators.
32+
*
33+
* @api
34+
* @param \Cinam\Randomizer\Randomizer $randomizer The random number generator.
35+
* @return \Nubs\RandomNameGenerator\All The constructed generator.
36+
*/
37+
public static function create(Randomizer $randomizer = null)
38+
{
39+
return new self([new Alliteration($randomizer), new Vgng($randomizer)], $randomizer);
40+
}
41+
3042
/**
3143
* Gets a randomly generated name using the configured generators.
3244
*
@@ -45,10 +57,6 @@ public function getName()
4557
*/
4658
protected function _getRandomGenerator()
4759
{
48-
$generator = $this->_randomizer ?
49-
$this->_randomizer->getArrayValue($this->_generators) :
50-
$this->_generators[array_rand($this->_generators)];
51-
52-
return new $generator($this->_randomizer);
60+
return $this->_randomizer ? $this->_randomizer->getArrayValue($this->_generators) : $this->_generators[array_rand($this->_generators)];
5361
}
5462
}

tests/AllTest.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class AllTest extends PHPUnit_Framework_TestCase
1515
*
1616
* @test
1717
* @covers ::__construct
18+
* @covers ::create
1819
* @covers ::getName
1920
* @uses \Nubs\RandomNameGenerator\Alliteration
2021
* @uses \Nubs\RandomNameGenerator\Vgng
@@ -23,7 +24,7 @@ class AllTest extends PHPUnit_Framework_TestCase
2324
*/
2425
public function getNameBasic()
2526
{
26-
$generator = new All();
27+
$generator = All::create();
2728
$name = $generator->getName();
2829
$this->assertRegexp('/.+/', $name);
2930
}
@@ -33,6 +34,7 @@ public function getNameBasic()
3334
*
3435
* @test
3536
* @covers ::__construct
37+
* @covers ::create
3638
* @covers ::getName
3739
* @uses \Nubs\RandomNameGenerator\Alliteration
3840
*
@@ -41,10 +43,10 @@ public function getNameBasic()
4143
public function getNameForced()
4244
{
4345
$numberGenerator = $this->getMock('\Cinam\Randomizer\NumberGenerator', array('getInt'));
44-
$numberGenerator->expects($this->exactly(3))->method('getInt')->will($this->onConsecutiveCalls(0, 20, 5));
46+
$numberGenerator->expects($this->exactly(2))->method('getInt')->will($this->onConsecutiveCalls(20, 5));
4547
$randomizer = new Randomizer($numberGenerator);
4648

47-
$generator = new All([], $randomizer);
49+
$generator = new All([new Alliteration($randomizer)]);
4850
$this->assertSame('Black Beetle', $generator->getName());
4951
}
5052
}

0 commit comments

Comments
 (0)