|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @license LGPLv3, https://opensource.org/licenses/LGPL-3.0 |
|
5
|
|
|
* @copyright Aimeos (aimeos.org), 2023 |
|
6
|
|
|
* @package MShop |
|
7
|
|
|
* @subpackage Common |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
namespace Aimeos\MShop\Common\Manager; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Method trait for managers |
|
16
|
|
|
* |
|
17
|
|
|
* @package MShop |
|
18
|
|
|
* @subpackage Common |
|
19
|
|
|
*/ |
|
20
|
|
|
trait Methods |
|
21
|
|
|
{ |
|
22
|
|
|
private ?\Aimeos\MShop\Common\Manager\Iface $object = null; |
|
23
|
|
|
private array $filterFcn = []; |
|
24
|
|
|
private string $domain; |
|
25
|
|
|
private string $subpath; |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
protected function init() |
|
29
|
|
|
{ |
|
30
|
|
|
$parts = explode( '\\', strtolower( get_class( $this ) ) ); |
|
31
|
|
|
array_shift( $parts ); array_shift( $parts ); // remove "Aimeos\MShop" |
|
32
|
|
|
array_pop( $parts ); |
|
33
|
|
|
|
|
34
|
|
|
$this->domain = array_shift( $parts ) ?: ''; |
|
35
|
|
|
array_shift( $parts ); // remove "manager" |
|
36
|
|
|
$this->subpath = join( '/', $parts ); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Adds a filter callback for an item type |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $iface Interface name of the item to apply the filter to |
|
44
|
|
|
* @param \Closure $fcn Anonymous function receiving the item to check as first parameter |
|
45
|
|
|
*/ |
|
46
|
|
|
public function addFilter( string $iface, \Closure $fcn ) |
|
47
|
|
|
{ |
|
48
|
|
|
if( !isset( $this->filterFcn[$iface] ) ) { |
|
49
|
|
|
$this->filterFcn[$iface] = []; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$this->filterFcn[$iface][] = $fcn; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Returns the class names of the manager and used decorators. |
|
58
|
|
|
* |
|
59
|
|
|
* @return array List of class names |
|
60
|
|
|
*/ |
|
61
|
|
|
public function classes() : array |
|
62
|
|
|
{ |
|
63
|
|
|
return [get_class( $this )]; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Injects the reference of the outmost object |
|
69
|
|
|
* |
|
70
|
|
|
* @param \Aimeos\MShop\Common\Manager\Iface $object Reference to the outmost manager or decorator |
|
71
|
|
|
* @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls |
|
72
|
|
|
*/ |
|
73
|
|
|
public function setObject( \Aimeos\MShop\Common\Manager\Iface $object ) : \Aimeos\MShop\Common\Manager\Iface |
|
74
|
|
|
{ |
|
75
|
|
|
$this->object = $object; |
|
76
|
|
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Applies the filters for the item type to the item |
|
82
|
|
|
* |
|
83
|
|
|
* @param object $item Item to apply the filter to |
|
84
|
|
|
* @return object|null Object if the item should be used, null if not |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function applyFilter( $item ) |
|
87
|
|
|
{ |
|
88
|
|
|
foreach( $this->filterFcn as $iface => $fcnList ) |
|
89
|
|
|
{ |
|
90
|
|
|
if( is_object( $item ) && $item instanceof $iface ) |
|
91
|
|
|
{ |
|
92
|
|
|
foreach( $fcnList as $fcn ) |
|
93
|
|
|
{ |
|
94
|
|
|
if( $fcn( $item ) === null ) { |
|
95
|
|
|
return null; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $item; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Creates the criteria attribute items from the list of entries |
|
107
|
|
|
* |
|
108
|
|
|
* @param array $list Associative array of code as key and array with properties as values |
|
109
|
|
|
* @return \Aimeos\Base\Criteria\Attribute\Standard[] List of criteria attribute items |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function createAttributes( array $list ) : array |
|
112
|
|
|
{ |
|
113
|
|
|
$attr = []; |
|
114
|
|
|
|
|
115
|
|
|
foreach( $list as $key => $fields ) |
|
116
|
|
|
{ |
|
117
|
|
|
$fields['code'] = $fields['code'] ?? $key; |
|
118
|
|
|
$attr[$key] = new \Aimeos\Base\Criteria\Attribute\Standard( $fields ); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $attr; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Returns the full configuration key for the passed last part |
|
127
|
|
|
* |
|
128
|
|
|
* @param string $name Configuration last part |
|
129
|
|
|
* @return string Full configuration key |
|
130
|
|
|
*/ |
|
131
|
|
|
protected function getConfigKey( string $name ) : string |
|
132
|
|
|
{ |
|
133
|
|
|
return 'mshop/' . $this->domain . '/manager/' . ( $this->subpath ? $this->subpath . '/' : '' ) . $name; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Returns the manager domain |
|
139
|
|
|
* |
|
140
|
|
|
* @return string Manager domain e.g. "product" |
|
141
|
|
|
*/ |
|
142
|
|
|
protected function getDomain() : string |
|
143
|
|
|
{ |
|
144
|
|
|
return $this->domain; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Returns the manager path |
|
150
|
|
|
* |
|
151
|
|
|
* @return string Manager path e.g. "product/lists/type" |
|
152
|
|
|
*/ |
|
153
|
|
|
protected function getManagerPath() : string |
|
154
|
|
|
{ |
|
155
|
|
|
return $this->domain . ( $this->subpath ? '/' . $this->subpath : '' ); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Returns the attribute helper functions for searching defined by the manager. |
|
161
|
|
|
* |
|
162
|
|
|
* @param \Aimeos\Base\Criteria\Attribute\Iface[] $attributes List of search attribute items |
|
163
|
|
|
* @return array Associative array of attribute code and helper function |
|
164
|
|
|
*/ |
|
165
|
|
|
protected function getSearchFunctions( array $attributes ) : array |
|
166
|
|
|
{ |
|
167
|
|
|
$list = []; |
|
168
|
|
|
$iface = \Aimeos\Base\Criteria\Attribute\Iface::class; |
|
169
|
|
|
|
|
170
|
|
|
foreach( $attributes as $key => $item ) |
|
171
|
|
|
{ |
|
172
|
|
|
if( $item instanceof $iface ) { |
|
173
|
|
|
$list[$item->getCode()] = $item->getFunction(); |
|
174
|
|
|
} else if( isset( $item['code'] ) ) { |
|
175
|
|
|
$list[$item['code']] = $item['function'] ?? null; |
|
176
|
|
|
} else { |
|
177
|
|
|
throw new \Aimeos\MShop\Exception( sprintf( 'Invalid attribute at position "%1$d"', $key ) ); |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
return $list; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Returns the item search key for the passed name |
|
187
|
|
|
* |
|
188
|
|
|
* @return string Item prefix e.g. "product.lists.type.id" |
|
189
|
|
|
*/ |
|
190
|
|
|
protected function getSearchKey( string $name = '' ) : string |
|
191
|
|
|
{ |
|
192
|
|
|
return $this->domain . ( $this->subpath ? '.' . $this->subpath : '' ) . ( $name ? '.' . $name : '' ); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Returns the attribute translations for searching defined by the manager. |
|
198
|
|
|
* |
|
199
|
|
|
* @param \Aimeos\Base\Criteria\Attribute\Iface[] $attributes List of search attribute items |
|
200
|
|
|
* @return array Associative array of attribute code and internal attribute code |
|
201
|
|
|
*/ |
|
202
|
|
|
protected function getSearchTranslations( array $attributes ) : array |
|
203
|
|
|
{ |
|
204
|
|
|
$translations = []; |
|
205
|
|
|
$iface = \Aimeos\Base\Criteria\Attribute\Iface::class; |
|
206
|
|
|
|
|
207
|
|
|
foreach( $attributes as $key => $item ) |
|
208
|
|
|
{ |
|
209
|
|
|
if( $item instanceof $iface ) { |
|
210
|
|
|
$translations[$item->getCode()] = $item->getInternalCode(); |
|
211
|
|
|
} else if( isset( $item['code'] ) ) { |
|
212
|
|
|
$translations[$item['code']] = $item['internalcode']; |
|
213
|
|
|
} else { |
|
214
|
|
|
throw new \Aimeos\MShop\Exception( sprintf( 'Invalid attribute at position "%1$d"', $key ) ); |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
return $translations; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Returns the attribute types for searching defined by the manager. |
|
224
|
|
|
* |
|
225
|
|
|
* @param \Aimeos\Base\Criteria\Attribute\Iface[] $attributes List of search attribute items |
|
226
|
|
|
* @return array Associative array of attribute code and internal attribute type |
|
227
|
|
|
*/ |
|
228
|
|
|
protected function getSearchTypes( array $attributes ) : array |
|
229
|
|
|
{ |
|
230
|
|
|
$types = []; |
|
231
|
|
|
$iface = \Aimeos\Base\Criteria\Attribute\Iface::class; |
|
232
|
|
|
|
|
233
|
|
|
foreach( $attributes as $key => $item ) |
|
234
|
|
|
{ |
|
235
|
|
|
if( $item instanceof $iface ) { |
|
236
|
|
|
$types[$item->getCode()] = $item->getInternalType(); |
|
237
|
|
|
} else if( isset( $item['code'] ) ) { |
|
238
|
|
|
$types[$item['code']] = $item['internaltype']; |
|
239
|
|
|
} else { |
|
240
|
|
|
throw new \Aimeos\MShop\Exception( sprintf( 'Invalid attribute at position "%1$d"', $key ) ); |
|
241
|
|
|
} |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
return $types; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* Returns the name of the used table |
|
250
|
|
|
* |
|
251
|
|
|
* @return string Table name e.g. "mshop_product_lists_type" |
|
252
|
|
|
*/ |
|
253
|
|
|
protected function getTable() : string |
|
254
|
|
|
{ |
|
255
|
|
|
return 'mshop_' . $this->domain . ( $this->subpath ?: '' ); |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* Returns the outmost decorator of the decorator stack |
|
261
|
|
|
* |
|
262
|
|
|
* @return \Aimeos\MShop\Common\Manager\Iface Outmost decorator object |
|
263
|
|
|
*/ |
|
264
|
|
|
protected function object() : \Aimeos\MShop\Common\Manager\Iface |
|
265
|
|
|
{ |
|
266
|
|
|
return $this->object ?? $this; |
|
267
|
|
|
} |
|
268
|
|
|
} |
|
269
|
|
|
|