Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

Add firstname gender method to all Person provider #114

Merged
merged 1 commit into from
Apr 14, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,15 @@ Each of the generator properties (like `name`, `address`, and `lorem`) are calle

### `Faker\Provider\en_US\Person`

prefix // 'Ms.'
suffix // 'Jr.'
name // 'Dr. Zane Stroman'
firstName // 'Maynard'
lastName // 'Zulauf'
title($gender = null|'male'|'female') // 'Ms.'
titleMale // 'Mr.'
titleFemale // 'Ms.'
suffix // 'Jr.'
name($gender = null|'male'|'female') // 'Dr. Zane Stroman'
firstName($gender = null|'male'|'female') // 'Maynard'
firstNameMale // 'Maynard'
firstNameFemale // 'Rachel'
lastName // 'Zulauf'

### `Faker\Provider\en_US\Address`

Expand Down
10 changes: 5 additions & 5 deletions src/Faker/Provider/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ public static function toUpper($string = '')
/**
* Chainable method for making any formatter optional.
*
* @param float $weight Set the probability of receiving a null value.
* "0" will always return null, "1" will always return the generator.
* @param float $weight Set the probability of receiving a null value.
* "0" will always return null, "1" will always return the generator.
* @return mixed|null
*/
public function optional($weight = 0.5)
Expand All @@ -277,9 +277,9 @@ public function optional($weight = 0.5)
* $faker->unique()->randomElement(array(1, 2, 3));
* </code>
*
* @param boolean $reset If set to true, resets the list of existing values
* @param integer $maxRetries Maximum number of retries to find a unique value,
* After which an OverflowExcption is thrown.
* @param boolean $reset If set to true, resets the list of existing values
* @param integer $maxRetries Maximum number of retries to find a unique value,
* After which an OverflowExcption is thrown.
* @throws OverflowException When no unique value can be found by iterating $maxRetries times
*
* @return UniqueGenerator A proxy class returning only non-existing values
Expand Down
16 changes: 8 additions & 8 deletions src/Faker/Provider/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static function unixTime($max = 'now')
/**
* Get a datetime object for a date between January 1, 1970 and now
*
* @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
* @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
* @example DateTime('2005-08-16 20:39:21')
* @return \DateTime
*/
Expand All @@ -46,7 +46,7 @@ public static function dateTime($max = 'now')
/**
* Get a datetime object for a date between January 1, 001 and now
*
* @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
* @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
* @example DateTime('1265-03-22 21:15:52')
* @return \DateTime
*/
Expand Down Expand Up @@ -94,8 +94,8 @@ public static function time($format = 'H:i:s', $max = 'now')
* Get a DateTime object based on a random date between two given dates.
* Accepts date strings that can be recognized by strtotime().
*
* @param string $startDate Defaults to 30 years ago
* @param string $endDate Defaults to "now"
* @param string $startDate Defaults to 30 years ago
* @param string $endDate Defaults to "now"
* @example DateTime('1999-02-02 11:42:52')
* @return \DateTime
*/
Expand All @@ -117,7 +117,7 @@ public static function dateTimeBetween($startDate = '-30 years', $endDate = 'now
}

/**
* @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
* @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
* @example DateTime('1964-04-04 11:02:02')
* @return \DateTime
*/
Expand All @@ -127,7 +127,7 @@ public static function dateTimeThisCentury($max = 'now')
}

/**
* @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
* @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
* @example DateTime('2010-03-10 05:18:58')
* @return \DateTime
*/
Expand All @@ -137,7 +137,7 @@ public static function dateTimeThisDecade($max = 'now')
}

/**
* @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
* @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
* @example DateTime('2011-09-19 09:24:37')
* @return \DateTime
*/
Expand All @@ -147,7 +147,7 @@ public static function dateTimeThisYear($max = 'now')
}

