Completed
Push — develop ( 3f9c00...d25215 )
by Nate
02:35
created

FieldAttributeTrait::fieldAttributeLabels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
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\craft\ember\records;
10
11
use Craft;
12
use craft\base\Field;
13
use craft\base\FieldInterface;
14
use craft\records\Field as FieldRecord;
15
use flipbox\craft\ember\models\FieldRulesTrait;
16
use flipbox\craft\ember\objects\FieldMutatorTrait;
17
use yii\db\ActiveQueryInterface;
18
19
/**
20
 * Intended to be used on an ActiveRecord, this class provides `$this->userId` attribute along with 'getters'
21
 * and 'setters' to ensure continuity between the Id and Object.  An user object is lazy loaded when called.
22
 * In addition, ActiveRecord rules are available.
23
 *
24
 * @author Flipbox Factory <[email protected]>
25
 * @since 2.0.0
26
 *
27
 * @property FieldRecord $fieldRecord
28
 */
29
trait FieldAttributeTrait
30
{
31
    use ActiveRecordTrait,
32
        FieldRulesTrait,
33
        FieldMutatorTrait;
34
35
    /**
36
     * @inheritdoc
37
     */
38
    public function fieldAttributes(): array
39
    {
40
        return [
41
            'fieldId'
42
        ];
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48
    public function fieldAttributeLabels(): array
49
    {
50
        return [
51
            'fieldId' => Craft::t('app', 'Field Id')
52
        ];
53
    }
54
55
    /**
56
     * @inheritDoc
57
     */
58
    protected function internalSetFieldId(int $id = null)
59
    {
60
        $this->setAttribute('fieldId', $id);
61
        return $this;
62
    }
63
64
    /**
65
     * @inheritDoc
66
     */
67
    protected function internalGetFieldId()
68
    {
69
        if (null === ($id = $this->getAttribute('fieldId'))) {
70
            return null;
71
        }
72
        return (int)$id;
73
    }
74
75
    /**
76
     * @return FieldInterface|Field|null
77
     */
78
    protected function resolveField()
79
    {
80
        if ($field = $this->resolveFieldFromRelation()) {
81
            return $field;
82
        }
83
84
        return $this->resolveFieldFromId();
85
    }
86
87
    /**
88
     * @return FieldInterface|Field|null
89
     */
90
    private function resolveFieldFromRelation()
91
    {
92
        if (false === $this->isRelationPopulated('fieldRecord')) {
93
            return null;
94
        }
95
96
        if (null === ($record = $this->getRelation('fieldRecord'))) {
97
            return null;
98
        }
99
100
        /** @var FieldRecord $record */
101
102
        return Craft::$app->getFields()->getFieldById($record->id);
103
    }
104
105
    /**
106
     * Get the associated Field record
107
     *
108
     * @return ActiveQueryInterface
109
     */
110
    public function getFieldRecord(): ActiveQueryInterface
111
    {
112
        return $this->hasOne(
113
            FieldRecord::class,
114
            ['id' => 'fieldId']
115
        );
116
    }
117
}
118