Completed
Push — develop ( 0f5a88...a06987 )
by Nate
04:58
created

UserTypeAttributeTrait::internalSetType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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
use yii\db\ActiveRecordInterface;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 1.0.0
21
 *
22
 * @property UserType|null $typeRecord
23
 */
24
trait UserTypeAttributeTrait
25
{
26
    use ActiveRecordTrait,
27
        UserTypeMutatorTrait;
28
29
    /**
30
     * Populates the named relation with the related records.
31
     * Note that this method does not check if the relation exists or not.
32
     * @param string $name the relation name, e.g. `orders` for a relation defined via `getOrders()` method (case-sensitive).
33
     * @param ActiveRecordInterface|array|null $records the related records to be populated into the relation.
34
     * @see getRelation()
35
     */
36
    abstract public function populateRelation($name, $records);
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