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
|
|
|
* This trait is used for building query class which contains mutual relation operations. |
17
|
|
|
* |
18
|
|
|
* @version 2.0 |
19
|
|
|
* @author vistart <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
trait MutualQueryTrait |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Get the opposite relation. |
26
|
|
|
* @param BaseUserModel|string $user initiator |
27
|
|
|
* @param BaseUserModel|string $other recipient. |
28
|
|
|
* @param Connection $database |
29
|
|
|
* @return |
30
|
|
|
*/ |
31
|
8 |
|
public function opposite($user, $other, $database = null) |
32
|
|
|
{ |
33
|
8 |
|
$model = $this->noInitModel; |
|
|
|
|
34
|
8 |
|
return $this->andWhere([$model->createdByAttribute => $other, $model->otherGuidAttribute => $user])->one($database); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get all the opposites. |
39
|
|
|
* @param string $user initator. |
40
|
|
|
* @param array $others all recipients. |
41
|
|
|
* @param Connection $database |
42
|
|
|
* @return array instances. |
43
|
|
|
*/ |
44
|
8 |
|
public function opposites($user, $others = [], $database = null) |
45
|
|
|
{ |
46
|
8 |
|
$model = $this->noInitModel; |
47
|
8 |
|
$query = $this->andWhere([$model->otherGuidAttribute => $user]); |
|
|
|
|
48
|
8 |
|
if (!empty($others)) { |
49
|
|
|
$query = $query->andWhere([$model->createdByAttribute => array_values($others)]); |
50
|
|
|
} |
51
|
8 |
|
return $query->all($database); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Specify initiators. |
56
|
|
|
* @param string|array $users the guid of initiator if string, or guid array |
57
|
|
|
* of initiators if array. |
58
|
|
|
* @return \static $this |
59
|
|
|
*/ |
60
|
10 |
|
public function initiators($users = []) |
61
|
|
|
{ |
62
|
10 |
|
if (empty($users)) { |
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
10 |
|
$model = $this->noInitModel; |
66
|
10 |
|
return $this->andWhere([$model->createdByAttribute => $users]); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Specify recipients. |
71
|
|
|
* @param string|array $users the guid of recipient if string, or guid array |
72
|
|
|
* of recipients if array. |
73
|
|
|
* @return \static $this |
74
|
|
|
*/ |
75
|
14 |
|
public function recipients($users = []) |
76
|
|
|
{ |
77
|
14 |
|
if (empty($users)) { |
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
14 |
|
$model = $this->noInitModel; |
81
|
14 |
|
return $this->andWhere([$model->otherGuidAttribute => $users]); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
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: