1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE |
6
|
|
|
* @link https://github.com/flipboxfactory/craft-ember |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\organizations\records; |
10
|
|
|
|
11
|
|
|
use flipbox\craft\ember\records\ActiveRecordTrait; |
12
|
|
|
use flipbox\organizations\objects\UserTypeMutatorTrait; |
13
|
|
|
use flipbox\organizations\records\UserType as TypeRecord; |
14
|
|
|
use yii\base\Model; |
15
|
|
|
use yii\db\ActiveQueryInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Flipbox Factory <[email protected]> |
19
|
|
|
* @since 1.0.0 |
20
|
|
|
* |
21
|
|
|
* @property UserType|null $typeRecord |
22
|
|
|
*/ |
23
|
|
|
trait UserTypeAttributeTrait |
24
|
|
|
{ |
25
|
|
|
use ActiveRecordTrait, |
26
|
|
|
UserTypeMutatorTrait; |
27
|
|
|
|
28
|
|
|
abstract public function populateRelation($name, $records); |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return bool |
32
|
|
|
*/ |
33
|
|
|
public function isTypeSet(): bool |
34
|
|
|
{ |
35
|
|
|
return null !== $this->isRelationPopulated('typeRecord'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return UserType|null |
40
|
|
|
*/ |
41
|
|
|
protected function internalGetType() |
42
|
|
|
{ |
43
|
|
|
return $this->typeRecord; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param int|null $id |
48
|
|
|
*/ |
49
|
|
|
protected function internalSetTypeId(int $id = null) |
50
|
|
|
{ |
51
|
|
|
$this->setAttribute('typeId', $id); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return int|null |
56
|
|
|
*/ |
57
|
|
|
protected function internalGetTypeId() |
58
|
|
|
{ |
59
|
|
|
return $this->getAttribute('typeId'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param UserType|null $type |
64
|
|
|
*/ |
65
|
|
|
protected function internalSetType(UserType $type = null) |
66
|
|
|
{ |
67
|
|
|
$this->populateRelation('typeRecord', $type); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
|
|
protected function typeRules(): array |
74
|
|
|
{ |
75
|
|
|
return [ |
76
|
|
|
[ |
77
|
|
|
[ |
78
|
|
|
'typeId' |
79
|
|
|
], |
80
|
|
|
'number', |
81
|
|
|
'integerOnly' => true |
82
|
|
|
], |
83
|
|
|
[ |
84
|
|
|
[ |
85
|
|
|
'typeId', |
86
|
|
|
'type' |
87
|
|
|
], |
88
|
|
|
'safe', |
89
|
|
|
'on' => [ |
90
|
|
|
Model::SCENARIO_DEFAULT |
91
|
|
|
] |
92
|
|
|
] |
93
|
|
|
]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return ActiveQueryInterface |
98
|
|
|
*/ |
99
|
|
|
protected function getTypeRecord() |
100
|
|
|
{ |
101
|
|
|
return $this->hasOne( |
102
|
|
|
TypeRecord::class, |
103
|
|
|
['id' => 'typeId'] |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|