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\models; |
14
|
|
|
|
15
|
|
|
use MongoDB\BSON\Binary; |
16
|
|
|
use rhosocial\base\helpers\Number; |
17
|
|
|
use rhosocial\base\models\models\BaseUserModel; |
18
|
|
|
use rhosocial\base\models\queries\BaseMongoBlameableQuery; |
19
|
|
|
use rhosocial\base\models\traits\BlameableTrait; |
20
|
|
|
use yii\web\IdentityInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Description of BaseMongoBlameableModel |
24
|
|
|
* |
25
|
|
|
* @version 1.0 |
26
|
|
|
* @author vistart <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
abstract class BaseMongoBlameableModel extends BaseMongoEntityModel |
29
|
|
|
{ |
30
|
|
|
use BlameableTrait; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Initialize the blameable model. |
34
|
|
|
* If query class is not specified, [[BaseMongoBlameableQuery]] will be taken. |
35
|
|
|
*/ |
36
|
58 |
|
public function init() |
37
|
|
|
{ |
38
|
58 |
|
if (!is_string($this->queryClass) || empty($this->queryClass)) { |
39
|
45 |
|
$this->queryClass = BaseMongoBlameableQuery::class; |
40
|
|
|
} |
41
|
58 |
|
if ($this->skipInit) { |
42
|
45 |
|
return; |
43
|
|
|
} |
44
|
58 |
|
$this->initBlameableEvents(); |
45
|
58 |
|
parent::init(); |
46
|
58 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get the query class with specified identity. |
50
|
|
|
* @param BaseUserModel $identity |
51
|
|
|
* @return BaseMongoBlameableQuery |
52
|
|
|
*/ |
53
|
4 |
|
public static function findByIdentity($identity = null) |
54
|
|
|
{ |
55
|
4 |
|
return static::find()->byIdentity($identity); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Because every document has a `MongoId" class, this class no longer needs GUID feature. |
60
|
|
|
* @var boolean determines whether enable the GUID features. |
61
|
|
|
*/ |
62
|
|
|
public $guidAttribute = false; |
63
|
|
|
public $idAttribute = '_id'; |
64
|
|
|
public $idAttributeType = 2; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @inheritdoc |
68
|
|
|
* You can override this method if enabled fields cannot meet your requirements. |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
58 |
|
public function attributes() |
72
|
|
|
{ |
73
|
58 |
|
return $this->enabledFields(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get blame who owned this blameable model. |
78
|
|
|
* NOTICE! This method will not check whether `$hostClass` exists. You should |
79
|
|
|
* specify it in `init()` method. |
80
|
|
|
* @return BaseUserQuery user. |
81
|
|
|
*/ |
82
|
15 |
|
public function getHost() |
83
|
|
|
{ |
84
|
15 |
|
$hostClass = $this->hostClass; |
85
|
15 |
|
$user = $hostClass::buildNoInitModel(); |
86
|
|
|
/* @var BaseUserModel $user */ |
87
|
15 |
|
return $this->hasOne($hostClass::className(), [$user->guidAttribute => 'createdBy']); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get created_by attribute. |
92
|
|
|
* @return string|null |
93
|
|
|
*/ |
94
|
15 |
|
public function getCreatedBy() |
95
|
|
|
{ |
96
|
15 |
|
$createdByAttribute = $this->createdByAttribute; |
97
|
15 |
|
return (!is_string($createdByAttribute) || empty($createdByAttribute)) ? null : $this->$createdByAttribute->getData(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Set host. |
102
|
|
|
* @param Binary|IdentityInterface|string $host |
103
|
|
|
* @return Binary|false |
104
|
|
|
*/ |
105
|
58 |
|
public function setHost($host) |
106
|
|
|
{ |
107
|
58 |
|
if ($host instanceof Binary && $host->getType() == Binary::TYPE_UUID) { |
|
|
|
|
108
|
3 |
|
return $this->{$this->createdByAttribute} = $host; |
109
|
|
|
} |
110
|
58 |
|
if ($host instanceof $this->hostClass || $host instanceof IdentityInterface) { |
111
|
58 |
|
return $this->{$this->createdByAttribute} = new Binary($host->getGUID(), Binary::TYPE_UUID); |
112
|
|
|
} |
113
|
9 |
|
if (is_string($host) && preg_match(Number::GUID_REGEX, $host)) { |
114
|
3 |
|
return $this->{$this->createdByAttribute} = new Binary(Number::guid_bin($host), Binary::TYPE_UUID); |
115
|
|
|
} |
116
|
6 |
|
if (strlen($host) == 16) { |
117
|
3 |
|
return $this->{$this->createdByAttribute} = new Binary($host, Binary::TYPE_UUID); |
118
|
|
|
} |
119
|
3 |
|
return false; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get updater who updated this blameable model recently. |
124
|
|
|
* NOTICE! This method will not check whether `$hostClass` exists. You should |
125
|
|
|
* specify it in `init()` method. |
126
|
|
|
* @return BaseUserQuery user. |
127
|
|
|
*/ |
128
|
19 |
|
public function getUpdater() |
129
|
|
|
{ |
130
|
19 |
|
if (!is_string($this->updatedByAttribute) || empty($this->updatedByAttribute)) { |
131
|
1 |
|
return null; |
132
|
|
|
} |
133
|
18 |
|
$hostClass = $this->hostClass; |
134
|
18 |
|
$host = $hostClass::buildNoInitModel(); |
135
|
|
|
/* @var $user BaseUserModel */ |
136
|
18 |
|
return $this->hasOne($hostClass::className(), [$host->guidAttribute => 'updatedBy']); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get updated_by attribute. |
141
|
|
|
* @return string|null |
142
|
|
|
*/ |
143
|
18 |
|
public function getUpdatedBy() |
144
|
|
|
{ |
145
|
18 |
|
$updatedByAttribute = $this->updatedByAttribute; |
146
|
18 |
|
return (!is_string($updatedByAttribute) || empty($updatedByAttribute)) ? null : $this->$updatedByAttribute->getData(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Set updater. |
151
|
|
|
* @param Binary|IdentityInterface|string $updater |
152
|
|
|
* @return Binary|false |
153
|
|
|
*/ |
154
|
16 |
|
public function setUpdater($updater) |
155
|
|
|
{ |
156
|
16 |
|
if (!is_string($this->updatedByAttribute) || empty($this->updatedByAttribute)) { |
157
|
1 |
|
return false; |
158
|
|
|
} |
159
|
15 |
|
if ($updater instanceof Binary && $updater->getType() == Binary::TYPE_UUID) { |
|
|
|
|
160
|
3 |
|
return $this->{$this->updatedByAttribute} = $updater; |
161
|
|
|
} |
162
|
12 |
|
if ($updater instanceof $this->hostClass || $updater instanceof IdentityInterface) { |
163
|
3 |
|
return $this->{$this->updatedByAttribute} = new Binary($updater->getGUID(), Binary::TYPE_UUID); |
164
|
|
|
} |
165
|
9 |
|
if (is_string($updater) && preg_match(Number::GUID_REGEX, $updater)) { |
166
|
3 |
|
return $this->{$this->updatedByAttribute} = new Binary(Number::guid_bin($updater), Binary::TYPE_UUID); |
167
|
|
|
} |
168
|
6 |
|
if (strlen($updater) == 16) { |
169
|
3 |
|
return $this->{$this->updatedByAttribute} = new Binary($updater, Binary::TYPE_UUID); |
170
|
|
|
} |
171
|
3 |
|
return false; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Return the current user's GUID if current model doesn't specify the owner |
176
|
|
|
* yet, or return the owner's GUID if current model has been specified. |
177
|
|
|
* This method is ONLY used for being triggered by event. DO NOT call, |
178
|
|
|
* override or modify it directly, unless you know the consequences. |
179
|
|
|
* @param ModelEvent $event |
180
|
|
|
* @return Binary the GUID of current user or the owner. |
181
|
|
|
*/ |
182
|
58 |
|
public function onGetCurrentUserGuid($event) |
183
|
|
|
{ |
184
|
58 |
|
$sender = $event->sender; |
185
|
|
|
/* @var $sender static */ |
186
|
58 |
|
if (isset($sender->attributes[$sender->createdByAttribute])) { |
187
|
58 |
|
return $sender->attributes[$sender->createdByAttribute]; |
188
|
|
|
} |
189
|
|
|
$identity = \Yii::$app->user->identity; |
190
|
|
|
/* @var BaseUserModel $identity */ |
191
|
|
|
if ($identity) { |
192
|
|
|
return new Binary($identity->getGUID(), Binary::TYPE_UUID); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.