/**
* @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
* @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
* @example DateTime('2011-10-05 12:51:46')
* @return \DateTime
*/
Expand Down
12 changes: 6 additions & 6 deletions src/Faker/Provider/Lorem.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ public static function words($nb = 3, $asText = false)
* Generate a random sentence
*
* @example 'Lorem ipsum dolor sit amet.'
* @param integer $nbWords around how many words the sentence should contain
* @param boolean $variableNbWords set to false if you want exactly $nbWords returned,
* otherwise $nbWords may vary by +/-40% with a minimum of 1
* @param integer $nbWords around how many words the sentence should contain
* @param boolean $variableNbWords set to false if you want exactly $nbWords returned,
* otherwise $nbWords may vary by +/-40% with a minimum of 1
* @return string
*/
public static function sentence($nbWords = 6, $variableNbWords = true)
Expand Down Expand Up @@ -116,9 +116,9 @@ public static function sentences($nb = 3, $asText = false)
* Generate a single paragraph
*
* @example 'Sapiente sunt omnis. Ut pariatur ad autem ducimus et. Voluptas rem voluptas sint modi dolorem amet.'
* @param integer $nbSentences around how many sentences the paragraph should contain
* @param boolean $variableNbSentences set to false if you want exactly $nbSentences returned,
* otherwise $nbSentences may vary by +/-40% with a minimum of 1
* @param integer $nbSentences around how many sentences the paragraph should contain
* @param boolean $variableNbSentences set to false if you want exactly $nbSentences returned,
* otherwise $nbSentences may vary by +/-40% with a minimum of 1
* @return string
*/
public static function paragraph($nbSentences = 3, $variableNbSentences = true)
Expand Down
99 changes: 90 additions & 9 deletions src/Faker/Provider/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,118 @@

