Completed
Push — master ( 11971b...eeafb3 )
by Nate
01:16
created

FieldMutatorTrait::resolveField()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
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
     * Set associated fieldId
30
     *
31
     * @param int|null $id
32
     * @return $this
33
     */
34
    public function setFieldId(int $id = null)
35
    {
36
        $this->fieldId = $id;
37
        return $this;
38
    }
39
40
    /**
41
     * Get associated fieldId
42
     *
43
     * @return int|null
44
     */
45
    public function getFieldId()
46
    {
47
        if (null === $this->fieldId && null !== $this->field) {
48
            $this->fieldId = $this->field->id;
49
        }
50
51
        return $this->fieldId;
52
    }
53
54
    /**
55
     * Associate a site
56
     *
57
     * @param mixed $field
58
     * @return $this
59
     */
60
    public function setField($field = null)
61
    {
62
        $this->field = null;
63
64
        if (!$field = $this->internalResolveField($field)) {
65
            $this->field = $this->fieldId = null;
66
        } else {
67
            $this->fieldId = $field->id;
68
            $this->field = $field;
69
        }
70
71
        return $this;
72
    }
73
74
    /**
75
     * @return FieldInterface|Field|null
76
     */
77
    public function getField()
78
    {
79
        if ($this->field === null) {
80
            $site = $this->resolveField();
81
            $this->setField($site);
82
            return $site;
83
        }
84
85
        $fieldId = $this->fieldId;
86
        if ($fieldId !== null &&
87
            $fieldId !== $this->field->id
88
        ) {
89
            $this->field = null;
90
            return $this->getField();
91
        }
92
93
        return $this->field;
94
    }
95
96
    /**
97
     * @return FieldInterface|Field|null
98
     */
99
    protected function resolveField()
100
    {
101
        if ($site = $this->resolveFieldFromId()) {
102
            return $site;
103
        }
104
105
        return null;
106
    }
107
108
    /**
109
     * @return FieldInterface|Field|null
110
     */
111
    private function resolveFieldFromId()
112
    {
113
        if (null === $this->fieldId) {
114
            return null;
115
        }
116
117
        /** @noinspection PhpIncompatibleReturnTypeInspection */
118
        return Craft::$app->getFields()->getFieldById($this->fieldId);
119
    }
120
121
    /**
122
     * @param mixed $field
123
     * @return FieldInterface|FieldInterface|Field|null
124
     */
125
    protected function internalResolveField($field = null)
126
    {
127
        if ($field instanceof FieldInterface) {
128
            return $field;
129
        }
130
131
        if (is_numeric($field)) {
132
            return Craft::$app->getFields()->getFieldById($field);
133
        }
134
135
        if (is_string($field)) {
136
            return Craft::$app->getFields()->getFieldByHandle($field);
137
        }
138
139
        return Craft::$app->getFields()->createField($field);
140
    }
141
}
142