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
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* This trait is used for building query class which contains mutual relation operations. |
19
|
|
|
* |
20
|
|
|
* @version 1.0 |
21
|
|
|
* @author vistart <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
trait MutualQueryTrait |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get the opposite relation. |
28
|
|
|
* @param BaseUserModel|string $user initiator |
29
|
|
|
* @param BaseUserModel|string $other recipient. |
30
|
|
|
* @param Connection $database |
31
|
|
|
* @return |
32
|
|
|
*/ |
33
|
1 |
|
public function opposite($user, $other, $database = null) |
34
|
|
|
{ |
35
|
1 |
|
$model = $this->noInitModel; |
|
|
|
|
36
|
1 |
|
if ($user instanceof BaseUserModel) { |
37
|
1 |
|
$user = $user->getGUID(); |
38
|
|
|
} |
39
|
1 |
|
if ($other instanceof BaseUserModel) { |
40
|
1 |
|
$other = $other->getGUID(); |
41
|
|
|
} |
42
|
1 |
|
return $this->andWhere([$model->createdByAttribute => $other, $model->otherGuidAttribute => $user])->one($database); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get all the opposites. |
47
|
|
|
* @param string $user initator. |
48
|
|
|
* @param array $others all recipients. |
49
|
|
|
* @param Connection $database |
50
|
|
|
* @return array instances. |
51
|
|
|
*/ |
52
|
1 |
|
public function opposites($user, $others = [], $database = null) |
53
|
|
|
{ |
54
|
1 |
|
$model = $this->noInitModel; |
55
|
1 |
|
if ($user instanceof BaseUserModel) { |
56
|
1 |
|
$user = $user->getGUID(); |
57
|
|
|
} |
58
|
1 |
|
$query = $this->andWhere([$model->otherGuidAttribute => $user]); |
|
|
|
|
59
|
1 |
|
if (!empty($others)) { |
60
|
1 |
|
if ($others instanceof BaseUserModel) { |
61
|
1 |
|
$others = [$others->getGUID()]; |
62
|
1 |
|
} elseif (is_array($others)) { |
63
|
1 |
|
$others = BaseUserModel::compositeGUIDs($others); |
64
|
|
|
} |
65
|
1 |
|
$query = $query->andWhere([$model->createdByAttribute => array_values($others)]); |
66
|
|
|
} |
67
|
1 |
|
return $query->all($database); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Specify initiators. |
72
|
|
|
* @param string|array $users the guid of initiator if string, or guid array |
73
|
|
|
* of initiators if array. |
74
|
|
|
* @return \static $this |
75
|
|
|
*/ |
76
|
19 |
|
public function initiators($users = []) |
77
|
|
|
{ |
78
|
19 |
|
if (empty($users)) { |
79
|
1 |
|
return $this; |
80
|
|
|
} |
81
|
19 |
|
$model = $this->noInitModel; |
82
|
19 |
|
if ($users instanceof BaseUserModel) { |
83
|
19 |
|
$users = $users->getGUID(); |
84
|
1 |
|
} elseif (is_array($users)) { |
85
|
1 |
|
$users = BaseUserModel::compositeGUIDs($users); |
86
|
|
|
} |
87
|
19 |
|
return $this->andWhere([$model->createdByAttribute => $users]); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Specify recipients. |
92
|
|
|
* @param string|array $users the guid of recipient if string, or guid array |
93
|
|
|
* of recipients if array. |
94
|
|
|
* @return \static $this |
95
|
|
|
*/ |
96
|
19 |
|
public function recipients($users = []) |
97
|
|
|
{ |
98
|
19 |
|
if (empty($users)) { |
99
|
1 |
|
return $this; |
100
|
|
|
} |
101
|
19 |
|
$model = $this->noInitModel; |
102
|
19 |
|
if ($users instanceof BaseUserModel) { |
103
|
19 |
|
$users = $users->getGUID(); |
104
|
1 |
|
} elseif (is_array($users)) { |
105
|
1 |
|
$users = BaseUserModel::compositeGUIDs($users); |
106
|
|
|
} |
107
|
19 |
|
return $this->andWhere([$model->otherGuidAttribute => $users]); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: