|
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\ember\services\traits\elements; |
|
10
|
|
|
|
|
11
|
|
|
use craft\base\ElementInterface; |
|
12
|
|
|
use craft\elements\db\ElementQueryInterface; |
|
13
|
|
|
use craft\errors\ElementNotFoundException; |
|
14
|
|
|
use craft\helpers\ArrayHelper; |
|
15
|
|
|
use flipbox\ember\helpers\ObjectHelper; |
|
16
|
|
|
use flipbox\ember\helpers\QueryHelper; |
|
17
|
|
|
use flipbox\ember\services\traits\queries\BaseAccessor; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @author Flipbox Factory <[email protected]> |
|
21
|
|
|
* @since 1.0.0 |
|
22
|
|
|
*/ |
|
23
|
|
|
trait Accessor |
|
24
|
|
|
{ |
|
25
|
|
|
use BaseAccessor; |
|
26
|
|
|
|
|
27
|
|
|
/******************************************* |
|
28
|
|
|
* OBJECT CLASSES |
|
29
|
|
|
*******************************************/ |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return string|null |
|
33
|
|
|
*/ |
|
34
|
|
|
abstract public static function elementClass(); |
|
35
|
|
|
|
|
36
|
|
|
/******************************************* |
|
37
|
|
|
* CREATE |
|
38
|
|
|
*******************************************/ |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param array $config |
|
42
|
|
|
* @return ElementInterface |
|
43
|
|
|
* @throws \yii\base\InvalidConfigException |
|
44
|
|
|
*/ |
|
45
|
|
|
public function create($config = []): ElementInterface |
|
46
|
|
|
{ |
|
47
|
|
|
if ($config instanceof ElementInterface) { |
|
48
|
|
|
return $config; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** @var ElementInterface $element */ |
|
52
|
|
|
$element = ObjectHelper::create( |
|
53
|
|
|
$this->prepareConfig($config), |
|
54
|
|
|
ElementInterface::class |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
|
|
return $element; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param array $config |
|
62
|
|
|
* @return array |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function prepareConfig($config = []): array |
|
65
|
|
|
{ |
|
66
|
|
|
if (!is_array($config)) { |
|
67
|
|
|
$config = ArrayHelper::toArray($config, [], false); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
// Auto-set the class |
|
71
|
|
|
$class = static::elementClass(); |
|
72
|
|
|
if ($class !== null) { |
|
73
|
|
|
$config['class'] = $class; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $config; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/******************************************* |
|
80
|
|
|
* QUERY |
|
81
|
|
|
*******************************************/ |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Get query |
|
85
|
|
|
* |
|
86
|
|
|
* @param $criteria |
|
87
|
|
|
* @return ElementQueryInterface |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getQuery($criteria = []): ElementQueryInterface |
|
90
|
|
|
{ |
|
91
|
|
|
/** @var ElementInterface $elementClass */ |
|
92
|
|
|
$elementClass = static::elementClass(); |
|
93
|
|
|
|
|
94
|
|
|
/** @var ElementQueryInterface $query */ |
|
95
|
|
|
$query = $elementClass::find(); |
|
96
|
|
|
|
|
97
|
|
|
// Configure it |
|
98
|
|
|
QueryHelper::configure( |
|
99
|
|
|
$query, |
|
100
|
|
|
$criteria |
|
101
|
|
|
); |
|
102
|
|
|
|
|
103
|
|
|
return $query; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param $identifier |
|
108
|
|
|
* @return array |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function identifierCondition($identifier): array |
|
111
|
|
|
{ |
|
112
|
|
|
$base = [ |
|
113
|
|
|
'status' => null |
|
114
|
|
|
]; |
|
115
|
|
|
|
|
116
|
|
|
if (is_array($identifier)) { |
|
117
|
|
|
return array_merge($base, $identifier); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$base['id'] = $identifier; |
|
121
|
|
|
|
|
122
|
|
|
return $base; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/******************************************* |
|
126
|
|
|
* FIND / GET |
|
127
|
|
|
*******************************************/ |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return ElementInterface[] |
|
131
|
|
|
*/ |
|
132
|
|
|
public function findAll() |
|
133
|
|
|
{ |
|
134
|
|
|
return $this->getQuery()->all(); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @param $identifier |
|
139
|
|
|
* @return ElementInterface|null |
|
140
|
|
|
*/ |
|
141
|
|
|
public function find($identifier) |
|
142
|
|
|
{ |
|
143
|
|
|
if ($identifier instanceof ElementInterface) { |
|
144
|
|
|
return $identifier; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
return $this->findByQuery($this->getQuery( |
|
148
|
|
|
$this->identifierCondition($identifier) |
|
149
|
|
|
)); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param $identifier |
|
154
|
|
|
* @return ElementInterface |
|
155
|
|
|
* @throws ElementNotFoundException |
|
156
|
|
|
*/ |
|
157
|
|
|
public function get($identifier): ElementInterface |
|
158
|
|
|
{ |
|
159
|
|
|
if (!$object = $this->find($identifier)) { |
|
160
|
|
|
$this->notFoundException(); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
return $object; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/******************************************* |
|
167
|
|
|
* EXCEPTIONS |
|
168
|
|
|
*******************************************/ |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @param int|null $id |
|
172
|
|
|
* @throws ElementNotFoundException |
|
173
|
|
|
*/ |
|
174
|
|
|
protected function notFoundByIdException(int $id = null) |
|
175
|
|
|
{ |
|
176
|
|
|
throw new ElementNotFoundException( |
|
177
|
|
|
sprintf( |
|
178
|
|
|
'Element does not exist given the id "%s".', |
|
179
|
|
|
(string)$id |
|
180
|
|
|
) |
|
181
|
|
|
); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @throws ElementNotFoundException |
|
186
|
|
|
*/ |
|
187
|
|
|
protected function notFoundException() |
|
188
|
|
|
{ |
|
189
|
|
|
throw new ElementNotFoundException( |
|
190
|
|
|
sprintf( |
|
191
|
|
|
"Element not found." |
|
192
|
|
|
) |
|
193
|
|
|
); |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|