From 8f437755160aa29990b593bfe7322a8b3815c4c8 Mon Sep 17 00:00:00 2001 From: Andreas Heigl Date: Sun, 15 Sep 2019 18:48:44 +0200 Subject: [PATCH] Replace get/setSex with get/setGender As explained in #94 it's a good idea to use gender instead of sex. To not break BC the get/setSex are still available but are marked as deprecated. The current functionality works as before. In addition to that it is possible to set `other` as gender and the default now is to return `unknown` instead of NULL. --- src/Common/Entity/User.php | 47 ++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/src/Common/Entity/User.php b/src/Common/Entity/User.php index fdbd38d35..7f2aa5633 100755 --- a/src/Common/Entity/User.php +++ b/src/Common/Entity/User.php @@ -8,8 +8,10 @@ class User extends \stdClass { - const SEX_MALE = 'male'; - const SEX_FEMALE = 'female'; + const GENDER_MALE = 'male'; + const GENDER_FEMALE = 'female'; + const GENDER_OTHER = 'other'; + const GENDER_UNKNOWN = 'unknown'; /** * @var string @@ -47,11 +49,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 = GENDER_UNKNOWN; /** * @var string|null @@ -80,22 +82,43 @@ 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'); + $genders = [ + GENDER_OTHER, + GENDER_MALE, + GENDER_FEMALE, + ]; + if (! $gender in_array($genders)) { + throw new \InvalidArgumentException('Argument $gender is not valid'); } - $this->sex = $sex; + $this->gender = $gender; + } + + /** + * @deprecated use `getGender` instead + */ + public function getSex() : string + { + return $this->getGender(); + } + + /** + * @deprecated Use setGender instead + */ + public function setSex(string $sex) : void + { + $this->setGender($sex); } }