Passed
Push — master ( 1edbe1...d7fe2d )
by vistart
03:21
created

Profile::getGenderDescs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 0
cts 0
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
crap 6
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\user;
14
15
use rhosocial\base\models\models\BaseBlameableModel;
16
use Yii;
17
18
/**
19
 * Simple Profile Model.
20
 * One Profile corresponds to only one [[User]].
21
 *
22
 * If you're using MySQL, we recommend that you create a data table using the following statement:
23
```SQL
24
CREATE TABLE `profile` (
25
  `guid` varbinary(16) NOT NULL COMMENT 'User GUID',
26
  `nickname` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'Nickname',
27
  `first_name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT 'First Name',
28
  `last_name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT 'Last Name',
29
  `gravatar_type` smallint(6) NOT NULL DEFAULT '0' COMMENT 'Gravatar Type',
30
  `gravatar` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT 'Gravatar',
31
  `gender` tinyint(1) NOT NULL DEFAULT '1' COMMENT 'Gender',
32
  `timezone` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT 'UTC' COMMENT 'Timezone',
33
  `individual_sign` text COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'Individual Sign',
34
  `created_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00' COMMENT 'Created At',
35
  `updated_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00' COMMENT 'Updated At',
36
  PRIMARY KEY (`guid`),
37
  CONSTRAINT `user_profile_fk` FOREIGN KEY (`guid`) REFERENCES `user` (`guid`) ON DELETE CASCADE ON UPDATE CASCADE
38
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Profile';
39
```
40
 *
41
 * @property string $nickname Nickname
42
 * @property string $first_name First Name
43
 * @property string $last_name Last Name
44
 * @property string $gender Gender
45
 * @property string $gravatar_type Gravatar Type
46
 * @property string $gravatar Gravatar
47
 * @property string $timezone Timezone
48
 * @property string $individual_sign Individual Signature
49
 *
50
 * @property-read User $user
51
 *
52
 * @version 1.0
53
 * @author vistart <[email protected]>
54
 */
55
class Profile extends BaseBlameableModel
56
{
57
    public $createdByAttribute = 'guid';
58
59
    // The host of Profile is only permitted to modify it.
60
    public $updatedByAttribute = false;
61
62
    // Profile do not have its identifier.
63
    public $idAttribute = false;
64
65
    // Profile do not need to record IP address.
66
    public $enableIP = 0;
67
68
    /**
69
     * @var string Specify the nickname as the content attribute.
70
     */
71
    public $contentAttribute = 'nickname';
72
73
    public function attributeLabels()
74
    {
75
        return [
76
            'nickname' => Yii::t('user', 'Nickname'),
77
            'first_name' => Yii::t('user', 'First Name'),
78
            'last_name' => Yii::t('user', 'Last Name'),
79
            'gender' => Yii::t('user', 'Gender'),
80
            'gravatar_type' => Yii::t('user', 'Gravatar Type'),
81
            'gravatar' => Yii::t('user', 'Gravatar'),
82
            'timezone' => Yii::t('user', 'Timezone'),
83
            'individual_sign' => Yii::t('user', 'Individual Signature'),
84
            'created_at' => Yii::t('user', 'Creation Time'),
85
            'updated_at' => Yii::t('user', 'Last Updated Time'),
86
        ];
87
    }
88
89
    /**
90
     * Get rules associated with individual sign attribute.
91
     * You can override this method if current rules do not satisfy your needs.
92
     * If you do not use individual sign attribute, please override this method and return empty array.
93
     * @return array Rules associated with individual sign.
94
     */
95 16
    public function getIndividualSignRules()
96
    {
97
        return [
98 16
            ['individual_sign', 'string', 'skipOnEmpty' => true],
99
            ['individual_sign', 'default', 'value' => ''],
100
        ];
101
    }
102
103
    /**
104
     * Get rules associated with name attribute.
105
     * You can override this method if current rules do not satisfy your needs.
106
     * If you do not use name attribute, please override this method and return empty array.
107
     * @return array Rules associated with name.
108
     */
109 16
    public function getNameRules()
110
    {
111
        return [
112 16
            [['first_name', 'last_name'], 'string', 'max' => 255, 'skipOnEmpty' => true],
113
            [['first_name', 'last_name'], 'default', 'value' => ''],
114
        ];
115
    }
116
117
    const GENDER_UNSPECIFIED = 0;
118
    const GENDER_MALE = 1;
119
    const GENDER_FEMALE = 2;
120
121
    public static $genders = [
122
        self::GENDER_UNSPECIFIED => 'Unspecified',
123
        self::GENDER_MALE => 'Male',
124
        self::GENDER_FEMALE => 'Female',
125
    ];
126
127
    /**
128
     * Get rules associated with gender attribute.
129
     * You can override this method if current rules do not satisfy your needs.
130
     * If you do not use gender attribute, please override this method and return empty array.
131
     * @return array Rules associated with gender.
132
     */
133 16
    public function getGenderRules()
134
    {
135
        return [
136 16
            ['gender', 'default', 'value' => 0],
137 16
            ['gender', 'in', 'range' => array_keys(static::$genders)],
138
        ];
139
    }
140
141
    public static function getGenderDesc($gender = null)
142
    {
143
        if (array_key_exists($gender, self::$genders)) {
144
            return Yii::t('user', self::$genders[$gender]);
145
        }
146
        return null;
147
    }
148
149
    public static function getGenderDescs()
150
    {
151
        $genders = [];
152
        foreach (self::$genders as $key => $gender) {
153
            $genders[$key] = static::getGenderDesc($key);
154
        }
155
        return $genders;
156
    }
157
158 16
    public function getGravatarRules()
159
    {
160
        return [
161 16
            ['gravatar_type', 'default', 'value' => 0],
162
            ['gravatar_type', 'integer'],
163
            ['gravatar', 'default', 'value' => ''],
164
            ['gravatar', 'string', 'max' => 255],
165
        ];
166
    }
167
168 16
    public function getTimezoneRules()
169
    {
170
        return [
171 16
            ['timezone', 'default', 'value' => 'UTC'],
172
            ['timezone', 'string'],
173
        ];
174
    }
175
176
    /**
177
     * @inheritdoc
178
     */
179 16
    public function rules()
180
    {
181 16
        return array_merge($this->getNameRules(),
182 16
                $this->getGenderRules(),
183 16
                $this->getGravatarRules(),
184 16
                $this->getTimezoneRules(),
185 16
                $this->getIndividualSignRules(),
186 16
                parent::rules());
187
    }
188
189
    /**
190
     * @inheritdoc
191
     */
192 18
    public static function tableName()
193
    {
194 18
        return '{{%profile}}';
195
    }
196
}
197