1
0
mirror of https://github.com/fzaninotto/Faker.git synced 2025-03-22 08:19:52 +01:00

Adding coding standards changes

This commit is contained in:
Robert Reinhard 2013-08-23 14:38:17 -07:00
parent 059ff45e36
commit 5a53cc02f4
3 changed files with 13 additions and 6 deletions

View File

@ -197,7 +197,7 @@ Each of the generator properties (like `name`, `address`, and `lorem`) are calle
## Optional data
All formatters can be made optional by chaining `optional`. When optional, the formatter will randomly return `NULL`, which can be useful for seeding non-required fields. For example:
All formatters can be made optional by chaining `optional`. When optional, the formatter will randomly return `NULL`, which can be useful for seeding non-required fields. For example:
$faker->optional->country

View File

@ -191,11 +191,16 @@ class Base
/**
* Chainable method for making any formatter optional
* @param float $weight Set the percentage that the formatter is empty. "0" would always return null,
* @param float $weight Set the percentage that the formatter is empty. "0" would always return null,
* "1" would always return the formatter
* @return null or whatever the formatter would use
*/
public function optional($weight = 0.5) {
return mt_rand() / mt_getrandmax() <= $weight ? $this->generator : new \Faker\NullGenerator;
public function optional($weight = 0.5)
{
// Return the formatter
if (mt_rand() / mt_getrandmax() <= $weight) return $this->generator;
// Return NULL
else return new \Faker\NullGenerator();
}
}

View File

@ -131,13 +131,15 @@ class BaseTest extends \PHPUnit_Framework_TestCase
$this->assertRegExp('/foo[a-z]Ba\dr/', BaseProvider::bothify('foo?Ba#r'));
}
public function testOptionalChainingOfProperty() {
public function testOptionalChainingOfProperty()
{
$faker = \Faker\Factory::create();
$this->assertNotNull($faker->optional(1)->randomNumber);
$this->assertNull($faker->optional(0)->randomNumber);
}
public function testOptionalChainingOfMethod() {
public function testOptionalChainingOfMethod()
{
$faker = \Faker\Factory::create();
$this->assertNotNull($faker->optional(1)->randomNumber(4));
$this->assertNull($faker->optional(0)->randomNumber(4));