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\user\models\invitation; |
14
|
|
|
|
15
|
|
|
use rhosocial\base\models\models\BaseBlameableModel; |
16
|
|
|
use rhosocial\base\models\queries\BaseBlameableQuery; |
17
|
|
|
use rhosocial\user\User; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class Invitation |
21
|
|
|
* This class is used to record each invitation of the current user. If you want to use the invitation code, please |
22
|
|
|
* refer to the [[InvitationCode]] class. |
23
|
|
|
* You can not use this class directly, but you need to declare and use specific invitation scenes, see [[Registration]]. |
24
|
|
|
* |
25
|
|
|
* @property integer $content Invitation Type. The custom value range should be greater than or equal to 0x80. |
26
|
|
|
* Once this value is determined, no modification is recommended. |
27
|
|
|
* @property User|string $invitee Invited person. |
28
|
|
|
* @property string $invitee_guid The GUID of invited person. |
29
|
|
|
* Once this value is determined, no modification is recommended. |
30
|
|
|
* |
31
|
|
|
* @package rhosocial\user\models\invitation |
32
|
|
|
* @version 1.0 |
33
|
|
|
* @author vistart <[email protected]> |
34
|
|
|
*/ |
35
|
|
|
abstract class Invitation extends BaseBlameableModel |
36
|
|
|
{ |
37
|
|
|
public $hostClass = User::class; |
38
|
|
|
public $idAttribute = false; |
39
|
|
|
public $updatedAtAttribute = false; |
40
|
|
|
/** |
41
|
|
|
* @var array The content field in the data table should be an integer. |
42
|
|
|
*/ |
43
|
|
|
public $contentAttributeRule = ['integer']; |
44
|
|
|
/** |
45
|
|
|
* @var bool Whether to allow to send invitation to the same invitee repeatedly. |
46
|
|
|
*/ |
47
|
|
|
public $allowRepeated = true; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
|
|
public function getInviteeRules() |
53
|
|
|
{ |
54
|
|
|
$rules = [ |
55
|
|
|
['invitee_guid', 'required'], |
56
|
|
|
['invitee_guid', 'string'], |
57
|
|
|
]; |
58
|
|
|
if (!$this->allowRepeated) { |
59
|
|
|
$rules[] = [ |
60
|
|
|
[$this->createdByAttribute, $this->contentAttribute, 'invitee_guid'], 'unique', 'targetAttribute' => [ |
61
|
|
|
$this->createdByAttribute, $this->contentAttribute, 'invitee_guid', |
62
|
|
|
] |
63
|
|
|
]; |
64
|
|
|
} |
65
|
|
|
return $rules; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
|
|
public function rules() |
72
|
|
|
{ |
73
|
|
|
return array_merge(parent::rules(), $this->getInviteeRules()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param User|string $invitee |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
public function setInvitee($invitee) |
81
|
|
|
{ |
82
|
|
|
return $this->invitee_guid = (string)$invitee; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return User |
87
|
|
|
*/ |
88
|
|
|
public function getInvitee() |
89
|
|
|
{ |
90
|
|
|
$userClass = $this->hostClass; |
91
|
|
|
return $userClass::findOne($this->invitee_guid); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param User|string|array $invitee |
96
|
|
|
* @return BaseBlameableQuery |
97
|
|
|
*/ |
98
|
|
|
public static function findByInvitee($invitee) |
99
|
|
|
{ |
100
|
|
|
if (!is_array($invitee)) { |
101
|
|
|
$i[0] = $invitee; |
|
|
|
|
102
|
|
|
$invitee = $i; |
103
|
|
|
} |
104
|
|
|
foreach ($invitee as $key => $i) { |
105
|
|
|
$invitee[$key] = (string)$i; |
106
|
|
|
} |
107
|
|
|
return static::find()->andWhere(['invitee_guid' => $invitee]); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public static function tableName() |
114
|
|
|
{ |
115
|
|
|
return '{{%user_invitation}}'; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.