These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 | use yii\base\Event; |
||
18 | use yii\caching\TagDependency; |
||
19 | |||
20 | /** |
||
21 | * Simple Profile Model. |
||
22 | * One Profile corresponds to only one [[User]]. |
||
23 | * |
||
24 | * If you're using MySQL, we recommend that you create a data table using the following statement: |
||
25 | ```SQL |
||
26 | CREATE TABLE `profile` ( |
||
27 | `guid` varbinary(16) NOT NULL COMMENT 'User GUID', |
||
28 | `nickname` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'Nickname', |
||
29 | `first_name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT 'First Name', |
||
30 | `last_name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT 'Last Name', |
||
31 | `gravatar_type` smallint(6) NOT NULL DEFAULT '0' COMMENT 'Gravatar Type', |
||
32 | `gravatar` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT 'Gravatar', |
||
33 | `gender` tinyint(1) NOT NULL DEFAULT '1' COMMENT 'Gender', |
||
34 | `timezone` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT 'UTC' COMMENT 'Timezone', |
||
35 | `individual_sign` text COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'Individual Sign', |
||
36 | `created_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00' COMMENT 'Created At', |
||
37 | `updated_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00' COMMENT 'Updated At', |
||
38 | PRIMARY KEY (`guid`), |
||
39 | CONSTRAINT `user_profile_fk` FOREIGN KEY (`guid`) REFERENCES `user` (`guid`) ON DELETE CASCADE ON UPDATE CASCADE |
||
40 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Profile'; |
||
41 | ``` |
||
42 | * |
||
43 | * @property string $nickname Nickname |
||
44 | * @property string $first_name First Name |
||
45 | * @property string $last_name Last Name |
||
46 | * @property string $gender Gender |
||
47 | * @property string $gravatar_type Gravatar Type |
||
48 | * @property string $gravatar Gravatar |
||
49 | * @property string $timezone Timezone |
||
50 | * @property string $individual_sign Individual Signature |
||
51 | * |
||
52 | * @property-read User $user |
||
53 | * |
||
54 | * @version 1.0 |
||
55 | * @author vistart <[email protected]> |
||
56 | */ |
||
57 | class Profile extends BaseBlameableModel |
||
58 | { |
||
59 | public $createdByAttribute = 'guid'; |
||
60 | |||
61 | // The host of Profile is only permitted to modify it. |
||
62 | public $updatedByAttribute = false; |
||
63 | |||
64 | // Profile do not have its identifier. |
||
65 | public $idAttribute = false; |
||
66 | |||
67 | // Profile do not need to record IP address. |
||
68 | public $enableIP = 0; |
||
69 | |||
70 | /** |
||
71 | * @var string Specify the nickname as the content attribute. |
||
72 | */ |
||
73 | public $contentAttribute = 'nickname'; |
||
74 | |||
75 | const SCENARIO_UPDATE = 'update'; |
||
76 | |||
77 | public function attributeLabels() |
||
78 | { |
||
79 | return [ |
||
80 | $this->contentAttribute => Yii::t('user', 'Nickname'), |
||
81 | 'first_name' => Yii::t('user', 'First Name'), |
||
82 | 'last_name' => Yii::t('user', 'Last Name'), |
||
83 | 'gender' => Yii::t('user', 'Gender'), |
||
84 | 'gravatar_type' => Yii::t('user', 'Gravatar Type'), |
||
85 | 'gravatar' => Yii::t('user', 'Gravatar'), |
||
86 | 'timezone' => Yii::t('user', 'Timezone'), |
||
87 | 'individual_sign' => Yii::t('user', 'Individual Signature'), |
||
88 | $this->createdByAttribute => Yii::t('user', 'Created By'), |
||
89 | $this->createdAtAttribute => Yii::t('user', 'Creation Time'), |
||
90 | $this->updatedAtAttribute => Yii::t('user', 'Last Updated Time'), |
||
91 | ]; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Get rules associated with individual sign attribute. |
||
96 | * You can override this method if current rules do not satisfy your needs. |
||
97 | * If you do not use individual sign attribute, please override this method and return empty array. |
||
98 | * @return array Rules associated with individual sign. |
||
99 | */ |
||
100 | 16 | public function getIndividualSignRules() |
|
101 | { |
||
102 | return [ |
||
103 | 16 | ['individual_sign', 'string', 'skipOnEmpty' => true], |
|
104 | ['individual_sign', 'default', 'value' => ''], |
||
105 | ]; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Get rules associated with name attribute. |
||
110 | * You can override this method if current rules do not satisfy your needs. |
||
111 | * If you do not use name attribute, please override this method and return empty array. |
||
112 | * @return array Rules associated with name. |
||
113 | */ |
||
114 | 16 | public function getNameRules() |
|
115 | { |
||
116 | return [ |
||
117 | 16 | [['first_name', 'last_name'], 'string', 'max' => 255, 'skipOnEmpty' => true], |
|
118 | [['first_name', 'last_name'], 'default', 'value' => ''], |
||
119 | ]; |
||
120 | } |
||
121 | |||
122 | const GENDER_UNSPECIFIED = 0xffff; |
||
123 | const GENDER_MALE = 1; |
||
124 | const GENDER_FEMALE = 2; |
||
125 | |||
126 | public static $genders = [ |
||
127 | self::GENDER_MALE => 'Male', |
||
128 | self::GENDER_FEMALE => 'Female', |
||
129 | ]; |
||
130 | |||
131 | /** |
||
132 | * Get rules associated with gender attribute. |
||
133 | * You can override this method if current rules do not satisfy your needs. |
||
134 | * If you do not use gender attribute, please override this method and return empty array. |
||
135 | * @return array Rules associated with gender. |
||
136 | */ |
||
137 | 16 | public function getGenderRules() |
|
138 | { |
||
139 | return [ |
||
140 | 16 | ['gender', 'default', 'value' => self::GENDER_MALE], |
|
141 | 16 | ['gender', 'in', 'range' => array_keys(static::$genders)], |
|
142 | ]; |
||
143 | } |
||
144 | |||
145 | public static function getGenderDesc($gender = null) |
||
146 | { |
||
147 | if (array_key_exists($gender, self::$genders)) { |
||
148 | return Yii::t('user', self::$genders[$gender]); |
||
149 | } |
||
150 | return null; |
||
151 | } |
||
152 | |||
153 | public static function getGenderDescs() |
||
154 | { |
||
155 | $genders = []; |
||
156 | foreach (self::$genders as $key => $gender) { |
||
157 | $genders[$key] = static::getGenderDesc($key); |
||
158 | } |
||
159 | return $genders; |
||
160 | } |
||
161 | |||
162 | public static function getGenderDescsWithEmpty() |
||
163 | { |
||
164 | $genders[''] = Yii::t('user', 'All'); |
||
0 ignored issues
–
show
|
|||
165 | foreach (self::$genders as $key => $gender) { |
||
166 | $genders[$key] = static::getGenderDesc($key); |
||
167 | } |
||
168 | return $genders; |
||
169 | } |
||
170 | |||
171 | 16 | public function getGravatarRules() |
|
172 | { |
||
173 | return [ |
||
174 | 16 | ['gravatar_type', 'default', 'value' => 0], |
|
175 | ['gravatar_type', 'integer'], |
||
176 | ['gravatar', 'default', 'value' => ''], |
||
177 | ['gravatar', 'string', 'max' => 255], |
||
178 | ]; |
||
179 | } |
||
180 | |||
181 | 16 | public function getTimezoneRules() |
|
182 | { |
||
183 | return [ |
||
184 | 16 | ['timezone', 'in', 'range' => \DateTimeZone::listIdentifiers()], |
|
185 | 16 | ['timezone', 'default', 'value' => Yii::$app->timeZone], |
|
186 | ]; |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * @inheritdoc |
||
191 | */ |
||
192 | 16 | public function rules() |
|
193 | { |
||
194 | 16 | return array_merge($this->getNameRules(), |
|
195 | 16 | $this->getGenderRules(), |
|
196 | 16 | $this->getGravatarRules(), |
|
197 | 16 | $this->getTimezoneRules(), |
|
198 | 16 | $this->getIndividualSignRules(), |
|
199 | 16 | parent::rules()); |
|
200 | } |
||
201 | |||
202 | /** |
||
203 | * @inheritdoc |
||
204 | */ |
||
205 | 18 | public static function tableName() |
|
206 | { |
||
207 | 18 | return '{{%profile}}'; |
|
208 | } |
||
209 | |||
210 | 16 | public function scenarios() |
|
211 | { |
||
212 | 16 | return array_merge(parent::scenarios(), [ |
|
213 | 16 | self::SCENARIO_UPDATE => [$this->contentAttribute, 'first_name', 'last_name', 'gender', 'gravatar_type', 'gravatar', 'timezone', 'individual_sign'], |
|
214 | ]); |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * @inheritdoc |
||
219 | */ |
||
220 | 18 | public function init() |
|
221 | { |
||
222 | 18 | $this->on(static::EVENT_AFTER_UPDATE, [$this, 'onInvalidTags']); |
|
223 | 18 | parent::init(); |
|
224 | 18 | } |
|
225 | |||
226 | /** |
||
227 | * @var string |
||
228 | */ |
||
229 | public $cacheTagPrefix = 'tag_user_profile_'; |
||
230 | |||
231 | /** |
||
232 | * @return string |
||
233 | */ |
||
234 | public function getCacheTag() |
||
235 | { |
||
236 | return $this->cacheTagPrefix . $this->getID(); |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * @param Event $event |
||
241 | */ |
||
242 | public function onInvalidTags($event) |
||
243 | { |
||
244 | $sender = $event->sender; |
||
245 | /*@var $sender static */ |
||
246 | return TagDependency::invalidate(Yii::$app->cache, $sender->getCacheTag()); |
||
247 | } |
||
248 | } |
||
249 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.