Completed
Push — develop ( 7f03d4...6a69af )
by Nate
09:17
created

UserTypeAttributeTrait::isTypeSet()   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 0
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
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