1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://flipboxfactory.com/software/meta/license |
6
|
|
|
* @link https://www.flipboxfactory.com/software/meta/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\meta\services; |
10
|
|
|
|
11
|
|
|
use flipbox\craft\sortable\associations\db\SortableAssociationQueryInterface; |
12
|
|
|
use flipbox\craft\sortable\associations\records\SortableAssociationInterface; |
13
|
|
|
use flipbox\craft\sortable\associations\services\SortableAssociations; |
14
|
|
|
use flipbox\ember\services\traits\records\Accessor; |
15
|
|
|
use flipbox\meta\db\MetaAssociationsQuery; |
16
|
|
|
use flipbox\meta\db\MetaQuery; |
17
|
|
|
use flipbox\meta\records\Meta as MetaRecord; |
18
|
|
|
use yii\db\ActiveQuery; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Flipbox Factory <[email protected]> |
22
|
|
|
* @since 1.0.0 |
23
|
|
|
* |
24
|
|
|
* @method MetaAssociationsQuery parentGetQuery($config = []) |
25
|
|
|
* @method MetaRecord create(array $attributes = []) |
26
|
|
|
* @method MetaRecord find($identifier) |
27
|
|
|
* @method MetaRecord get($identifier) |
28
|
|
|
* @method MetaRecord findByCondition($condition = []) |
29
|
|
|
* @method MetaRecord getByCondition($condition = []) |
30
|
|
|
* @method MetaRecord findByCriteria($criteria = []) |
31
|
|
|
* @method MetaRecord getByCriteria($criteria = []) |
32
|
|
|
* @method MetaRecord[] findAllByCondition($condition = []) |
33
|
|
|
* @method MetaRecord[] getAllByCondition($condition = []) |
34
|
|
|
* @method MetaRecord[] findAllByCriteria($criteria = []) |
35
|
|
|
* @method MetaRecord[] getAllByCriteria($criteria = []) |
36
|
|
|
*/ |
37
|
|
|
class Records extends SortableAssociations |
38
|
|
|
{ |
39
|
|
|
use Accessor; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritdoc |
43
|
|
|
*/ |
44
|
|
|
const SOURCE_ATTRIBUTE = MetaRecord::SOURCE_ATTRIBUTE; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @inheritdoc |
48
|
|
|
*/ |
49
|
|
|
const TARGET_ATTRIBUTE = MetaRecord::TARGET_ATTRIBUTE; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @inheritdoc |
53
|
|
|
*/ |
54
|
|
|
public static function recordClass(): string |
55
|
|
|
{ |
56
|
|
|
return MetaRecord::class; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @inheritdoc |
61
|
|
|
*/ |
62
|
|
|
protected static function tableAlias(): string |
63
|
|
|
{ |
64
|
|
|
return MetaRecord::tableAlias(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @inheritdoc |
69
|
|
|
* @return MetaQuery |
70
|
|
|
*/ |
71
|
|
|
public function getQuery($config = []): SortableAssociationQueryInterface |
72
|
|
|
{ |
73
|
|
|
return new MetaAssociationsQuery(static::recordClass(), $config); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritdoc |
78
|
|
|
* |
79
|
|
|
* @param MetaRecord $record |
80
|
|
|
* @return MetaQuery |
81
|
|
|
*/ |
82
|
|
|
protected function associationQuery( |
83
|
|
|
SortableAssociationInterface $record |
84
|
|
|
): SortableAssociationQueryInterface { |
85
|
|
|
return $this->query( |
86
|
|
|
$record->{static::SOURCE_ATTRIBUTE}, |
87
|
|
|
$record->fieldId, |
|
|
|
|
88
|
|
|
$record->ownerSiteId |
|
|
|
|
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @inheritdoc |
94
|
|
|
* |
95
|
|
|
* @param MetaQuery $query |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
|
|
protected function existingAssociations( |
99
|
|
|
SortableAssociationQueryInterface $query |
100
|
|
|
): array { |
101
|
|
|
$source = $this->resolveStringAttribute($query, static::SOURCE_ATTRIBUTE); |
102
|
|
|
$field = $this->resolveStringAttribute($query, 'fieldId'); |
103
|
|
|
$site = $this->resolveStringAttribute($query, 'siteId'); |
104
|
|
|
|
105
|
|
|
if ($source === null || $field === null || $site === null) { |
106
|
|
|
return []; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $this->associations($source, $field, $site); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param $source |
114
|
|
|
* @param int $fieldId |
115
|
|
|
* @param int $siteId |
116
|
|
|
* @return SortableAssociationQueryInterface|ActiveQuery |
117
|
|
|
*/ |
118
|
|
|
private function query( |
119
|
|
|
$source, |
120
|
|
|
int $fieldId, |
121
|
|
|
int $siteId = null |
122
|
|
|
): SortableAssociationQueryInterface { |
123
|
|
|
return $this->getQuery() |
124
|
|
|
->where([ |
125
|
|
|
static::SOURCE_ATTRIBUTE => $source, |
126
|
|
|
'fieldId' => $fieldId, |
127
|
|
|
'ownerSiteId' => $siteId |
128
|
|
|
]) |
129
|
|
|
->orderBy(['sortOrder' => SORT_ASC]); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param $source |
134
|
|
|
* @param int $fieldId |
135
|
|
|
* @param int $siteId |
136
|
|
|
* @return array |
137
|
|
|
*/ |
138
|
|
|
private function associations( |
139
|
|
|
$source, |
140
|
|
|
int $fieldId, |
141
|
|
|
int $siteId |
142
|
|
|
): array { |
143
|
|
|
return $this->query($source, $fieldId, $siteId) |
|
|
|
|
144
|
|
|
->indexBy(static::TARGET_ATTRIBUTE) |
145
|
|
|
->all(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
// /** |
149
|
|
|
// * @param SortableAssociationQueryInterface $query |
150
|
|
|
// * @return \flipbox\domains\fields\Domains|null |
151
|
|
|
// */ |
152
|
|
|
// protected function resolveFieldFromQuery( |
153
|
|
|
// SortableAssociationQueryInterface $query |
154
|
|
|
// ) { |
155
|
|
|
// if (null === ($fieldId = $this->resolveStringAttribute($query, 'fieldId'))) { |
156
|
|
|
// return null; |
157
|
|
|
// } |
158
|
|
|
// |
159
|
|
|
// return MetaPlugin::getInstance()->getFields()->findById($fieldId); |
160
|
|
|
// } |
161
|
|
|
// |
162
|
|
|
// /** |
163
|
|
|
// * @inheritdoc |
164
|
|
|
// * @param bool $validate |
165
|
|
|
// * @throws \Exception |
166
|
|
|
// */ |
167
|
|
|
// public function save( |
168
|
|
|
// SortableAssociationQueryInterface $query, |
169
|
|
|
// bool $validate = true |
170
|
|
|
// ): bool { |
171
|
|
|
// if ($validate === true && null !== ($field = $this->resolveFieldFromQuery($query))) { |
172
|
|
|
// $error = ''; |
173
|
|
|
// |
174
|
|
|
// (new MinMaxValidator([ |
175
|
|
|
// 'min' => $field->min, |
176
|
|
|
// 'max' => $field->max |
177
|
|
|
// ]))->validate($query, $error); |
178
|
|
|
// |
179
|
|
|
// if (!empty($error)) { |
180
|
|
|
// MetaPlugin::error(sprintf( |
181
|
|
|
// "Meta failed to save due to the following validation errors: '%s'", |
182
|
|
|
// Json::encode($error) |
183
|
|
|
// )); |
184
|
|
|
// return false; |
185
|
|
|
// } |
186
|
|
|
// } |
187
|
|
|
// |
188
|
|
|
// return parent::save($query); |
189
|
|
|
// } |
190
|
|
|
} |
191
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: