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
|
|
|
/** |
16
|
|
|
* Description of MutualTrait |
17
|
|
|
* |
18
|
|
|
* @author vistart <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
trait MutualTrait |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
public $otherGuidAttribute = 'other_guid'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Get initiator. |
27
|
|
|
* @return \vistart\Models\queries\BaseUserQuery |
28
|
|
|
*/ |
29
|
|
|
public function getInitiator() |
30
|
|
|
{ |
31
|
|
|
return $this->getUser(); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Get recipient. |
36
|
|
|
* @return \vistart\Models\queries\BaseUserQuery |
37
|
|
|
*/ |
38
|
|
|
public function getRecipient() |
39
|
|
|
{ |
40
|
|
|
if (!is_string($this->otherGuidAttribute)) { |
41
|
|
|
return null; |
42
|
|
|
} |
43
|
|
|
$userClass = $this->userClass; |
|
|
|
|
44
|
|
|
$model = $userClass::buildNoInitModel(); |
45
|
|
|
return $this->hasOne($userClass::className(), [$model->guidAttribute => $this->otherGuidAttribute]); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function setRecipient($user) |
49
|
|
|
{ |
50
|
|
|
if (!is_string($this->otherGuidAttribute)) { |
51
|
|
|
return null; |
52
|
|
|
} |
53
|
|
|
$otherGuidAttribute = $this->otherGuidAttribute; |
54
|
|
|
return $this->$otherGuidAttribute = $user->guid; |
55
|
|
|
} |
56
|
|
|
|
57
|
8 |
|
public function getMutualRules() |
58
|
|
|
{ |
59
|
8 |
|
$rules = []; |
60
|
8 |
|
if (is_string($this->otherGuidAttribute)) { |
61
|
|
|
$rules = [ |
62
|
8 |
|
[$this->otherGuidAttribute, 'required'], |
63
|
8 |
|
[$this->otherGuidAttribute, 'string', 'max' => 36], |
64
|
8 |
|
]; |
65
|
8 |
|
} |
66
|
8 |
|
return $rules; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
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.