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
|
|
|
* This trait accepts both an Field or and Field Id and ensures that the both |
17
|
|
|
* the Field and the Id are in sync; If one changes (and does not match the other) it |
18
|
|
|
* resolves (removes / updates) the other. |
19
|
|
|
* |
20
|
|
|
* In addition, this trait is primarily useful when a new Field is set and saved; the Field |
21
|
|
|
* Id can be retrieved without needing to explicitly set the newly created Id. |
22
|
|
|
* |
23
|
|
|
* @property Field|FieldInterface|null $field |
24
|
|
|
* |
25
|
|
|
* @author Flipbox Factory <[email protected]> |
26
|
|
|
* @since 2.0.0 |
27
|
|
|
*/ |
28
|
|
|
trait FieldMutatorTrait |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var Field|FieldInterface|null |
32
|
|
|
*/ |
33
|
|
|
private $field; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Internally set the Field Id. This can be overridden. A record for example |
37
|
|
|
* should use `setAttribute`. |
38
|
|
|
* |
39
|
|
|
* @param int|null $id |
40
|
|
|
* @return $this |
41
|
|
|
*/ |
42
|
|
|
abstract protected function internalSetFieldId(int $id = null); |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Internally get the Field Id. This can be overridden. A record for example |
46
|
|
|
* should use `getAttribute`. |
47
|
|
|
* |
48
|
|
|
* @return int|null |
49
|
|
|
*/ |
50
|
|
|
abstract protected function internalGetFieldId(); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return bool |
54
|
|
|
*/ |
55
|
|
|
public function isFieldSet(): bool |
56
|
|
|
{ |
57
|
|
|
return null !== $this->field; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Set associated fieldId |
62
|
|
|
* |
63
|
|
|
* @param int|null $id |
64
|
|
|
* @return $this |
65
|
|
|
*/ |
66
|
|
|
public function setFieldId(int $id = null) |
67
|
|
|
{ |
68
|
|
|
$this->internalSetFieldId($id); |
69
|
|
|
|
70
|
|
|
if (null !== $this->field && $id !== $this->field->id) { |
71
|
|
|
$this->field = null; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get associated fieldId |
79
|
|
|
* |
80
|
|
|
* @return int|null |
81
|
|
|
*/ |
82
|
|
|
public function getFieldId() |
83
|
|
|
{ |
84
|
|
|
if (null === $this->internalGetFieldId() && null !== $this->field) { |
85
|
|
|
$this->setFieldId($this->field->id); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $this->internalGetFieldId(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Associate a site |
93
|
|
|
* |
94
|
|
|
* @param mixed $field |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
|
|
public function setField($field = null) |
98
|
|
|
{ |
99
|
|
|
$this->field = null; |
100
|
|
|
$this->internalSetFieldId(null); |
101
|
|
|
|
102
|
|
|
if (null !== ($field = $this->verifyField($field))) { |
103
|
|
|
$this->field = $field; |
104
|
|
|
$this->internalSetFieldId($field->id); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return FieldInterface|Field|null |
112
|
|
|
*/ |
113
|
|
|
public function getField() |
114
|
|
|
{ |
115
|
|
|
if ($this->field === null) { |
116
|
|
|
$field = $this->resolveField(); |
117
|
|
|
$this->setField($field); |
118
|
|
|
return $field; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$fieldId = $this->internalGetFieldId(); |
122
|
|
|
if ($fieldId !== null && $fieldId !== $this->field->id) { |
123
|
|
|
$this->field = null; |
124
|
|
|
return $this->getField(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $this->field; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return FieldInterface|Field|null |
132
|
|
|
*/ |
133
|
|
|
protected function resolveField() |
134
|
|
|
{ |
135
|
|
|
if ($site = $this->resolveFieldFromId()) { |
136
|
|
|
return $site; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return null; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return FieldInterface|Field|null |
144
|
|
|
*/ |
145
|
|
|
private function resolveFieldFromId() |
146
|
|
|
{ |
147
|
|
|
if (null === ($fieldId = $this->internalGetFieldId())) { |
148
|
|
|
return null; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
152
|
|
|
return Craft::$app->getFields()->getFieldById($fieldId); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param mixed $field |
157
|
|
|
* @return FieldInterface|FieldInterface|Field|null |
158
|
|
|
*/ |
159
|
|
|
protected function verifyField($field = null) |
160
|
|
|
{ |
161
|
|
|
if (null === $field) { |
162
|
|
|
return null; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
if ($field instanceof FieldInterface) { |
166
|
|
|
return $field; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
if (is_numeric($field)) { |
170
|
|
|
return Craft::$app->getFields()->getFieldById($field); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
if (is_string($field)) { |
174
|
|
|
return Craft::$app->getFields()->getFieldByHandle($field); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return null; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|