Completed
Push — master ( d7fe2d...0f3d1f )
by vistart
03:24
created

Profile::scenarios()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 1
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
    const SCENARIO_UPDATE = 'update';
74
75
    public function attributeLabels()
76
    {
77
        return [
78
            'nickname' => Yii::t('user', 'Nickname'),
79
            'first_name' => Yii::t('user', 'First Name'),
80
            'last_name' => Yii::t('user', 'Last Name'),
81
            'gender' => Yii::t('user', 'Gender'),
82
            'gravatar_type' => Yii::t('user', 'Gravatar Type'),
83
            'gravatar' => Yii::t('user', 'Gravatar'),
84
            'timezone' => Yii::t('user', 'Timezone'),
85
            'individual_sign' => Yii::t('user', 'Individual Signature'),
86
            'created_at' => Yii::t('user', 'Creation Time'),
87
            'updated_at' => Yii::t('user', 'Last Updated Time'),
88
        ];
89
    }
90
91
    /**
92
     * Get rules associated with individual sign attribute.
93
     * You can override this method if current rules do not satisfy your needs.
94
     * If you do not use individual sign attribute, please override this method and return empty array.
95
     * @return array Rules associated with individual sign.
96
     */
97 16
    public function getIndividualSignRules()
98
    {
99
        return [
100 16
            ['individual_sign', 'string', 'skipOnEmpty' => true],
101
            ['individual_sign', 'default', 'value' => ''],
102
        ];
103
    }
104
105
    /**
106
     * Get rules associated with name attribute.
107
     * You can override this method if current rules do not satisfy your needs.
108
     * If you do not use name attribute, please override this method and return empty array.
109
     * @return array Rules associated with name.
110
     */
111 16
    public function getNameRules()
112
    {
113
        return [
114 16
            [['first_name', 'last_name'], 'string', 'max' => 255, 'skipOnEmpty' => true],
115
            [['first_name', 'last_name'], 'default', 'value' => ''],
116
        ];
117
    }
118
119
    const GENDER_UNSPECIFIED = 0;
120
    const GENDER_MALE = 1;
121
    const GENDER_FEMALE = 2;
122
123
    public static $genders = [
124
        self::GENDER_UNSPECIFIED => 'Unspecified',
125
        self::GENDER_MALE => 'Male',
126
        self::GENDER_FEMALE => 'Female',
127
    ];
128
129
    /**
130
     * Get rules associated with gender attribute.
131
     * You can override this method if current rules do not satisfy your needs.
132
     * If you do not use gender attribute, please override this method and return empty array.
133
     * @return array Rules associated with gender.
134
     */
135 16
    public function getGenderRules()
136
    {
137
        return [
138 16
            ['gender', 'default', 'value' => 0],
139 16
            ['gender', 'in', 'range' => array_keys(static::$genders)],
140
        ];
141
    }
142
143
    public static function getGenderDesc($gender = null)
144
    {
145
        if (array_key_exists($gender, self::$genders)) {
146
            return Yii::t('user', self::$genders[$gender]);
147
        }
148
        return null;
149
    }
150
151
    public static function getGenderDescs()
152
    {
153
        $genders = [];
154
        foreach (self::$genders as $key => $gender) {
155
            $genders[$key] = static::getGenderDesc($key);
156
        }
157
        return $genders;
158
    }
159
160 16
    public function getGravatarRules()
161
    {
162
        return [
163 16
            ['gravatar_type', 'default', 'value' => 0],
164
            ['gravatar_type', 'integer'],
165
            ['gravatar', 'default', 'value' => ''],
166
            ['gravatar', 'string', 'max' => 255],
167
        ];
168
    }
169
170 16
    public function getTimezoneRules()
171
    {
172
        return [
173 16
            ['timezone', 'default', 'value' => 'UTC'],
174
            ['timezone', 'string'],
175
        ];
176
    }
177
178
    /**
179
     * @inheritdoc
180
     */
181 16
    public function rules()
182
    {
183 16
        return array_merge($this->getNameRules(),
184 16
                $this->getGenderRules(),
185 16
                $this->getGravatarRules(),
186 16
                $this->getTimezoneRules(),
187 16
                $this->getIndividualSignRules(),
188 16
                parent::rules());
189
    }
190
191
    /**
192
     * @inheritdoc
193
     */
194 18
    public static function tableName()
195
    {
196 18
        return '{{%profile}}';
197
    }
198
199 16
    public function scenarios()
200
    {
201 16
        return array_merge(parent::scenarios(), [
202 16
            self::SCENARIO_UPDATE => [$this->contentAttribute, 'first_name', 'last_name', 'gender', 'gravatar_type', 'gravatar', 'timezone', 'individual_sign'],
203
        ]);
204
    }
205
}
206