'未知', self::MALE => '男', self::FEMALE => '女', }; } /** * 获取性别的整数值 * * @return int 性别值 */ public function value(): int { return $this->value; } /** * 检查当前性别是否与指定性别相同 * * @param self $gender 要比较的性别 * @return bool 如果性别相同返回 true,否则返回 false */ public function is(self $gender): bool { return $this === $gender; } /** * 根据整数值创建对应的性别枚举实例 * * @param int $value 性别值 * @return self|null 返回对应的性别枚举实例,如果值无效则返回 null */ public static function fromValue(int $value): ?self { return match ($value) { self::UNKNOWN->value => self::UNKNOWN, self::MALE->value => self::MALE, self::FEMALE->value => self::FEMALE, default => null }; } /** * 获取所有性别的值数组 * * @return array 包含所有性别值的数组 */ public static function values(): array { return array_column(self::cases(), 'value'); } /** * 获取所有性别的键值对数组 * * @return array 性别值作为键,显示文本作为值的关联数组 */ public static function all(): array { return [ self::UNKNOWN->value => self::UNKNOWN->label(), self::MALE->value => self::MALE->label(), self::FEMALE->value => self::FEMALE->label(), ]; } }