Complex classes like BlameableTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BlameableTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
49 | trait BlameableTrait |
||
50 | { |
||
51 | use ConfirmationTrait, |
||
52 | SelfBlameableTrait; |
||
53 | |||
54 | private $blameableLocalRules = []; |
||
55 | private $blameableLocalBehaviors = []; |
||
56 | |||
57 | /** |
||
58 | * @var boolean|string|array Specify the attribute(s) name of content(s). If |
||
59 | * there is only one content attribute, you can assign its name. Or there |
||
60 | * is multiple attributes associated with contents, you can assign their |
||
61 | * names in array. If you don't want to use this feature, please assign |
||
62 | * false. |
||
63 | * For example: |
||
64 | * ```php |
||
65 | * public $contentAttribute = 'comment'; // only one field named as 'comment'. |
||
66 | * ``` |
||
67 | * or |
||
68 | * ```php |
||
69 | * public $contentAttribute = ['year', 'month', 'day']; // multiple fields. |
||
70 | * ``` |
||
71 | * or |
||
72 | * ```php |
||
73 | * public $contentAttribute = false; // no need of this feature. |
||
74 | * ``` |
||
75 | * If you don't need this feature, you should add rules corresponding with |
||
76 | * `content` in `rules()` method of your user model by yourself. |
||
77 | */ |
||
78 | public $contentAttribute = 'content'; |
||
79 | |||
80 | /** |
||
81 | * @var array built-in validator name or validatation method name and |
||
82 | * additional parameters. |
||
83 | */ |
||
84 | public $contentAttributeRule = ['string', 'max' => 255]; |
||
85 | |||
86 | /** |
||
87 | * @var boolean|string Specify the field which stores the type of content. |
||
88 | */ |
||
89 | public $contentTypeAttribute = false; |
||
90 | |||
91 | /** |
||
92 | * @var boolean|array Specify the logic type of content, not data type. If |
||
93 | * your content doesn't need this feature. please specify false. If the |
||
94 | * $contentAttribute is specified to false, this attribute will be skipped. |
||
95 | * ```php |
||
96 | * public $contentTypes = [ |
||
97 | * 'public', |
||
98 | * 'private', |
||
99 | * 'friend', |
||
100 | * ]; |
||
101 | * ``` |
||
102 | */ |
||
103 | public $contentTypes = false; |
||
104 | |||
105 | /** |
||
106 | * @var boolean|string This attribute speicfy the name of description |
||
107 | * attribute. If this attribute is assigned to false, this feature will be |
||
108 | * skipped. |
||
109 | */ |
||
110 | public $descriptionAttribute = false; |
||
111 | |||
112 | /** |
||
113 | * @var string |
||
114 | */ |
||
115 | public $initDescription = ''; |
||
116 | |||
117 | /** |
||
118 | * @var string the attribute that will receive current user ID value. This |
||
119 | * attribute must be assigned. |
||
120 | */ |
||
121 | public $createdByAttribute = "user_guid"; |
||
122 | |||
123 | /** |
||
124 | * @var string the attribute that will receive current user ID value. |
||
125 | * Set this property to false if you do not want to record the updater ID. |
||
126 | */ |
||
127 | public $updatedByAttribute = "user_guid"; |
||
128 | |||
129 | /** |
||
130 | * @var boolean Add combinated unique rule if assigned to true. |
||
131 | */ |
||
132 | public $idCreatorCombinatedUnique = true; |
||
133 | |||
134 | /** |
||
135 | * @var boolean|string The name of user class which own the current entity. |
||
136 | * If this attribute is assigned to false, this feature will be skipped, and |
||
137 | * when you use create() method of UserTrait, it will be assigned with |
||
138 | * current user class. |
||
139 | */ |
||
140 | //public $userClass; |
||
141 | |||
142 | /** |
||
143 | * @var boolean|string Host class. |
||
144 | */ |
||
145 | public $hostClass; |
||
146 | public static $cacheKeyBlameableRules = 'blameable_rules'; |
||
147 | public static $cacheTagBlameableRules = 'tag_blameable_rules'; |
||
148 | public static $cacheKeyBlameableBehaviors = 'blameable_behaviors'; |
||
149 | public static $cacheTagBlameableBehaviors = 'tag_blameable_behaviors'; |
||
150 | |||
151 | /** |
||
152 | * @inheritdoc |
||
153 | * ------------ |
||
154 | * The classical rules is like following: |
||
155 | * [ |
||
156 | * ['guid', 'required'], |
||
157 | * ['guid', 'unique'], |
||
158 | * ['guid', 'string', 'max' => 36], |
||
159 | * |
||
160 | * ['id', 'required'], |
||
161 | * ['id', 'unique'], |
||
162 | * ['id', 'string', 'max' => 4], |
||
163 | * |
||
164 | * ['created_at', 'safe'], |
||
165 | * ['updated_at', 'safe'], |
||
166 | * |
||
167 | * ['ip_type', 'in', 'range' => [4, 6]], |
||
168 | * ['ip', 'number', 'integerOnly' => true, 'min' => 0], |
||
169 | * ] |
||
170 | * @return array |
||
171 | */ |
||
172 | 197 | public function rules() |
|
173 | { |
||
174 | 197 | return $this->getBlameableRules(); |
|
175 | } |
||
176 | |||
177 | /** |
||
178 | * @inheritdoc |
||
179 | */ |
||
180 | 204 | public function behaviors() |
|
181 | { |
||
182 | 204 | return $this->getBlameableBehaviors(); |
|
183 | } |
||
184 | |||
185 | /** |
||
186 | * Get total of contents which owned by their owner. |
||
187 | * @return integer |
||
188 | */ |
||
189 | 1 | public function countOfOwner() |
|
190 | { |
||
191 | 1 | $createdByAttribute = $this->createdByAttribute; |
|
192 | 1 | return static::find()->where([$createdByAttribute => $this->$createdByAttribute])->count(); |
|
193 | } |
||
194 | |||
195 | /** |
||
196 | * Get content. |
||
197 | * @return mixed |
||
198 | */ |
||
199 | 6 | public function getContent() |
|
200 | { |
||
201 | 6 | $contentAttribute = $this->contentAttribute; |
|
202 | 6 | if ($contentAttribute === false) { |
|
203 | return null; |
||
204 | } |
||
205 | 6 | if (is_array($contentAttribute)) { |
|
206 | $content = []; |
||
207 | foreach ($contentAttribute as $key => $value) { |
||
208 | $content[$key] = $this->$value; |
||
209 | } |
||
210 | return $content; |
||
211 | } |
||
212 | 6 | return $this->$contentAttribute; |
|
213 | } |
||
214 | |||
215 | /** |
||
216 | * Set content. |
||
217 | * @param mixed $content |
||
218 | */ |
||
219 | 63 | public function setContent($content) |
|
220 | { |
||
221 | 63 | $contentAttribute = $this->contentAttribute; |
|
222 | 63 | if ($contentAttribute === false) { |
|
223 | return; |
||
224 | } |
||
225 | 63 | if (is_array($contentAttribute)) { |
|
226 | foreach ($contentAttribute as $key => $value) { |
||
227 | $this->$value = $content[$key]; |
||
228 | } |
||
229 | return; |
||
230 | } |
||
231 | 63 | $this->$contentAttribute = $content; |
|
232 | 63 | } |
|
233 | |||
234 | /** |
||
235 | * Determines whether content could be edited. Your should implement this |
||
236 | * method by yourself. |
||
237 | * @return boolean |
||
238 | * @throws NotSupportedException |
||
239 | */ |
||
240 | public function getContentCanBeEdited() |
||
241 | { |
||
242 | if ($this->contentAttribute === false) { |
||
243 | return false; |
||
244 | } |
||
245 | throw new NotSupportedException("This method is not implemented."); |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Get blameable rules cache key. |
||
250 | * @return string cache key. |
||
251 | */ |
||
252 | 197 | public function getBlameableRulesCacheKey() |
|
253 | { |
||
254 | 197 | return static::class . $this->cachePrefix . static::$cacheKeyBlameableRules; |
|
|
|||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Get blameable rules cache tag. |
||
259 | * @return string cache tag |
||
260 | */ |
||
261 | 197 | public function getBlameableRulesCacheTag() |
|
262 | { |
||
263 | 197 | return static::class . $this->cachePrefix . static::$cacheTagBlameableRules; |
|
264 | } |
||
265 | |||
266 | /** |
||
267 | * Get the rules associated with content to be blamed. |
||
268 | * @return array rules. |
||
269 | */ |
||
270 | 197 | public function getBlameableRules() |
|
271 | { |
||
272 | 197 | $cache = $this->getCache(); |
|
273 | 197 | if ($cache) { |
|
274 | 197 | $this->blameableLocalRules = $cache->get($this->getBlameableRulesCacheKey()); |
|
275 | } |
||
276 | // 若当前规则不为空,且是数组,则认为是规则数组,直接返回。 |
||
277 | 197 | if (!empty($this->blameableLocalRules) && is_array($this->blameableLocalRules)) { |
|
278 | 79 | return $this->blameableLocalRules; |
|
279 | } |
||
280 | |||
281 | // 父类规则与确认规则合并。 |
||
282 | 197 | if ($cache) { |
|
283 | 197 | TagDependency::invalidate($cache, [$this->getEntityRulesCacheTag()]); |
|
284 | } |
||
285 | 197 | $rules = array_merge( |
|
286 | 197 | parent::rules(), |
|
287 | 197 | $this->getConfirmationRules(), |
|
288 | 197 | $this->getBlameableAttributeRules(), |
|
289 | 197 | $this->getDescriptionRules(), |
|
290 | 197 | $this->getContentRules(), |
|
291 | 197 | $this->getSelfBlameableRules() |
|
292 | ); |
||
293 | 197 | $this->setBlameableRules($rules); |
|
294 | 197 | return $this->blameableLocalRules; |
|
295 | } |
||
296 | |||
297 | /** |
||
298 | * Get the rules associated with `createdByAttribute`, `updatedByAttribute` |
||
299 | * and `idAttribute`-`createdByAttribute` combination unique. |
||
300 | * @return array rules. |
||
301 | */ |
||
302 | 197 | public function getBlameableAttributeRules() |
|
303 | { |
||
304 | 197 | $rules = []; |
|
305 | // 创建者和上次修改者由 BlameableBehavior 负责,因此标记为安全。 |
||
306 | 197 | if (!is_string($this->createdByAttribute) || empty($this->createdByAttribute)) { |
|
307 | throw new NotSupportedException('You must assign the creator.'); |
||
308 | } |
||
309 | 197 | $rules[] = [ |
|
310 | 197 | [$this->createdByAttribute], |
|
311 | 197 | 'safe', |
|
312 | ]; |
||
313 | |||
314 | 197 | if (is_string($this->updatedByAttribute) && !empty($this->updatedByAttribute)) { |
|
315 | 111 | $rules[] = [ |
|
316 | 111 | [$this->updatedByAttribute], |
|
317 | 111 | 'safe', |
|
318 | ]; |
||
319 | } |
||
320 | |||
321 | 197 | if ($this->idCreatorCombinatedUnique && is_string($this->idAttribute)) { |
|
322 | 195 | $rules ['id'] = [ |
|
323 | 195 | [$this->idAttribute, |
|
324 | 195 | $this->createdByAttribute], |
|
325 | 195 | 'unique', |
|
326 | 195 | 'targetAttribute' => [$this->idAttribute, |
|
327 | 195 | $this->createdByAttribute], |
|
328 | ]; |
||
329 | } |
||
330 | 197 | return $rules; |
|
331 | } |
||
332 | |||
333 | 197 | public function getIdRules() |
|
334 | { |
||
335 | 197 | if ($this->idCreatorCombinatedUnique && $this->idAttributeType !== static::$idTypeAutoIncrement) { |
|
336 | return [ |
||
337 | 138 | [[$this->idAttribute], 'required'], |
|
338 | ]; |
||
339 | } |
||
340 | 105 | return parent::getIdRules(); |
|
341 | } |
||
342 | |||
343 | /** |
||
344 | * Get the rules associated with `description` attribute. |
||
345 | * @return array rules. |
||
346 | */ |
||
347 | 197 | public function getDescriptionRules() |
|
348 | { |
||
349 | 197 | $rules = []; |
|
350 | 197 | if (is_string($this->descriptionAttribute) && !empty($this->descriptionAttribute)) { |
|
351 | 65 | $rules[] = [ |
|
352 | 65 | [$this->descriptionAttribute], |
|
353 | 65 | 'string' |
|
354 | ]; |
||
355 | 65 | $rules[] = [ |
|
356 | 65 | [$this->descriptionAttribute], |
|
357 | 65 | 'default', |
|
358 | 65 | 'value' => $this->initDescription, |
|
359 | ]; |
||
360 | } |
||
361 | 197 | return $rules; |
|
362 | } |
||
363 | |||
364 | /** |
||
365 | * Get the rules associated with `content` and `contentType` attributes. |
||
366 | * @return array rules. |
||
367 | */ |
||
368 | 197 | public function getContentRules() |
|
369 | { |
||
370 | 197 | if (!$this->contentAttribute) { |
|
371 | 46 | return []; |
|
372 | } |
||
373 | 161 | $rules = []; |
|
374 | 161 | $rules[] = [$this->contentAttribute, 'required']; |
|
375 | 161 | if ($this->contentAttributeRule) { |
|
376 | 161 | if (is_string($this->contentAttributeRule)) { |
|
377 | $this->contentAttributeRule = [$this->contentAttributeRule]; |
||
378 | } |
||
379 | 161 | if (is_array($this->contentAttributeRule)) { |
|
380 | 161 | $rules[] = array_merge([$this->contentAttribute], $this->contentAttributeRule); |
|
381 | } |
||
382 | } |
||
383 | |||
384 | 161 | if (!$this->contentTypeAttribute) { |
|
385 | 142 | return $rules; |
|
386 | } |
||
387 | |||
388 | 19 | if (is_array($this->contentTypes) && !empty($this->contentTypes)) { |
|
389 | 19 | $rules[] = [[ |
|
390 | 19 | $this->contentTypeAttribute], |
|
391 | 19 | 'required']; |
|
392 | 19 | $rules[] = [[ |
|
393 | 19 | $this->contentTypeAttribute], |
|
394 | 19 | 'in', |
|
395 | 19 | 'range' => array_keys($this->contentTypes)]; |
|
396 | } |
||
397 | 19 | return $rules; |
|
398 | } |
||
399 | |||
400 | /** |
||
401 | * Set blameable rules. |
||
402 | * @param array $rules |
||
403 | */ |
||
404 | 197 | protected function setBlameableRules($rules = []) |
|
405 | { |
||
406 | 197 | $this->blameableLocalRules = $rules; |
|
407 | 197 | $cache = $this->getCache(); |
|
408 | 197 | if ($cache) { |
|
409 | 197 | $tagDependency = new TagDependency(['tags' => [$this->getBlameableRulesCacheTag()]]); |
|
410 | 197 | $cache->set($this->getBlameableRulesCacheKey(), $rules, 0, $tagDependency); |
|
411 | } |
||
412 | 197 | } |
|
413 | |||
414 | /** |
||
415 | * Get blameable behaviors cache key. |
||
416 | * @return string cache key. |
||
417 | */ |
||
418 | 204 | public function getBlameableBehaviorsCacheKey() |
|
422 | |||
423 | /** |
||
424 | * Get blameable behaviors cache tag. |
||
425 | * @return string cache tag. |
||
426 | */ |
||
427 | 204 | public function getBlameableBehaviorsCacheTag() |
|
431 | |||
432 | /** |
||
433 | * Get blameable behaviors. If current behaviors array is empty, the init |
||
434 | * array will be given. |
||
435 | * @return array |
||
436 | */ |
||
437 | 204 | public function getBlameableBehaviors() |
|
438 | { |
||
439 | 204 | $cache = $this->getCache(); |
|
440 | 204 | if ($cache) { |
|
441 | 204 | $this->blameableLocalBehaviors = $cache->get($this->getBlameableBehaviorsCacheKey()); |
|
442 | } |
||
443 | 204 | if (empty($this->blameableLocalBehaviors) || !is_array($this->blameableLocalBehaviors)) { |
|
444 | 204 | if ($cache) { |
|
445 | 204 | TagDependency::invalidate($cache, [$this->getEntityBehaviorsCacheTag()]); |
|
459 | |||
460 | /** |
||
461 | * Set blameable behaviors. |
||
462 | * @param array $behaviors |
||
463 | */ |
||
464 | 204 | protected function setBlameableBehaviors($behaviors = []) |
|
474 | |||
475 | /** |
||
476 | * Set description. |
||
477 | * @return string description. |
||
478 | */ |
||
479 | 1 | public function getDescription() |
|
484 | |||
485 | /** |
||
486 | * Get description. |
||
487 | * @param string $desc description. |
||
488 | * @return string|null description if enabled, or null if disabled. |
||
489 | */ |
||
490 | 1 | public function setDescription($desc) |
|
495 | |||
496 | /** |
||
497 | * Get blame who owned this blameable model. |
||
498 | * NOTICE! This method will not check whether `$hostClass` exists. You should |
||
499 | * specify it in `init()` method. |
||
500 | * @return BaseUserQuery user. |
||
501 | */ |
||
502 | 17 | public function getUser() |
|
506 | |||
507 | /** |
||
508 | * Declares a `has-one` relation. |
||
509 | * The declaration is returned in terms of a relational [[\yii\db\ActiveQuery]] instance |
||
510 | * through which the related record can be queried and retrieved back. |
||
511 | * |
||
512 | * A `has-one` relation means that there is at most one related record matching |
||
513 | * the criteria set by this relation, e.g., a customer has one country. |
||
514 | * |
||
515 | * For example, to declare the `country` relation for `Customer` class, we can write |
||
516 | * the following code in the `Customer` class: |
||
517 | * |
||
518 | * ```php |
||
519 | * public function getCountry() |
||
520 | * { |
||
521 | * return $this->hasOne(Country::className(), ['id' => 'country_id']); |
||
522 | * } |
||
523 | * ``` |
||
524 | * |
||
525 | * Note that in the above, the 'id' key in the `$link` parameter refers to an attribute name |
||
526 | * in the related class `Country`, while the 'country_id' value refers to an attribute name |
||
527 | * in the current AR class. |
||
528 | * |
||
529 | * Call methods declared in [[\yii\db\ActiveQuery]] to further customize the relation. |
||
530 | * |
||
531 | * This method is provided by [[\yii\db\BaseActiveRecord]]. |
||
532 | * @param string $class the class name of the related record |
||
533 | * @param array $link the primary-foreign key constraint. The keys of the array refer to |
||
534 | * the attributes of the record associated with the `$class` model, while the values of the |
||
535 | * array refer to the corresponding attributes in **this** AR class. |
||
536 | * @return \yii\dbActiveQueryInterface the relational query object. |
||
537 | */ |
||
538 | public abstract function hasOne($class, $link); |
||
539 | |||
540 | /** |
||
541 | * Get host of this model. |
||
542 | * @return BaseUserQuery |
||
543 | */ |
||
544 | 34 | public function getHost() |
|
550 | |||
551 | /** |
||
552 | * Set host of this model. |
||
553 | * @param string $host |
||
554 | * @return type |
||
555 | */ |
||
556 | 146 | public function setHost($host) |
|
557 | { |
||
558 | 146 | if ($host instanceof $this->hostClass || $host instanceof \yii\web\IdentityInterface) { |
|
559 | 107 | return $this->{$this->createdByAttribute} = $host->getGUID(); |
|
560 | } |
||
561 | 50 | if (is_string($host) && preg_match(Number::GUID_REGEX, $host)) { |
|
562 | 1 | return $this->{$this->createdByAttribute} = Number::guid_bin($host); |
|
563 | } |
||
564 | 50 | if (strlen($host) == 16) { |
|
565 | 49 | return $this->{$this->createdByAttribute} = $host; |
|
566 | } |
||
567 | 1 | return false; |
|
568 | } |
||
569 | |||
570 | /** |
||
571 | * |
||
572 | * @param BaseUserModel|string $user |
||
573 | * @return boolean |
||
574 | */ |
||
575 | 4 | public function setUser($user) |
|
579 | |||
580 | /** |
||
581 | * Get updater who updated this blameable model recently. |
||
582 | * NOTICE! This method will not check whether `$hostClass` exists. You should |
||
583 | * specify it in `init()` method. |
||
584 | * @return BaseUserQuery user. |
||
585 | */ |
||
586 | 6 | public function getUpdater() |
|
596 | |||
597 | /** |
||
598 | * |
||
599 | * @param BaseUserModel|string $user |
||
600 | * @return boolean |
||
601 | */ |
||
602 | 5 | public function setUpdater($updater) |
|
618 | |||
619 | /** |
||
620 | * This event is triggered before the model update. |
||
621 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
622 | * override or modify it directly, unless you know the consequences. |
||
623 | * @param ModelEvent $event |
||
624 | */ |
||
625 | 57 | public function onContentChanged($event) |
|
631 | |||
632 | /** |
||
633 | * Return the current user's GUID if current model doesn't specify the owner |
||
634 | * yet, or return the owner's GUID if current model has been specified. |
||
635 | * This method is ONLY used for being triggered by event. DO NOT call, |
||
636 | * override or modify it directly, unless you know the consequences. |
||
637 | * @param ModelEvent $event |
||
638 | * @return string the GUID of current user or the owner. |
||
639 | */ |
||
640 | 140 | public function onGetCurrentUserGuid($event) |
|
653 | |||
654 | /** |
||
655 | * Initialize type of content. the first of element[index is 0] of |
||
656 | * $contentTypes will be used. |
||
657 | * @param ModelEvent $event |
||
658 | */ |
||
659 | 22 | public function onInitContentType($event) |
|
673 | |||
674 | /** |
||
675 | * Initialize description property with $initDescription. |
||
676 | * @param ModelEvent $event |
||
677 | */ |
||
678 | 71 | public function onInitDescription($event) |
|
690 | |||
691 | /** |
||
692 | * Attaches an event handler to an event. |
||
693 | * |
||
694 | * The event handler must be a valid PHP callback. The following are |
||
695 | * some examples: |
||
696 | * |
||
697 | * ``` |
||
698 | * function ($event) { ... } // anonymous function |
||
699 | * [$object, 'handleClick'] // $object->handleClick() |
||
700 | * ['Page', 'handleClick'] // Page::handleClick() |
||
701 | * 'handleClick' // global function handleClick() |
||
702 | * ``` |
||
703 | * |
||
704 | * The event handler must be defined with the following signature, |
||
705 | * |
||
706 | * ``` |
||
707 | * function ($event) |
||
708 | * ``` |
||
709 | * |
||
710 | * where `$event` is an [[Event]] object which includes parameters associated with the event. |
||
711 | * |
||
712 | * This method is provided by [[\yii\base\Component]]. |
||
713 | * @param string $name the event name |
||
714 | * @param callable $handler the event handler |
||
715 | * @param mixed $data the data to be passed to the event handler when the event is triggered. |
||
716 | * When the event handler is invoked, this data can be accessed via [[Event::data]]. |
||
717 | * @param boolean $append whether to append new event handler to the end of the existing |
||
718 | * handler list. If false, the new handler will be inserted at the beginning of the existing |
||
719 | * handler list. |
||
720 | * @see off() |
||
721 | */ |
||
722 | public abstract function on($name, $handler, $data = null, $append = true); |
||
723 | |||
724 | /** |
||
725 | * Detaches an existing event handler from this component. |
||
726 | * This method is the opposite of [[on()]]. |
||
727 | * This method is provided by [[\yii\base\Component]] |
||
728 | * @param string $name event name |
||
729 | * @param callable $handler the event handler to be removed. |
||
730 | * If it is null, all handlers attached to the named event will be removed. |
||
731 | * @return boolean if a handler is found and detached |
||
732 | * @see on() |
||
733 | */ |
||
734 | public abstract function off($name, $handler = null); |
||
735 | |||
736 | /** |
||
737 | * Attach events associated with blameable model. |
||
738 | */ |
||
739 | 204 | public function initBlameableEvents() |
|
754 | |||
755 | /** |
||
756 | * @inheritdoc |
||
757 | */ |
||
758 | 85 | public function enabledFields() |
|
759 | { |
||
760 | 85 | $fields = parent::enabledFields(); |
|
761 | 85 | if (is_string($this->createdByAttribute) && !empty($this->createdByAttribute)) { |
|
762 | 85 | $fields[] = $this->createdByAttribute; |
|
763 | } |
||
764 | 85 | if (is_string($this->updatedByAttribute) && !empty($this->updatedByAttribute) && |
|
765 | 85 | $this->createdByAttribute != $this->updatedByAttribute) { |
|
766 | $fields[] = $this->updatedByAttribute; |
||
767 | } |
||
768 | 85 | if (is_string($this->contentAttribute)) { |
|
769 | 85 | $fields[] = $this->contentAttribute; |
|
770 | } |
||
771 | 85 | if (is_array($this->contentAttribute)) { |
|
772 | $fields = array_merge($fields, $this->contentAttribute); |
||
773 | } |
||
774 | 85 | if (is_string($this->descriptionAttribute)) { |
|
775 | 1 | $fields[] = $this->descriptionAttribute; |
|
776 | } |
||
777 | 85 | if (is_string($this->confirmationAttribute)) { |
|
778 | 1 | $fields[] = $this->confirmationAttribute; |
|
779 | } |
||
780 | 85 | if (is_string($this->parentAttribute)) { |
|
781 | 1 | $fields[] = $this->parentAttribute; |
|
782 | } |
||
783 | 85 | return $fields; |
|
784 | } |
||
785 | |||
786 | /** |
||
787 | * Find all follows by specified identity. If `$identity` is null, the logged-in |
||
788 | * identity will be taken. |
||
789 | * @param string|integer $pageSize If it is 'all`, then will find all follows, |
||
790 | * the `$currentPage` parameter will be skipped. If it is integer, it will be |
||
791 | * regarded as sum of models in one page. |
||
792 | * @param integer $currentPage The current page number, begun with 0. |
||
793 | * @param mixed $identity It's type depends on {$this->hostClass}. |
||
794 | * @return static[] If no follows, null will be given, or return follow array. |
||
795 | */ |
||
796 | 1 | public static function findAllByIdentityInBatch($pageSize = 'all', $currentPage = 0, $identity = null) |
|
803 | |||
804 | /** |
||
805 | * Find one follow by specified identity. If `$identity` is null, the logged-in |
||
806 | * identity will be taken. If $identity doesn't has the follower, null will |
||
807 | * be given. |
||
808 | * @param integer $id user id. |
||
809 | * @param boolean $throwException |
||
810 | * @param mixed $identity It's type depends on {$this->hostClass}. |
||
811 | * @return static |
||
812 | * @throws InvalidParamException |
||
813 | */ |
||
814 | 1 | public static function findOneById($id, $throwException = true, $identity = null) |
|
826 | |||
827 | /** |
||
828 | * Get total of follows of specified identity. |
||
829 | * @param mixed $identity It's type depends on {$this->hostClass}. |
||
830 | * @return integer total. |
||
831 | */ |
||
832 | 3 | public static function countByIdentity($identity = null) |
|
836 | |||
837 | /** |
||
838 | * Get pagination, used for building contents page by page. |
||
839 | * @param integer $limit |
||
840 | * @param mixed $identity It's type depends on {$this->hostClass}. |
||
841 | * @return Pagination |
||
842 | */ |
||
843 | 2 | public static function getPagination($limit = 10, $identity = null) |
|
852 | } |
||
853 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: