|
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 yii\base\InvalidConfigException; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @version 1.0 |
|
19
|
|
|
* @author vistart <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
Trait SubsidiaryTrait |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var string[] Subsidiary map. |
|
25
|
|
|
* Array key represents class alias, |
|
26
|
|
|
* array value represents the full qualified class name corresponds to the alias. |
|
27
|
|
|
* |
|
28
|
|
|
* For example: |
|
29
|
|
|
* ```php |
|
30
|
|
|
* public $subsidiaryMap = [ |
|
31
|
|
|
* 'Profile' => 'app\models\user\Profile', |
|
32
|
|
|
* ]; |
|
33
|
|
|
* ``` |
|
34
|
|
|
* or: |
|
35
|
|
|
* ```php |
|
36
|
|
|
* public $subsidiaryMap = [ |
|
37
|
|
|
* 'Profile' => [ |
|
38
|
|
|
* 'class' => 'app\models\user\Profile', |
|
39
|
|
|
* 'max' => 1, |
|
40
|
|
|
* ] |
|
41
|
|
|
* ]; |
|
42
|
|
|
* |
|
43
|
|
|
* If you want to create subsidiary model and the class is not found, the array elements will be taken. |
|
44
|
|
|
*/ |
|
45
|
|
|
public $subsidiaryMap = []; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Add subsidiary. |
|
49
|
|
|
* @param string $name |
|
50
|
|
|
* @param string|array $config |
|
51
|
|
|
* @return boolean |
|
52
|
|
|
* @throws InvalidConfigException |
|
53
|
|
|
*/ |
|
54
|
6 |
|
public function addSubsidiary($name, $config) |
|
55
|
|
|
{ |
|
56
|
6 |
|
if (!is_string($name) || empty($name)) { |
|
57
|
1 |
|
throw new InvalidConfigException('Subsidiary name not specified.'); |
|
58
|
|
|
} |
|
59
|
6 |
|
$name = strtolower($name); |
|
60
|
6 |
|
if (!is_array($config)) { |
|
61
|
6 |
|
if (is_string($config) && !empty($config)) { |
|
62
|
6 |
|
$this->subsidiaryMap[$name] = ['class' => $config]; |
|
63
|
|
|
} else { |
|
64
|
6 |
|
throw new InvalidConfigException('Subsidiary class not specified.'); |
|
65
|
|
|
} |
|
66
|
|
|
} else { |
|
67
|
1 |
|
if (isset($config['class']) && class_exists($config['class'])) { |
|
68
|
1 |
|
$this->subsidiaryMap[$name] = $config; |
|
69
|
|
|
} else { |
|
70
|
1 |
|
throw new InvalidConfigException('Subsidiary class not specified.'); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
6 |
|
return true; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $name |
|
79
|
|
|
* @return boolean |
|
80
|
|
|
*/ |
|
81
|
1 |
|
public function removeSubsidiary($name) |
|
82
|
|
|
{ |
|
83
|
1 |
|
$name = strtolower($name); |
|
84
|
1 |
|
if (array_key_exists($name, $this->subsidiaryMap)) { |
|
85
|
1 |
|
unset($this->subsidiaryMap[$name]); |
|
86
|
1 |
|
return true; |
|
87
|
|
|
} |
|
88
|
1 |
|
return false; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $name |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
6 |
|
public function getSubsidiaryClass($name) |
|
97
|
|
|
{ |
|
98
|
6 |
|
$name = strtolower($name); |
|
99
|
6 |
|
if (array_key_exists($name, $this->subsidiaryMap) && array_key_exists('class', $this->subsidiaryMap[$name])) |
|
100
|
|
|
{ |
|
101
|
6 |
|
return class_exists($this->subsidiaryMap[$name]['class']) ? $this->subsidiaryMap[$name]['class'] : null; |
|
102
|
|
|
} |
|
103
|
1 |
|
return null; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
2 |
|
public function getSubsidiaries($name, $limit = 'all', $page = 0) |
|
107
|
|
|
{ |
|
108
|
2 |
|
$class = $this->getSubsidiaryClass($name); |
|
109
|
2 |
|
if (empty($class)) { |
|
110
|
1 |
|
return null; |
|
111
|
|
|
} |
|
112
|
2 |
|
$query = $class::find(); |
|
113
|
2 |
|
if (!method_exists($query, 'createdBy')) { |
|
114
|
|
|
return null; |
|
115
|
|
|
} |
|
116
|
2 |
|
return $class::find()->createdBy($this)->page($limit, $page)->all(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
6 |
|
public function __call($name, $arguments) |
|
120
|
|
|
{ |
|
121
|
6 |
|
if (strpos(strtolower($name), "create") === 0) { |
|
122
|
6 |
|
$class = strtolower(substr($name, 6)); |
|
123
|
6 |
|
$config = (isset($arguments) && isset($arguments[0])) ? $arguments[0] : []; |
|
124
|
6 |
|
return $this->createSubsidiary($class, $config); |
|
125
|
|
|
} |
|
126
|
|
|
return parent::__call($name, $arguments); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Find existed, or create new model. |
|
131
|
|
|
* If model to be found doesn't exist, and $config is null, the parameter |
|
132
|
|
|
* `$condition` will be regarded as properties of new model. |
|
133
|
|
|
* If you want to know whether the returned model is new model, please check |
|
134
|
|
|
* the return value of `getIsNewRecord()` method. |
|
135
|
|
|
* @param string $className Full qualified class name. |
|
136
|
|
|
* @param array $condition Search condition, or properties if not found and |
|
137
|
|
|
* $config is null. |
|
138
|
|
|
* @param array $config new model's configuration array. If you specify this |
|
139
|
|
|
* parameter, the $condition will be skipped when created one. |
|
140
|
|
|
* @return [[$className]] the existed model, or new model created by specified |
|
|
|
|
|
|
141
|
|
|
* condition or configuration. |
|
142
|
|
|
*/ |
|
143
|
1 |
|
public function findOneOrCreate($className, $condition = [], $config = null) |
|
144
|
|
|
{ |
|
145
|
1 |
|
$entity = new $className(['skipInit' => true]); |
|
146
|
1 |
|
if (!isset($condition[$entity->createdByAttribute])) { |
|
147
|
1 |
|
$condition[$entity->createdByAttribute] = $this->getGUID(); |
|
|
|
|
|
|
148
|
|
|
} |
|
149
|
1 |
|
$model = $className::findOne($condition); |
|
150
|
1 |
|
if (!$model) { |
|
151
|
1 |
|
if ($config === null || !is_array($config)) { |
|
152
|
1 |
|
$config = $condition; |
|
153
|
|
|
} |
|
154
|
1 |
|
$model = $this->create($className, $config); |
|
155
|
|
|
} |
|
156
|
1 |
|
return $model; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Create new entity model associated with current user. The model to be created |
|
161
|
|
|
* must be extended from [[BaseBlameableModel]], [[BaseMongoBlameableModel]], |
|
162
|
|
|
* [[BaseRedisBlameableModel]], or any other classes used [[BlameableTrait]]. |
|
163
|
|
|
* if $config does not specify `userClass` property, self will be assigned to. |
|
164
|
|
|
* @param string $className Full qualified class name. |
|
165
|
|
|
* @param array $config name-value pairs that will be used to initialize |
|
166
|
|
|
* the object properties. |
|
167
|
|
|
* @param boolean $loadDefault Determines whether loading default values |
|
168
|
|
|
* after entity model created. |
|
169
|
|
|
* Notice! The [[\yii\mongodb\ActiveRecord]] and [[\yii\redis\ActiveRecord]] |
|
170
|
|
|
* does not support loading default value. If you want to assign properties |
|
171
|
|
|
* with default values, please define the `default` rule(s) for properties in |
|
172
|
|
|
* `rules()` method and return them by yourself if you don't specified them in config param. |
|
173
|
|
|
* @param boolean $skipIfSet whether existing value should be preserved. |
|
174
|
|
|
* This will only set defaults for attributes that are `null`. |
|
175
|
|
|
* @return [[$className]] new model created with specified configuration. |
|
|
|
|
|
|
176
|
|
|
*/ |
|
177
|
60 |
|
public function create($className, $config = [], $loadDefault = true, $skipIfSet = true) |
|
178
|
|
|
{ |
|
179
|
60 |
|
if (!isset($config['hostClass'])) { |
|
180
|
60 |
|
$config['hostClass'] = static::class; |
|
181
|
|
|
} |
|
182
|
60 |
|
if (isset($config['class'])) { |
|
183
|
18 |
|
unset($config['class']); |
|
184
|
|
|
} |
|
185
|
60 |
|
$entity = new $className($config); |
|
186
|
60 |
|
$entity->setHost($this); |
|
187
|
60 |
|
if ($loadDefault && method_exists($entity, 'loadDefaultValues')) { |
|
188
|
30 |
|
$entity->loadDefaultValues($skipIfSet); |
|
189
|
|
|
} |
|
190
|
60 |
|
return $entity; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* |
|
195
|
|
|
* @param string $name |
|
196
|
|
|
* @param array $config |
|
197
|
|
|
* @return type |
|
198
|
|
|
*/ |
|
199
|
6 |
|
public function createSubsidiary($name, $config) |
|
200
|
|
|
{ |
|
201
|
6 |
|
if (!is_string($name) || empty($name)) { |
|
202
|
1 |
|
return null; |
|
203
|
|
|
} |
|
204
|
6 |
|
$className = ''; |
|
|
|
|
|
|
205
|
6 |
|
if (class_exists($name)) { |
|
206
|
1 |
|
$className = $name; |
|
207
|
6 |
|
} elseif (array_key_exists($name, $this->subsidiaryMap)) { |
|
208
|
6 |
|
$className = $this->getSubsidiaryClass($name); |
|
209
|
|
|
} else { |
|
210
|
1 |
|
return null; |
|
211
|
|
|
} |
|
212
|
6 |
|
return $this->create($className, $config); |
|
213
|
|
|
} |
|
214
|
|
|
} |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.