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

Commit

Permalink
Merge pull request #1087 from itigoppo/jp-name-kana-by-gender
Browse files Browse the repository at this point in the history
[ja_JP]kana of Japanese name by gender.
  • Loading branch information
fzaninotto authored Nov 9, 2016
2 parents 47104e5 + c5df1ff commit 70572ab
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 22 deletions.
10 changes: 8 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -1023,10 +1023,16 @@ echo $faker->taxId(); // "DIXDPZ44E08F367A"
<?php

// Generates a 'kana' name
echo $faker->kanaName; // "アオタ ミノル"
echo $faker->kanaName($gender = null|'male'|'female') // "アオタ ミノル"

// Generates a 'kana' first name
echo $faker->firstKanaName; // "ハルカ"
echo $faker->firstKanaName($gender = null|'male'|'female') // "ヒデキ"

// Generates a 'kana' first name on the male
echo $faker->firstKanaNameMale // "ヒデキ"

// Generates a 'kana' first name on the female
echo $faker->firstKanaNameFemale // "マアヤ"

// Generates a 'kana' last name
echo $faker->lastKanaName; // "ナカジマ"
Expand Down
73 changes: 57 additions & 16 deletions src/Faker/Provider/ja_JP/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,28 @@ class Person extends \Faker\Provider\Person
'若松', '渡辺',
);

protected static $kanaFormats = array(
'{{lastKanaName}} {{firstKanaName}}'
protected static $firstKanaNameFormat = array(
'{{firstKanaNameMale}}',
'{{firstKanaNameFemale}}',
);

protected static $firstKanaName = array(
'アキラ', 'アケミ', 'アスカ', 'アツシ', 'オサム',
'カオリ', 'カナ', 'キョウスケ', 'ケンイチ', 'クミコ', 'ケンイチ',
'サユリ', 'ジュン', 'ソウタロウ',
'タイチ', 'タロウ', 'チヨ', 'タクマ', 'ツバサ', 'トモミ', 'トモヤ',
'ナオキ', 'ナオコ', 'ナオト', 'ナナカ',
'ハナコ', 'ヒデキ', 'ハルカ', 'ヒロシ',
'マアヤ', 'マイ', 'マナブ', 'ミキ', 'ミツル', 'ミノル', 'モモコ', 'ヒロキ',
'ユイ', 'ユウタ', 'ヤスヒロ', 'ヨウイチ', 'ヨウコ', 'ヨウスケ', 'ユミコ', 'リョウスケ', 'リョウヘイ', 'レイ', 'リカ',
protected static $maleKanaNameFormats = array(
'{{lastKanaName}} {{firstKanaNameMale}}',
);

protected static $femaleKanaNameFormats = array(
'{{lastKanaName}} {{firstKanaNameFemale}}',
);

protected static $firstKanaNameMale = array(
'アキラ', 'アツシ', 'オサム', 'カズヤ', 'キョウスケ', 'ケンイチ', 'シュウヘイ', 'ショウタ', 'ジュン', 'ソウタロウ',
'タイチ', 'タロウ', 'タクマ', 'ツバサ', 'トモヤ', 'ナオキ', 'ナオト', 'ヒデキ', 'ヒロシ', 'マナブ', 'ミツル', 'ミノル',
'ユウキ', 'ユウタ', 'ヤスヒロ', 'ヨウイチ', 'ヨウスケ', 'リョウスケ', 'リョウヘイ', 'レイ',
);

protected static $firstKanaNameFemale = array(
'アケミ', 'アスカ', 'カオリ', 'カナ', 'クミコ', 'サユリ', 'サトミ', 'チヨ',
'ナオコ', 'ナナミ', 'ハナコ', 'ハルカ', 'マアヤ', 'マイ', 'ミカコ', 'ミキ', 'モモコ', 'ユイ', 'ユミコ', 'ヨウコ', 'リカ',
);

protected static $lastKanaName = array(
Expand All @@ -73,26 +82,58 @@ class Person extends \Faker\Provider\Person
);

/**
* @param string|null $gender 'male', 'female' or null for any
* @return string
* @example 'アオタ アキラ'
*/
public function kanaName()
public function kanaName($gender = null)
{
$format = static::randomElement(static::$kanaFormats);
if ($gender === static::GENDER_MALE) {
$format = static::randomElement(static::$maleKanaNameFormats);
} elseif ($gender === static::GENDER_FEMALE) {
$format = static::randomElement(static::$femaleKanaNameFormats);
} else {
$format = static::randomElement(array_merge(static::$maleKanaNameFormats, static::$femaleKanaNameFormats));
}

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

/**
* @example 'アオタ'
* @param string|null $gender 'male', 'female' or null for any
* @return string
* @example 'アキラ'
*/
public static function firstKanaName()
public function firstKanaName($gender = null)
{
return static::randomElement(static::$firstKanaName);
if ($gender === static::GENDER_MALE) {
return static::firstKanaNameMale();
} elseif ($gender === static::GENDER_FEMALE) {
return static::firstKanaNameFemale();
}

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

/**
* @example 'アキラ'
*/
public static function firstKanaNameMale()
{
return static::randomElement(static::$firstKanaNameMale);
}

/**
* @example 'アケミ'
*/
public static function firstKanaNameFemale()
{
return static::randomElement(static::$firstKanaNameFemale);
}

/**
* @example 'アオタ'
*/
public static function lastKanaName()
{
return static::randomElement(static::$lastKanaName);
Expand Down
26 changes: 22 additions & 4 deletions test/Faker/Provider/ja_JP/PersonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,40 @@

class PersonTest extends \PHPUnit_Framework_TestCase
{
public function testKanaNameReturnsAotaMinoru()
public function testKanaNameMaleReturns()
{
$faker = new Generator();
$faker->addProvider(new Person($faker));
$faker->seed(1);

$this->assertEquals('アオタ ミノル', $faker->kanaName);
$this->assertEquals('アオタ ミノル', $faker->kanaName('male'));
}

public function testFirstKanaNameReturnsHaruka()
public function testKanaNameFemaleReturns()
{
$faker = new Generator();
$faker->addProvider(new Person($faker));
$faker->seed(1);

$this->assertEquals('ハルカ', $faker->firstKanaName);
$this->assertEquals('アオタ ミキ', $faker->kanaName('female'));
}

public function testFirstKanaNameMaleReturns()
{
$faker = new Generator();
$faker->addProvider(new Person($faker));
$faker->seed(1);

$this->assertEquals('ヒデキ', $faker->firstKanaName('male'));
}

public function testFirstKanaNameFemaleReturns()
{
$faker = new Generator();
$faker->addProvider(new Person($faker));
$faker->seed(1);

$this->assertEquals('マアヤ', $faker->firstKanaName('female'));
}

public function testLastKanaNameReturnsNakajima()
Expand Down

0 comments on commit 70572ab

Please sign in to comment.