/
ElementMutatorTrait.php
151 lines (129 loc) · 3.22 KB
/
ElementMutatorTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/**
* @copyright Copyright (c) Flipbox Digital Limited
* @license https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE
* @link https://github.com/flipboxfactory/craft-ember/
*/
namespace flipbox\craft\ember\objects;
use Craft;
use craft\base\Element;
use craft\base\ElementInterface;
/**
* @property int|null $elementId
* @property Element|ElementInterface|null $element
*
* @author Flipbox Factory <hello@flipboxfactory.com>
* @since 2.0.0
*/
trait ElementMutatorTrait
{
/**
* @var Element|null
*/
private $element;
/**
* @return bool
*/
public function isElementSet(): bool
{
return null !== $this->element;
}
/**
* Set associated elementId
*
* @param $id
* @return $this
*/
public function setElementId(int $id)
{
$this->elementId = $id;
return $this;
}
/**
* Get associated elementId
*
* @return int|null
*/
public function getElementId()
{
if (null === $this->elementId && null !== $this->element) {
$this->elementId = $this->element->id;
}
return $this->elementId;
}
/**
* Associate a element
*
* @param mixed $element
* @return $this
*/
public function setElement($element = null)
{
$this->element = null;
if (!$element = $this->internalResolveElement($element)) {
$this->element = $this->elementId = null;
} else {
/** @var Element $element */
$this->elementId = $element->id;
$this->element = $element;
}
return $this;
}
/**
* @return ElementInterface|null
*/
public function getElement()
{
/** @var Element $element */
if ($this->element === null) {
$element = $this->resolveElement();
$this->setElement($element);
return $element;
}
$elementId = $this->elementId;
if ($elementId !== null &&
$elementId !== $this->element->id
) {
$this->element = null;
return $this->getElement();
}
return $this->element;
}
/**
* @return ElementInterface|null
*/
protected function resolveElement()
{
if ($model = $this->resolveElementFromId()) {
return $model;
}
return null;
}
/**
* @return ElementInterface|null
*/
private function resolveElementFromId()
{
if (null === $this->elementId) {
return null;
}
return Craft::$app->getElements()->getElementById($this->elementId);
}
/**
* @param mixed $element
* @return ElementInterface|Element|null
*/
protected function internalResolveElement($element = null)
{
if ($element instanceof ElementInterface) {
return $element;
}
if (is_numeric($element)) {
return Craft::$app->getElements()->getElementById($element);
}
if (is_string($element)) {
return Craft::$app->getElements()->getElementByUri($element);
}
return Craft::$app->getElements()->createElement($element);
}
}