class Person extends \Faker\Provider\Base
{
protected static $formats = array(
'{{firstName}} {{lastName}}',
const GENDER_MALE = 'male';
const GENDER_FEMALE = 'female';

protected static $titleFormat = array(
'{{titleMale}}',
'{{titleFemale}}',
);

protected static $firstNameFormat = array(
'{{firstNameMale}}',
'{{firstNameFemale}}',
);

protected static $maleNameFormats = array(
'{{firstNameMale}} {{lastName}}',
);

protected static $femaleNameFormats = array(
'{{firstNameFemale}} {{lastName}}',
);

protected static $firstName = array('John', 'Jane');
protected static $firstNameMale = array(
'John',
);

protected static $firstNameFemale = array(
'Jane',
);

protected static $lastName = array('Doe');

protected static $titleMale = array('Mr.', 'Dr.', 'Prof.');

protected static $titleFemale = array('Mrs.', 'Ms.', 'Miss', 'Dr.', 'Prof.');

/**
* @param string|null $gender 'male', 'female' or null for any
* @example 'John Doe'
*/
public function name()
public function name($gender = null)
{
$format = static::randomElement(static::$formats);

if ($gender === static::GENDER_MALE) {
$format = static::randomElement(static::$maleNameFormats);
} elseif ($gender === static::GENDER_FEMALE) {
$format = static::randomElement(static::$femaleNameFormats);
} else {
$format = static::randomElement(array_merge(static::$maleNameFormats, static::$femaleNameFormats));
}

return $this->generator->parse($format);
}

/**
* @param string|null $gender 'male', 'female' or null for any
* @example 'John'
*/
public static function firstName()
public function firstName($gender = null)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not static anymore?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, sorry, saw the last line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, this method use now the variable $this so the method can't stay static anymore.

{
if ($gender === static::GENDER_MALE) {
return static::firstNameMale();
} elseif ($gender === static::GENDER_FEMALE) {
return static::firstNameFemale();
}

return $this->generator->parse(static::randomElement(static::$firstNameFormat));
}

public static function firstNameMale()
{
return static::randomElement(static::$firstNameMale);
}

public static function firstNameFemale()
{
return static::randomElement(static::$firstName);
return static::randomElement(static::$firstNameFemale);
}

/**
* @example 'Doe'
*/
public static function lastName()
public function lastName()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not static anymore?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because in some I18N Person providers like bg_BG , I need to use $this->generator->parse otherwise some tests failed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and I follow your advice to refactore using more formats

{
return static::randomElement(static::$lastName);
}

/**
* @example 'Mrs.'
*/
public function title($gender = null)
{
if ($gender === static::GENDER_MALE) {
return static::titleMale();
} elseif ($gender === static::GENDER_FEMALE) {
return static::titleFemale();
}

return $this->generator->parse(static::randomElement(static::$titleFormat));
}

/**
* @example 'Mr.'
*/
public static function titleMale()
{
return static::randomElement(static::$titleMale);
}

/**
* @example 'Mrs.'
*/
public static function titleFemale()
{
return static::randomElement(static::$titleFemale);
}
}
10 changes: 5 additions & 5 deletions src/Faker/Provider/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ abstract class Text extends \Faker\Provider\Base
* possible following words as the value.
*
* @example 'Alice, swallowing down her flamingo, and began by taking the little golden key'
* @param integer $maxNbChars Maximum number of characters the text should contain (minimum: 10)
* @param integer $indexSize Determines how many words are considered for the generation of the next word.
* The minimum is 1, and it produces the higher level of randomness, although the
* generated text usually doesn't make sense. Higher index sizes (up to 5)
* produce more correct text, at the price of less randomness.
* @param integer $maxNbChars Maximum number of characters the text should contain (minimum: 10)
* @param integer $indexSize Determines how many words are considered for the generation of the next word.
* The minimum is 1, and it produces the higher level of randomness, although the
* generated text usually doesn't make sense. Higher index sizes (up to 5)
* produce more correct text, at the price of less randomness.
* @return string
*/
public function realText($maxNbChars = 200, $indexSize = 2)
Expand Down
57 changes: 25 additions & 32 deletions src/Faker/Provider/bg_BG/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,23 @@

class Person extends \Faker\Provider\Person
{
protected static $formats = array(
protected static $lastNameFormat = array(
'{{lastNameMale}}',
'{{lastNameFemale}}',
);

protected static $maleNameFormats = array(
'{{firstNameMale}} {{lastNameMale}}',
'{{firstNameMale}} {{lastNameMale}}',
'{{firstNameMale}} {{lastNameMale}}',
'{{titleMale}} {{firstNameMale}} {{lastNameMale}}',
);

protected static $femaleNameFormats = array(
'{{firstNameFemale}} {{lastNameFemale}}',
'{{firstNameFemale}} {{lastNameFemale}}',
'{{firstNameFemale}} {{lastNameFemale}}',
'{{prefixMale}} {{firstNameMale}} {{lastNameMale}}',
'{{prefixFemale}} {{firstNameFemale}} {{lastNameFemale}}'
'{{titleFemale}} {{firstNameFemale}} {{lastNameFemale}}'
);

protected static $firstNameMale = array(
Expand Down Expand Up @@ -76,27 +84,22 @@ class Person extends \Faker\Provider\Person
'Габровлиева', 'Първанова', 'Певецова', 'Курухубева', 'Яркова', 'Плюцова', 'Балканска'
);

private static $prefixMale = array('Г-н', 'Др.');
private static $prefixFemale = array('Г-жа', 'Г-ца', 'Др.');

public static function firstName()
{
return mt_rand(1, 100) <= 50 ? static::firstNameMale() : static::firstNameFemale();
}

public static function firstNameMale()
{
return static::randomElement(static::$firstNameMale);
}

public static function firstNameFemale()
{
return static::randomElement(static::$firstNameFemale);
}
protected static $titleMale = array('Г-н', 'Др.');
protected static $titleFemale = array('Г-жа', 'Г-ца', 'Др.');

public static function lastName()
/**
* @param string|null $gender 'male', 'female' or null for any
* @example 'Чанталиев'
*/
public function lastName($gender = null)
{
return mt_rand(1, 100) <= 50 ? static::lastNameMale() : static::lastNameFemale();
if ($gender === static::GENDER_MALE) {
return static::lastNameMale();
} elseif ($gender === static::GENDER_FEMALE) {
return static::lastNameFemale();
}

return $this->generator->parse(static::randomElement(static::$lastNameFormat));
}

public static function lastNameMale()
Expand All @@ -108,14 +111,4 @@ public static function lastNameFemale()
{
return static::randomElement(static::$lastNameFemale);
}

public static function prefixMale()
{
return static::randomElement(static::$prefixMale);
}

public static function prefixFemale()
{
return static::randomElement(static::$prefixFemale);
}
}
Loading