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

Add lastName gender specific on ru_RU locale #1747

Merged
merged 11 commits into from
Sep 3, 2019
22 changes: 22 additions & 0 deletions src/Faker/Provider/ru_RU/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ class Person extends \Faker\Provider\Person
'Меркушев', 'Лыткин', 'Туров',
);

protected static $lastNameSuffix = array('a', '');

/**
* Return male middle name
*
Expand Down Expand Up @@ -154,4 +156,24 @@ public function middleName($gender = null)
static::GENDER_FEMALE,
)));
}

/**
* Return last name for the specified gender.
*
* @param string|null $gender A gender of the last name should be generated
* for. If the argument is skipped a random gender will be used.
* @return string Last name
*/
public function lastName($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.

Do I understand correctly or is the generated lastName always Male by default? Shouldn't it return a random gender?

Copy link
Contributor

Choose a reason for hiding this comment

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

Good point, missed that one.

@aanfarhan

Can you adjust, please?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alright, I'll add some adjustment to the code.

{
$lastName = static::randomElement(static::$lastName);

if (static::GENDER_FEMALE === $gender) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we should add a link here for explanation?

For example, https://en.m.wikipedia.org/wiki/Eastern_Slavic_naming_customs#Grammar.

/cc @pimjansen

Copy link
Contributor

Choose a reason for hiding this comment

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

Agree, im not a fan of "only in our usecases" but it will do for no i guess

return $lastName . 'a';
} elseif (static::GENDER_MALE === $gender) {
return $lastName;
}

return $lastName . static::randomElement(static::$lastNameSuffix);
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think about

Suggested change
return $lastName . static::randomElement(static::$lastNameSuffix);
return static::lastName(static::randomElement(array(
static::GENDER_FEMALE,
static::GENDER_MALE,
));

?

Then we can also do away with the newly introduced protected property.

}
}
37 changes: 37 additions & 0 deletions test/Faker/Provider/ru_RU/PersonTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Faker\Test\Provider\ru_RU;

use Faker\Generator;
use Faker\Provider\ru_RU\Person;
use PHPUnit\Framework\TestCase;

class PersonTest extends TestCase
{
/**
* @var Generator
*/
private $faker;

public function setUp()
Copy link
Contributor

Choose a reason for hiding this comment

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

Visibility should be protected here, but we can - with the right rules - automatically fix.

{
$faker = new Generator();
$faker->addProvider(new Person($faker));
$this->faker = $faker;
}

public function testLastNameFemale()
{
$this->assertEquals("a", substr($this->faker->lastName('female'), -1));
}

public function testLastNameMale()
{
$this->assertNotEquals("a", substr($this->faker->lastName('male'), -1));
}

public function testLastNameRandom()
{
$this->assertNotNull($this->faker->lastName());
}
}