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
|
|
|
* Description of MutualTrait |
20
|
|
|
* |
21
|
|
|
* @property-read mixed $initiator |
22
|
|
|
* @property mixed $recipient |
23
|
|
|
* @property-read array $mutualRules |
24
|
|
|
* @version 1.0 |
25
|
|
|
* @author vistart <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
trait MutualTrait |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
public $otherGuidAttribute = 'other_guid'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get initiator. |
34
|
|
|
* @return BaseUserQuery |
35
|
|
|
*/ |
36
|
1 |
|
public function getInitiator() |
37
|
|
|
{ |
38
|
1 |
|
return $this->getUser(); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get recipient. |
43
|
|
|
* @return BaseUserQuery |
44
|
|
|
*/ |
45
|
1 |
|
public function getRecipient() |
46
|
|
|
{ |
47
|
1 |
|
if (!is_string($this->otherGuidAttribute)) { |
48
|
|
|
return null; |
49
|
|
|
} |
50
|
1 |
|
$userClass = $this->userClass; |
|
|
|
|
51
|
1 |
|
$model = $userClass::buildNoInitModel(); |
52
|
1 |
|
return $this->hasOne($userClass::className(), [$model->guidAttribute => $this->otherGuidAttribute]); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Set recipient. |
57
|
|
|
* @param BaseUserModel $user |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
public function setRecipient($user) |
61
|
|
|
{ |
62
|
|
|
if (!is_string($this->otherGuidAttribute)) { |
63
|
|
|
return null; |
64
|
|
|
} |
65
|
|
|
$otherGuidAttribute = $this->otherGuidAttribute; |
66
|
|
|
return $this->$otherGuidAttribute = $user->getGUID(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get mutual attributes rules. |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
15 |
|
public function getMutualRules() |
74
|
|
|
{ |
75
|
15 |
|
$rules = []; |
76
|
15 |
|
if (is_string($this->otherGuidAttribute)) { |
77
|
|
|
$rules = [ |
78
|
15 |
|
[$this->otherGuidAttribute, 'required'], |
79
|
15 |
|
[$this->otherGuidAttribute, 'string', 'max' => 36], |
80
|
|
|
]; |
81
|
|
|
} |
82
|
15 |
|
return $rules; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
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.