diff --git a/src/Common/Entity/User.php b/src/Common/Entity/User.php index fdbd38d35..7f82b5fe9 100755 --- a/src/Common/Entity/User.php +++ b/src/Common/Entity/User.php @@ -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 @@ -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 @@ -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 = [ + 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); } } diff --git a/src/OAuth2/Provider/Facebook.php b/src/OAuth2/Provider/Facebook.php index 566c8d4c4..14fe5ddf0 100644 --- a/src/OAuth2/Provider/Facebook.php +++ b/src/OAuth2/Provider/Facebook.php @@ -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', diff --git a/src/OAuth2/Provider/Google.php b/src/OAuth2/Provider/Google.php index a52201338..3c8703d9f 100644 --- a/src/OAuth2/Provider/Google.php +++ b/src/OAuth2/Provider/Google.php @@ -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' ]); diff --git a/src/OAuth2/Provider/MailRu.php b/src/OAuth2/Provider/MailRu.php index c6e2ac18b..fcacd03a1 100644 --- a/src/OAuth2/Provider/MailRu.php +++ b/src/OAuth2/Provider/MailRu.php @@ -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); } ]); diff --git a/src/OAuth2/Provider/Meetup.php b/src/OAuth2/Provider/Meetup.php index e1df7a8a8..ba3b2eb55 100644 --- a/src/OAuth2/Provider/Meetup.php +++ b/src/OAuth2/Provider/Meetup.php @@ -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' ]); diff --git a/src/OAuth2/Provider/Vk.php b/src/OAuth2/Provider/Vk.php index 654616cfa..0f4d14864 100644 --- a/src/OAuth2/Provider/Vk.php +++ b/src/OAuth2/Provider/Vk.php @@ -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', diff --git a/src/OpenIDConnect/Provider/Google.php b/src/OpenIDConnect/Provider/Google.php index fb4636f71..bb8c6302c 100644 --- a/src/OpenIDConnect/Provider/Google.php +++ b/src/OpenIDConnect/Provider/Google.php @@ -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); }, ]); diff --git a/tests/Test/Common/ArrayHydratorTest.php b/tests/Test/Common/ArrayHydratorTest.php index aa0f966c5..0530b978e 100644 --- a/tests/Test/Common/ArrayHydratorTest.php +++ b/tests/Test/Common/ArrayHydratorTest.php @@ -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', ]); @@ -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()); } } diff --git a/tests/Test/OAuth2/Provider/VkTest.php b/tests/Test/OAuth2/Provider/VkTest.php index 92a255050..58eabf273 100644 --- a/tests/Test/OAuth2/Provider/VkTest.php +++ b/tests/Test/OAuth2/Provider/VkTest.php @@ -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()); } /**