diff --git a/readme.md b/readme.md index dd6c1aaa..1377f588 100644 --- a/readme.md +++ b/readme.md @@ -245,7 +245,7 @@ $faker->addProvider(new Faker\Provider\Lorem($faker)); $faker->addProvider(new Faker\Provider\Internet($faker)); ```` -Whenever you try to access a property on the `$faker` object, the generator looks for a method with the same name in all the providers attached to it. For instance, calling `$faker->name` triggers a call to `Faker\Provider\Name::name()`. +Whenever you try to access a property on the `$faker` object, the generator looks for a method with the same name in all the providers attached to it. For instance, calling `$faker->name` triggers a call to `Faker\Provider\Name::name()`. And since Faker starts with the last provider, you can easily override existing formatters: just add a provider containing methods named after the formatters you want to override. That means that you can esily add your own providers to a `Faker\Generator`. Just have a look at the existing providers to see how you can design powerful data generators in no time. diff --git a/src/Faker/Generator.php b/src/Faker/Generator.php index 92a1eb07..b0b306fc 100644 --- a/src/Faker/Generator.php +++ b/src/Faker/Generator.php @@ -7,9 +7,9 @@ class Generator protected $providers = array(); protected $formatters = array(); - public function addProvider($providers) + public function addProvider($provider) { - $this->providers[]= $providers; + array_unshift($this->providers, $provider); } public function getProviders() diff --git a/test/Faker/GeneratorTest.php b/test/Faker/GeneratorTest.php index ce95b208..9533bb2a 100644 --- a/test/Faker/GeneratorTest.php +++ b/test/Faker/GeneratorTest.php @@ -8,6 +8,14 @@ use Faker\Generator; class GeneratorTest extends \PHPUnit_Framework_TestCase { + public function testAddProviderGivesPriorityToNewlyAddedProvider() + { + $generator = new Generator; + $generator->addProvider(new FooProvider()); + $generator->addProvider(new BarProvider()); + $this->assertEquals('barfoo', $generator->format('fooFormatter')); + } + public function testGetFormatterReturnsCallable() { $generator = new Generator; @@ -113,4 +121,12 @@ class FooProvider { return 'baz' . $value; } +} + +class BarProvider +{ + public function fooFormatter() + { + return 'barfoo'; + } } \ No newline at end of file