|
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\models; |
|
14
|
|
|
|
|
15
|
|
|
use rhosocial\base\models\queries\BaseBlameableQuery; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Trait UserUsernameTrait |
|
19
|
|
|
* |
|
20
|
|
|
* @property string|Username $username |
|
21
|
|
|
* @package rhosocial\user\models |
|
22
|
|
|
* @version 1.0 |
|
23
|
|
|
* @author vistart <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
trait UserUsernameTrait |
|
26
|
|
|
{ |
|
27
|
|
|
public $usernameClass = false; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Check whether this user enables the username feature or not. |
|
31
|
|
|
* @return boolean |
|
32
|
|
|
*/ |
|
33
|
|
|
public function hasEnabledUsername() |
|
34
|
|
|
{ |
|
35
|
|
|
if ($this->usernameClass === false || !is_string($this->usernameClass) || !class_exists($this->usernameClass)) { |
|
36
|
|
|
return false; |
|
37
|
|
|
} |
|
38
|
|
|
return true; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Get username. |
|
43
|
|
|
* This method may return null, please consider processing the abnormal conditions. |
|
44
|
|
|
* @return BaseBlameableQuery |
|
45
|
|
|
*/ |
|
46
|
|
|
public function getUsername() |
|
47
|
|
|
{ |
|
48
|
|
|
if (!$this->hasEnabledUsername()) { |
|
49
|
|
|
return null; |
|
50
|
|
|
} |
|
51
|
|
|
$usernameClass = $this->usernameClass; |
|
52
|
|
|
$noInit = $usernameClass::buildNoInitModel(); |
|
53
|
|
|
/* @var $noInit Username */ |
|
54
|
|
|
return $this->hasOne($usernameClass, [$noInit->createdByAttribute => $this->guidAttribute]); |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Create or get username. |
|
59
|
|
|
* @param $username |
|
60
|
|
|
* @return null|Username |
|
61
|
|
|
*/ |
|
62
|
|
|
public function createUsername($username) |
|
63
|
|
|
{ |
|
64
|
|
|
if (!$this->hasEnabledUsername()) { |
|
65
|
|
|
return null; |
|
66
|
|
|
} |
|
67
|
|
|
$usernameClass = $this->usernameClass; |
|
68
|
|
|
$model = $usernameClass::findOne($this->getGUID()); |
|
|
|
|
|
|
69
|
|
|
if (!$model) { |
|
70
|
|
|
$model = $this->create($usernameClass); |
|
|
|
|
|
|
71
|
|
|
$model->setGUID($this->getGUID()); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
$model->content = $username; |
|
74
|
|
|
return $model; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Set username. |
|
79
|
|
|
* @param string|Username $username |
|
80
|
|
|
* @return bool |
|
81
|
|
|
*/ |
|
82
|
|
|
public function setUsername($username = null) |
|
83
|
|
|
{ |
|
84
|
|
|
if ($username === null && ($model = $this->getUsername()->one())) { |
|
85
|
|
|
return $model->delete() > 0; |
|
86
|
|
|
} |
|
87
|
|
|
if ($username instanceof Username) { |
|
88
|
|
|
$username = $username->content; |
|
89
|
|
|
} |
|
90
|
|
|
$model = $this->createUsername($username); |
|
91
|
|
|
return $model->save(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Remove username. |
|
96
|
|
|
* @return bool |
|
97
|
|
|
*/ |
|
98
|
|
|
public function removeUsername() |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->setUsername(null); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.