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\OrganizationTypeMutatorTrait; |
13
|
|
|
use yii\base\Model; |
14
|
|
|
use yii\db\ActiveQueryInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Flipbox Factory <[email protected]> |
18
|
|
|
* @since 1.0.0 |
19
|
|
|
* |
20
|
|
|
* @property OrganizationType|null $typeRecord |
21
|
|
|
*/ |
22
|
|
|
trait OrganizationTypeAttributeTrait |
23
|
|
|
{ |
24
|
|
|
use ActiveRecordTrait, |
25
|
|
|
OrganizationTypeMutatorTrait; |
26
|
|
|
|
27
|
|
|
abstract public function populateRelation($name, $records); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return bool |
31
|
|
|
*/ |
32
|
|
|
public function isTypeSet(): bool |
33
|
|
|
{ |
34
|
|
|
return null !== $this->isRelationPopulated('typeRecord'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return OrganizationType|null |
39
|
|
|
*/ |
40
|
|
|
protected function internalGetType() |
41
|
|
|
{ |
42
|
|
|
return $this->typeRecord; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param int|null $id |
47
|
|
|
*/ |
48
|
|
|
protected function internalSetTypeId(int $id = null) |
49
|
|
|
{ |
50
|
|
|
$this->setAttribute('typeId', $id); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return int|null |
55
|
|
|
*/ |
56
|
|
|
protected function internalGetTypeId() |
57
|
|
|
{ |
58
|
|
|
return $this->getAttribute('typeId'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param OrganizationType|null $type |
63
|
|
|
*/ |
64
|
|
|
protected function internalSetType(OrganizationType $type = null) |
65
|
|
|
{ |
66
|
|
|
$this->populateRelation('typeRecord', $type); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
protected function typeRules(): array |
73
|
|
|
{ |
74
|
|
|
return [ |
75
|
|
|
[ |
76
|
|
|
[ |
77
|
|
|
'typeId' |
78
|
|
|
], |
79
|
|
|
'number', |
80
|
|
|
'integerOnly' => true |
81
|
|
|
], |
82
|
|
|
[ |
83
|
|
|
[ |
84
|
|
|
'typeId', |
85
|
|
|
'type' |
86
|
|
|
], |
87
|
|
|
'safe', |
88
|
|
|
'on' => [ |
89
|
|
|
Model::SCENARIO_DEFAULT |
90
|
|
|
] |
91
|
|
|
] |
92
|
|
|
]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return ActiveQueryInterface |
97
|
|
|
*/ |
98
|
|
|
protected function getTypeRecord() |
99
|
|
|
{ |
100
|
|
|
return $this->hasOne( |
101
|
|
|
OrganizationType::class, |
102
|
|
|
['id' => 'typeId'] |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|