1 | <?php |
||
28 | trait MutualTrait |
||
29 | { |
||
30 | |||
31 | public $otherGuidAttribute = 'other_guid'; |
||
32 | |||
33 | /** |
||
34 | * Get initiator. |
||
35 | * @return BaseUserQuery |
||
36 | */ |
||
37 | 17 | public function getInitiator() |
|
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() |
|
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.