Completed
Push — master ( eae9dd...13bbf2 )
by Nate
06:11
created

FieldMutatorTrait::isFieldSet()   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\craft\ember\objects;
10
11
use Craft;
12
use craft\base\Field;
13
use craft\base\FieldInterface;
14
15
/**
16
 * @property int|null $fieldId
17
 *
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 2.0.0
20
 */
21
trait FieldMutatorTrait
22
{
23
    /**
24
     * @var Field|FieldInterface|null
25
     */
26
    private $field;
27
28
    /**
29
     * @return bool
30
     */
31
    public function isFieldSet(): bool
32
    {
33
        return null !== $this->field;
34
    }
35
36
    /**
37
     * Set associated fieldId
38
     *
39
     * @param int|null $id
40
     * @return $this
41
     */
42
    public function setFieldId(int $id = null)
43
    {
44
        $this->fieldId = $id;
45
        return $this;
46
    }
47
48
    /**
49
     * Get associated fieldId
50
     *
51
     * @return int|null
52
     */
53
    public function getFieldId()
54
    {
55
        if (null === $this->fieldId && null !== $this->field) {
56
            $this->fieldId = $this->field->id;
57
        }
58
59
        return $this->fieldId;
60
    }
61
62
    /**
63
     * Associate a site
64
     *
65
     * @param mixed $field
66
     * @return $this
67
     */
68
    public function setField($field = null)
69
    {
70
        $this->field = null;
71
72
        if (!$field = $this->internalResolveField($field)) {
73
            $this->field = $this->fieldId = null;
74
        } else {
75
            $this->fieldId = $field->id;
76
            $this->field = $field;
77
        }
78
79
        return $this;
80
    }
81
82
    /**
83
     * @return FieldInterface|Field|null
84
     */
85
    public function getField()
86
    {
87
        if ($this->field === null) {
88
            $site = $this->resolveField();
89
            $this->setField($site);
90
            return $site;
91
        }
92
93
        $fieldId = $this->fieldId;
94
        if ($fieldId !== null &&
95
            $fieldId !== $this->field->id
96
        ) {
97
            $this->field = null;
98
            return $this->getField();
99
        }
100
101
        return $this->field;
102
    }
103
104
    /**
105
     * @return FieldInterface|Field|null
106
     */
107
    protected function resolveField()
108
    {
109
        if ($site = $this->resolveFieldFromId()) {
110
            return $site;
111
        }
112
113
        return null;
114
    }
115
116
    /**
117
     * @return FieldInterface|Field|null
118
     */
119
    private function resolveFieldFromId()
120
    {
121
        if (null === $this->fieldId) {
122
            return null;
123
        }
124
125
        /** @noinspection PhpIncompatibleReturnTypeInspection */
126
        return Craft::$app->getFields()->getFieldById($this->fieldId);
127
    }
128
129
    /**
130
     * @param mixed $field
131
     * @return FieldInterface|FieldInterface|Field|null
132
     */
133
    protected function internalResolveField($field = null)
134
    {
135
        if ($field instanceof FieldInterface) {
136
            return $field;
137
        }
138
139
        if (is_numeric($field)) {
140
            return Craft::$app->getFields()->getFieldById($field);
141
        }
142
143
        if (is_string($field)) {
144
            return Craft::$app->getFields()->getFieldByHandle($field);
145
        }
146
147
        return Craft::$app->getFields()->createField($field);
148
    }
149
}
150