|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* _ __ __ _____ _____ ___ ____ _____ |
|
5
|
|
|
* | | / // // ___//_ _// || __||_ _| |
|
6
|
|
|
* | |/ // /(__ ) / / / /| || | | | |
|
7
|
|
|
* |___//_//____/ /_/ /_/ |_||_| |_| |
|
8
|
|
|
* @link http://vistart.name/ |
|
9
|
|
|
* @copyright Copyright (c) 2016 vistart |
|
10
|
|
|
* @license http://vistart.name/license/ |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace vistart\Models\traits; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Assemble PasswordTrait, RegistrationTrait and IdentityTrait into UserTrait. |
|
17
|
|
|
* |
|
18
|
|
|
* @version 2.0 |
|
19
|
|
|
* @author vistart <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
trait UserTrait |
|
22
|
|
|
{ |
|
23
|
|
|
use PasswordTrait, |
|
24
|
|
|
RegistrationTrait, |
|
25
|
|
|
IdentityTrait; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Create new entity model associated with current user. |
|
29
|
|
|
* if $config does not specify `userClass` property, self will be assigned to. |
|
30
|
|
|
* @param string $className Full qualified class name. |
|
31
|
|
|
* @param array $config name-value pairs that will be used to initialize |
|
32
|
|
|
* the object properties. |
|
33
|
|
|
* @param boolean $loadDefault Determines whether loading default values |
|
34
|
|
|
* after entity model created. |
|
35
|
|
|
* @param boolean $skipIfSet whether existing value should be preserved. |
|
36
|
|
|
* This will only set defaults for attributes that are `null`. |
|
37
|
|
|
* @return $className |
|
|
|
|
|
|
38
|
|
|
*/ |
|
39
|
20 |
|
public function create($className, $config = [], $loadDefault = true, $skipIfSet = true) |
|
40
|
|
|
{ |
|
41
|
20 |
|
if (!isset($config['userClass'])) { |
|
42
|
20 |
|
$config['userClass'] = static::className(); |
|
43
|
20 |
|
} |
|
44
|
20 |
|
if (isset($config['class'])) { |
|
45
|
|
|
unset($config['class']); |
|
46
|
|
|
} |
|
47
|
20 |
|
$entity = new $className($config); |
|
48
|
20 |
|
$createdByAttribute = $entity->createdByAttribute; |
|
49
|
20 |
|
$entity->$createdByAttribute = $this->guid; |
|
|
|
|
|
|
50
|
20 |
|
if ($loadDefault && method_exists($entity, 'loadDefaultValues')) { |
|
51
|
20 |
|
$entity->loadDefaultValues($skipIfSet); |
|
52
|
20 |
|
} |
|
53
|
20 |
|
return $entity; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Find existed or create new model. |
|
58
|
|
|
* @param string $className |
|
59
|
|
|
* @param array $condition |
|
60
|
|
|
* @param array $config |
|
61
|
|
|
* @return $className |
|
|
|
|
|
|
62
|
|
|
*/ |
|
63
|
2 |
|
public function findOneOrCreate($className, $condition = [], $config = null) |
|
64
|
|
|
{ |
|
65
|
2 |
|
$entity = new $className(['skipInit' => true]); |
|
66
|
2 |
|
if (!isset($condition[$entity->createdByAttribute])) { |
|
67
|
2 |
|
$condition[$entity->createdByAttribute] = $this->guid; |
|
68
|
2 |
|
} |
|
69
|
2 |
|
$model = $className::findOne($condition); |
|
70
|
2 |
|
if (!$model) { |
|
71
|
2 |
|
if ($config === null || !is_array($config)) { |
|
72
|
2 |
|
$config = $condition; |
|
73
|
2 |
|
} |
|
74
|
2 |
|
$model = $this->create($className, $config); |
|
75
|
2 |
|
} |
|
76
|
2 |
|
return $model; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Get all rules with current user properties. |
|
81
|
|
|
* @return array all rules. |
|
82
|
|
|
*/ |
|
83
|
34 |
|
public function rules() |
|
84
|
|
|
{ |
|
85
|
34 |
|
return array_merge( |
|
86
|
34 |
|
parent::rules(), |
|
87
|
34 |
|
$this->passwordHashRules, |
|
88
|
34 |
|
$this->passwordResetTokenRules, |
|
89
|
34 |
|
$this->sourceRules, |
|
90
|
34 |
|
$this->statusRules, |
|
91
|
34 |
|
$this->authKeyRules, |
|
92
|
34 |
|
$this->accessTokenRules |
|
93
|
34 |
|
); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
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.