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\base\models\traits; |
14
|
|
|
|
15
|
|
|
use rhosocial\base\models\models\BaseUserModel; |
16
|
|
|
use rhosocial\base\models\queries\BaseUserQuery; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* This trait defines two roles: initiator and recipient. |
20
|
|
|
* The initiator is also the owner of this model. |
21
|
|
|
* |
22
|
|
|
* @property-read mixed $initiator |
23
|
|
|
* @property mixed $recipient |
24
|
|
|
* @property-read array $mutualRules |
25
|
|
|
* @version 1.0 |
26
|
|
|
* @author vistart <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
trait MutualTrait |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
public $otherGuidAttribute = 'other_guid'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Get initiator. |
35
|
|
|
* @return BaseUserQuery |
36
|
|
|
*/ |
37
|
17 |
|
public function getInitiator() |
38
|
|
|
{ |
39
|
17 |
|
return $this->getHost(); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get recipient. |
44
|
|
|
* @return BaseUserQuery |
45
|
|
|
*/ |
46
|
18 |
|
public function getRecipient() |
47
|
|
|
{ |
48
|
18 |
|
if (!is_string($this->otherGuidAttribute) || empty($this->otherGuidAttribute)) { |
49
|
1 |
|
throw new \yii\base\InvalidConfigException('Recipient GUID Attribute Not Specified.'); |
50
|
|
|
} |
51
|
17 |
|
$hostClass = $this->hostClass; |
|
|
|
|
52
|
17 |
|
$model = $hostClass::buildNoInitModel(); |
53
|
17 |
|
return $this->hasOne($hostClass::className(), [$model->guidAttribute => $this->otherGuidAttribute]); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Set recipient. |
58
|
|
|
* @param BaseUserModel $user |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
56 |
|
public function setRecipient($user) |
62
|
|
|
{ |
63
|
56 |
|
if (!is_string($this->otherGuidAttribute) || empty($this->otherGuidAttribute)) { |
64
|
1 |
|
throw new \yii\base\InvalidConfigException('Recipient GUID Attribute Not Specified.'); |
65
|
|
|
} |
66
|
56 |
|
if ($user instanceof BaseUserModel) { |
67
|
7 |
|
$user = $user->getGUID(); |
68
|
|
|
} |
69
|
56 |
|
$otherGuidAttribute = $this->otherGuidAttribute; |
70
|
56 |
|
return $this->$otherGuidAttribute = $user; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get mutual attributes rules. |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
53 |
|
public function getMutualRules() |
78
|
|
|
{ |
79
|
53 |
|
$rules = []; |
80
|
53 |
|
if (is_string($this->otherGuidAttribute)) { |
81
|
|
|
$rules = [ |
82
|
53 |
|
[$this->otherGuidAttribute, 'required'], |
83
|
53 |
|
[$this->otherGuidAttribute, 'string', 'max' => 16], |
84
|
|
|
]; |
85
|
|
|
} |
86
|
53 |
|
return $rules; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
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
Idable
provides a methodequalsId
that 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.