1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* _ __ __ _____ _____ ___ ____ _____ |
5
|
|
|
* | | / // // ___//_ _// || __||_ _| |
6
|
|
|
* | |/ // /(__ ) / / / /| || | | | |
7
|
|
|
* |___//_//____/ /_/ /_/ |_||_| |_| |
8
|
|
|
* @link https://vistart.name/ |
9
|
|
|
* @copyright Copyright (c) 2016 vistart |
10
|
|
|
* @license https://vistart.name/license/ |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace vistart\Models\traits; |
14
|
|
|
|
15
|
|
|
use vistart\Models\models\BaseUserModel; |
16
|
|
|
use vistart\Models\queries\BaseUserQuery; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Description of MutualTrait |
20
|
|
|
* |
21
|
|
|
* @author vistart <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
trait MutualTrait |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
public $otherGuidAttribute = 'other_guid'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Get initiator. |
30
|
|
|
* @return BaseUserQuery |
31
|
|
|
*/ |
32
|
|
|
public function getInitiator() |
33
|
|
|
{ |
34
|
|
|
return $this->getUser(); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get recipient. |
39
|
|
|
* @return BaseUserQuery |
40
|
|
|
*/ |
41
|
|
|
public function getRecipient() |
42
|
|
|
{ |
43
|
|
|
if (!is_string($this->otherGuidAttribute)) { |
44
|
|
|
return null; |
45
|
|
|
} |
46
|
|
|
$userClass = $this->userClass; |
|
|
|
|
47
|
|
|
$model = $userClass::buildNoInitModel(); |
48
|
|
|
return $this->hasOne($userClass::className(), [$model->guidAttribute => $this->otherGuidAttribute]); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Set recipient. |
53
|
|
|
* @param BaseUserModel $user |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
public function setRecipient($user) |
57
|
|
|
{ |
58
|
|
|
if (!is_string($this->otherGuidAttribute)) { |
59
|
|
|
return null; |
60
|
|
|
} |
61
|
|
|
$otherGuidAttribute = $this->otherGuidAttribute; |
62
|
|
|
return $this->$otherGuidAttribute = $user->guid; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get mutual attributes rules. |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
18 |
|
public function getMutualRules() |
70
|
|
|
{ |
71
|
18 |
|
$rules = []; |
72
|
18 |
|
if (is_string($this->otherGuidAttribute)) { |
73
|
|
|
$rules = [ |
74
|
18 |
|
[$this->otherGuidAttribute, 'required'], |
75
|
18 |
|
[$this->otherGuidAttribute, 'string', 'max' => 36], |
76
|
18 |
|
]; |
77
|
18 |
|
} |
78
|
18 |
|
return $rules; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
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.