Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace get/setSex with get/setGender -> master #105

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
72 changes: 57 additions & 15 deletions src/Common/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,20 @@

class User extends \stdClass
{
const SEX_MALE = 'male';
const SEX_FEMALE = 'female';
/**
* @deprecated Use GENDER_MALE instead!
*/
const SEX_MALE = 'male_deprecated';

/**
* @deprecated Use GENDER_FEMALE instead!
*/
const SEX_FEMALE = 'female_deprecated';

const GENDER_MALE = 'male';
const GENDER_FEMALE = 'female';
const GENDER_OTHER = 'other';
const GENDER_UNKNOWN = 'unknown';

/**
* @var string
Expand Down Expand Up @@ -47,11 +59,11 @@ class User extends \stdClass
public $username;

/**
* Should be female or male
* One of the GENDER_-constants
*
* @var string|null
* @var string
*/
protected $sex;
protected $gender = self::GENDER_UNKNOWN;

/**
* @var string|null
Expand Down Expand Up @@ -80,22 +92,52 @@ public function setBirthday(?\DateTime $birthday): void
}

/**
* @return string|null
* @return string
*/
public function getSex(): ?string
public function getGender(): string
{
return $this->sex;
return $this->gender;
}

/**
* @param string $sex
*/
public function setSex(string $sex): void
public function setGender(string $gender): void
{
if ($sex !== self::SEX_MALE && $sex !== self::SEX_FEMALE) {
throw new \InvalidArgumentException('Argument $sex is not valid');
if ($gender === self::SEX_MALE) {
trigger_error('the constant SEX_MALE is deprecated. use GENDER_MALE instead', E_USER_DEPRECATED);
$gender = self::GENDER_MALE;
}

if ($gender === self::SEX_FEMALE) {
trigger_error('the constant SEX_FEMALE is deprecated. use GENDER_FEMALE instead', E_USER_DEPRECATED);
$gender = self::GENDER_FEMALE;
}

$genders = [
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of this you can also declare a constant of valid values: const VALID_GENDERS = [self::GENDER_MALE, self::GENDER_FEMALE ....]

self::GENDER_OTHER,
self::GENDER_MALE,
self::GENDER_FEMALE,
];
if (! in_array($gender, $genders)) {
throw new \InvalidArgumentException('Argument $gender is not valid');
}

$this->sex = $sex;
$this->gender = $gender;
}

/**
* @deprecated use `getGender` instead
*/
public function getSex() : string
{
trigger_error('getSex is deprecated. Use getGender instead', E_USER_DEPRECATED);
return $this->getGender();
}

/**
* @deprecated Use setGender instead
*/
public function setSex(string $sex) : void
{
trigger_error('setSex is deprecated. Use setGender instead', E_USER_DEPRECATED);
$this->setGender($sex);
}
}
13 changes: 12 additions & 1 deletion src/OAuth2/Provider/Facebook.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,18 @@ public function getIdentity(AccessTokenInterface $accessToken)
'last_name' => 'lastname',
'email' => 'email',
'gender' => static function ($value, User $user) {
$user->setSex($value === 1 ? User::SEX_FEMALE : User::SEX_MALE);
switch ($value) {
case 'male':
$gender = User::GENDER_MALE;
break;
case 'female':
$gender = User::GENDER_FEMALE;
break;
default:
$gender = User::GENDER_OTHER;
break;
}
$user->setGender($gender);
},
'link' => 'url',
'locale' => 'locale',
Expand Down
2 changes: 1 addition & 1 deletion src/OAuth2/Provider/Google.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function getIdentity(AccessTokenInterface $accessToken)
'verified_email' => 'emailVerified',
'name' => 'fullname',
'gender' => static function ($value, User $user) {
$user->setSex($value);
$user->setGender($value);
},
'picture' => 'pictureURL'
]);
Expand Down
2 changes: 1 addition & 1 deletion src/OAuth2/Provider/MailRu.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function getIdentity(AccessTokenInterface $accessToken)
'nick' => 'username',
'pic_big' => 'pictureURL',
'sex' => static function ($value, User $user) {
$user->setSex($value === 1 ? User::SEX_FEMALE : User::SEX_MALE);
$user->setGender($value === 1 ? User::GENDER_FEMALE : User::GENDER_MALE);
}
]);

Expand Down
2 changes: 1 addition & 1 deletion src/OAuth2/Provider/Meetup.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function getIdentity(AccessTokenInterface $accessToken)
'username' => 'name',
'fullname' => 'name',
'sex' => static function ($value, User $user) {
$user->setSex($value);
$user->setGender($value);
},
'photo.photo_link' => 'pictureURL'
]);
Expand Down
2 changes: 1 addition & 1 deletion src/OAuth2/Provider/Vk.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function getIdentity(AccessTokenInterface $accessToken)
);
},
'sex' => static function ($value, User $user) {
$user->setSex($value === 1 ? User::SEX_FEMALE : User::SEX_MALE);
$user->setGender($value === 1 ? User::GENDER_FEMALE : User::GENDER_MALE);
},
'screen_name' => 'username',
'photo_max_orig' => 'pictureURL',
Expand Down
2 changes: 1 addition & 1 deletion src/OpenIDConnect/Provider/Google.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function getIdentity(AccessTokenInterface $accessToken)
'verified_email' => 'emailVerified',
'name' => 'fullname',
'gender' => static function ($value, User $user) {
$user->setSex($value);
$user->setGender($value);
},
]);

Expand Down
4 changes: 2 additions & 2 deletions tests/Test/Common/ArrayHydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function testHydrationSuccess()
$hydrator = new ArrayHydrator([
'firstname' => 'firstname',
'sex' => static function ($value, User $user) {
$user->setSex($value);
$user->setGender($value);
},
'image.picture' => 'pictureURL',
]);
Expand All @@ -32,6 +32,6 @@ public function testHydrationSuccess()

parent::assertEquals($expectedFirstName, $user->firstname);
parent::assertEquals($expectedPictureUrl, $user->pictureURL);
parent::assertEquals($expectedSex, $user->getSex());
parent::assertEquals($expectedSex, $user->getGender());
}
}
2 changes: 1 addition & 1 deletion tests/Test/OAuth2/Provider/VkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function testGetIdentitySuccess()
parent::assertSame($expectedId, $result->id);
parent::assertSame($expectedFirstname, $result->firstname);
parent::assertSame($expectedLastname, $result->lastname);
parent::assertSame('female', $result->getSex());
parent::assertSame('female', $result->getGender());
}

/**
Expand Down