Passed
Push — master ( c44bf2...7de111 )
by Aimeos
05:51
created

Methods::getSubManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 1
c 2
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 2
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
	/**
29
	 * Adds a filter callback for an item type
30
	 *
31
	 * @param string $iface Interface name of the item to apply the filter to
32
	 * @param \Closure $fcn Anonymous function receiving the item to check as first parameter
33
	 */
34
	public function addFilter( string $iface, \Closure $fcn )
35
	{
36
		if( !isset( $this->filterFcn[$iface] ) ) {
37
			$this->filterFcn[$iface] = [];
38
		}
39
40
		$this->filterFcn[$iface][] = $fcn;
41
	}
42
43
44
	/**
45
	 * Returns the class names of the manager and used decorators.
46
	 *
47
	 * @return array List of class names
48
	 */
49
	public function classes() : array
50
	{
51
		return [get_class( $this )];
52
	}
53
54
55
	/**
56
	 * Removes old entries from the storage
57
	 *
58
	 * @param iterable $siteids List of IDs for sites whose entries should be deleted
59
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls
60
	 */
61
	public function clear( iterable $siteids ) : \Aimeos\MShop\Common\Manager\Iface
62
	{
63
		return $this;
64
	}
65
66
67
	/**
68
	 * Creates a new empty item instance
69
	 *
70
	 * @param array $values Values the item should be initialized with
71
	 * @return \Aimeos\MShop\Attribute\Item\Iface New attribute item object
72
	 */
73
	public function create( array $values = [] ) : \Aimeos\MShop\Common\Item\Iface
74
	{
75
		return new \Aimeos\MShop\Common\Item\Base( '', $values );
76
	}
77
78
79
	/**
80
	 * Creates a new cursor based on the filter criteria
81
	 *
82
	 * @param \Aimeos\Base\Criteria\Iface $filter Criteria object with conditions, sortations, etc.
83
	 * @return \Aimeos\MShop\Common\Cursor\Iface Cursor object
84
	 */
85
	public function cursor( \Aimeos\Base\Criteria\Iface $filter ) : \Aimeos\MShop\Common\Cursor\Iface
86
	{
87
		return new \Aimeos\MShop\Common\Cursor\Standard( $filter );
88
	}
89
90
91
	/**
92
	 * Deletes one or more items.
93
	 *
94
	 * @param \Aimeos\MShop\Common\Item\Iface|\Aimeos\Map|array|string $items Item object, ID or a list of them
95
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls
96
	 */
97
	public function delete( $items ) : \Aimeos\MShop\Common\Manager\Iface
98
	{
99
		return $this;
100
	}
101
102
103
	/**
104
	 * Creates a filter object.
105
	 *
106
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
107
	 * @param bool $site TRUE for adding site criteria to limit items by the site of related items
108
	 * @return \Aimeos\Base\Criteria\Iface Returns the filter object
109
	 */
110
	public function filter( ?bool $default = false, bool $site = false ) : \Aimeos\Base\Criteria\Iface
111
	{
112
		throw new \LogicException( 'Not implemented' );
113
	}
114
115
116
	/**
117
	 * Returns the item specified by its ID
118
	 *
119
	 * @param string $id Id of item
120
	 * @param string[] $ref List of domains to fetch list items and referenced items for
121
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
122
	 * @return \Aimeos\MShop\Common\Item\Iface Item object
123
	 */
124
	public function get( string $id, array $ref = [], ?bool $default = false ) : \Aimeos\MShop\Common\Item\Iface
125
	{
126
		throw new \LogicException( 'Not implemented' );
127
	}
128
129
130
	/**
131
	 * Returns the available manager types
132
	 *
133
	 * @param bool $withsub Return also the resource type of sub-managers if true
134
	 * @return string[] Type of the manager and submanagers, subtypes are separated by slashes
135
	 */
136
	public function getResourceType( bool $withsub = true ) : array
137
	{
138
		return [$this->getManagerPath()];
139
	}
140
141
142
	/**
143
	 * Returns the additional column/search definitions
144
	 *
145
	 * @return array Associative list of column names as keys and items implementing \Aimeos\Base\Criteria\Attribute\Iface
146
	 */
147
	public function getSaveAttributes() : array
148
	{
149
		return [];
150
	}
151
152
153
	/**
154
	 * Returns the attributes that can be used for searching.
155
	 *
156
	 * @param bool $withsub Return also attributes of sub-managers if true
157
	 * @return \Aimeos\Base\Criteria\Attribute\Iface[] List of attribute items
158
	 */
159
	public function getSearchAttributes( bool $withsub = true ) : array
160
	{
161
		return [];
162
	}
163
164
165
	/**
166
	 * Returns a new manager for attribute extensions
167
	 *
168
	 * @param string $manager Name of the sub manager type in lower case
169
	 * @param string|null $name Name of the implementation, will be from configuration (or Default) if null
170
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager for different extensions, e.g Type, List's etc.
171
	 */
172
	public function getSubManager( string $manager, string $name = null ) : \Aimeos\MShop\Common\Manager\Iface
173
	{
174
		throw new \LogicException( 'Not implemented' );
175
	}
176
177
178
	/**
179
	 * Iterates over all matched items and returns the found ones
180
	 *
181
	 * @param \Aimeos\MShop\Common\Cursor\Iface $cursor Cursor object with filter, domains and cursor
182
	 * @param string[] $ref List of domains whose items should be fetched too
183
	 * @return \Aimeos\Map|null List of items implementing \Aimeos\MShop\Common\Item\Iface with ids as keys
184
	 */
185
	public function iterate( \Aimeos\MShop\Common\Cursor\Iface $cursor, array $ref = [] ) : ?\Aimeos\Map
186
	{
187
		return null;
188
	}
189
190
191
	/**
192
	 * Adds or updates an item object or a list of them.
193
	 *
194
	 * @param \Aimeos\MShop\Common\Item\Iface[]|\Aimeos\MShop\Common\Item\Iface $items Item or list of items whose data should be saved
195
	 * @param bool $fetch True if the new ID should be returned in the item
196
	 * @return \Aimeos\MShop\Common\Item\Iface[]|\Aimeos\MShop\Common\Item\Iface Saved item or items
197
	 */
198
	public function save( $items, bool $fetch = true )
199
	{
200
		return $items;
201
	}
202
203
204
	/**
205
	 * Searches for all items matching the given critera.
206
	 *
207
	 * @param \Aimeos\Base\Criteria\Iface $filter Criteria object with conditions, sortations, etc.
208
	 * @param string[] $ref List of domains to fetch list items and referenced items for
209
	 * @param int &$total Number of items that are available in total
210
	 * @return \Aimeos\Map List of items implementing \Aimeos\MShop\Common\Item\Iface with ids as keys
211
	 */
212
	public function search( \Aimeos\Base\Criteria\Iface $filter, array $ref = [], int &$total = null ) : \Aimeos\Map
213
	{
214
		return map();
215
	}
216
217
218
	/**
219
	 * Injects the reference of the outmost object
220
	 *
221
	 * @param \Aimeos\MShop\Common\Manager\Iface $object Reference to the outmost manager or decorator
222
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls
223
	 */
224
	public function setObject( \Aimeos\MShop\Common\Manager\Iface $object ) : \Aimeos\MShop\Common\Manager\Iface
225
	{
226
		$this->object = $object;
227
		return $this;
228
	}
229
230
231
	/**
232
	 * Starts a database transaction on the connection identified by the given name
233
	 *
234
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls
235
	 */
236
	public function begin() : \Aimeos\MShop\Common\Manager\Iface
237
	{
238
		return $this;
239
	}
240
241
242
	/**
243
	 * Commits the running database transaction on the connection identified by the given name
244
	 *
245
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls
246
	 */
247
	public function commit() : \Aimeos\MShop\Common\Manager\Iface
248
	{
249
		return $this;
250
	}
251
252
253
	/**
254
	 * Rolls back the running database transaction on the connection identified by the given name
255
	 *
256
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls
257
	 */
258
	public function rollback() : \Aimeos\MShop\Common\Manager\Iface
259
	{
260
		return $this;
261
	}
262
263
264
	/**
265
	 * Applies the filters for the item type to the item
266
	 *
267
	 * @param object $item Item to apply the filter to
268
	 * @return object|null Object if the item should be used, null if not
269
	 */
270
	protected function applyFilter( $item )
271
	{
272
		foreach( $this->filterFcn as $iface => $fcnList )
273
		{
274
			if( is_object( $item ) && $item instanceof $iface )
275
			{
276
				foreach( $fcnList as $fcn )
277
				{
278
					if( $fcn( $item ) === null ) {
279
						return null;
280
					}
281
				}
282
			}
283
		}
284
285
		return $item;
286
	}
287
288
289
	/**
290
	 * Creates the criteria attribute items from the list of entries
291
	 *
292
	 * @param array $list Associative array of code as key and array with properties as values
293
	 * @return \Aimeos\Base\Criteria\Attribute\Standard[] List of criteria attribute items
294
	 */
295
	protected function createAttributes( array $list ) : array
296
	{
297
		$attr = [];
298
299
		foreach( $list as $key => $fields )
300
		{
301
			$fields['code'] = $fields['code'] ?? $key;
302
			$attr[$key] = new \Aimeos\Base\Criteria\Attribute\Standard( $fields );
303
		}
304
305
		return $attr;
306
	}
307
308
309
	/**
310
	 * Returns the full configuration key for the passed last part
311
	 *
312
	 * @param string $name Configuration last part
313
	 * @return string Full configuration key
314
	 */
315
	protected function getConfigKey( string $name ) : string
316
	{
317
		$subPath = $this->getSubPath();
318
		return 'mshop/' . $this->getDomain() . '/manager/' . ( $subPath ? $subPath . '/' : '' ) . $name;
319
	}
320
321
322
	/**
323
	 * Returns the manager domain
324
	 *
325
	 * @return string Manager domain e.g. "product"
326
	 */
327
	protected function getDomain() : string
328
	{
329
		if( !isset( $this->domain ) ) {
330
			$this->initMethods();
331
		}
332
333
		return $this->domain;
334
	}
335
336
337
	/**
338
	 * Returns the manager path
339
	 *
340
	 * @return string Manager path e.g. "product/lists/type"
341
	 */
342
	protected function getManagerPath() : string
343
	{
344
		$subPath = $this->getSubPath();
345
		return $this->getDomain() . ( $subPath ? '/' . $subPath : '' );
346
	}
347
348
349
	/**
350
	 * Returns the attribute helper functions for searching defined by the manager.
351
	 *
352
	 * @param \Aimeos\Base\Criteria\Attribute\Iface[] $attributes List of search attribute items
353
	 * @return array Associative array of attribute code and helper function
354
	 */
355
	protected function getSearchFunctions( array $attributes ) : array
356
	{
357
		$list = [];
358
		$iface = \Aimeos\Base\Criteria\Attribute\Iface::class;
359
360
		foreach( $attributes as $key => $item )
361
		{
362
			if( $item instanceof $iface ) {
363
				$list[$item->getCode()] = $item->getFunction();
364
			} else if( isset( $item['code'] ) ) {
365
				$list[$item['code']] = $item['function'] ?? null;
366
			} else {
367
				throw new \Aimeos\MShop\Exception( sprintf( 'Invalid attribute at position "%1$d"', $key ) );
368
			}
369
		}
370
371
		return $list;
372
	}
373
374
375
	/**
376
	 * Returns the item search key for the passed name
377
	 *
378
	 * @return string Item prefix e.g. "product.lists.type.id"
379
	 */
380
	protected function getSearchKey( string $name = '' ) : string
381
	{
382
		$subPath = $this->getSubPath();
383
		return $this->getDomain() . ( $subPath ? '.' . $subPath : '' ) . ( $name ? '.' . $name : '' );
384
	}
385
386
387
	/**
388
	 * Returns the attribute translations for searching defined by the manager.
389
	 *
390
	 * @param \Aimeos\Base\Criteria\Attribute\Iface[] $attributes List of search attribute items
391
	 * @return array Associative array of attribute code and internal attribute code
392
	 */
393
	protected function getSearchTranslations( array $attributes ) : array
394
	{
395
		$translations = [];
396
		$iface = \Aimeos\Base\Criteria\Attribute\Iface::class;
397
398
		foreach( $attributes as $key => $item )
399
		{
400
			if( $item instanceof $iface ) {
401
				$translations[$item->getCode()] = $item->getInternalCode();
402
			} else if( isset( $item['code'] ) ) {
403
				$translations[$item['code']] = $item['internalcode'];
404
			} else {
405
				throw new \Aimeos\MShop\Exception( sprintf( 'Invalid attribute at position "%1$d"', $key ) );
406
			}
407
		}
408
409
		return $translations;
410
	}
411
412
413
	/**
414
	 * Returns the attribute types for searching defined by the manager.
415
	 *
416
	 * @param \Aimeos\Base\Criteria\Attribute\Iface[] $attributes List of search attribute items
417
	 * @return array Associative array of attribute code and internal attribute type
418
	 */
419
	protected function getSearchTypes( array $attributes ) : array
420
	{
421
		$types = [];
422
		$iface = \Aimeos\Base\Criteria\Attribute\Iface::class;
423
424
		foreach( $attributes as $key => $item )
425
		{
426
			if( $item instanceof $iface ) {
427
				$types[$item->getCode()] = $item->getInternalType();
428
			} else if( isset( $item['code'] ) ) {
429
				$types[$item['code']] = $item['internaltype'];
430
			} else {
431
				throw new \Aimeos\MShop\Exception( sprintf( 'Invalid attribute at position "%1$d"', $key ) );
432
			}
433
		}
434
435
		return $types;
436
	}
437
438
439
	/**
440
	 * Returns the manager domain sub-path
441
	 *
442
	 * @return string Manager domain sub-path e.g. "lists/type"
443
	 */
444
	protected function getSubPath() : string
445
	{
446
		if( !isset( $this->subpath ) ) {
447
			$this->initMethods();
448
		}
449
450
		return $this->subpath;
451
	}
452
453
454
	/**
455
	 * Returns the name of the used table
456
	 *
457
	 * @return string Table name e.g. "mshop_product_lists_type"
458
	 */
459
	protected function getTable() : string
460
	{
461
		$subPath = $this->getSubPath();
462
		return 'mshop_' . $this->getDomain() . ( $subPath ? '_' . str_replace( '/', '_', $subPath ) : '' );
463
	}
464
465
466
	/**
467
	 * Initializes the trait
468
	 */
469
	protected function initMethods()
470
	{
471
		$parts = explode( '\\', strtolower( get_class( $this ) ) );
472
		array_shift( $parts ); array_shift( $parts ); // remove "Aimeos\MShop"
473
		array_pop( $parts );
474
475
		$this->domain = array_shift( $parts ) ?: '';
476
		array_shift( $parts ); // remove "manager"
477
		$this->subpath = join( '/', $parts );
478
	}
479
480
481
	/**
482
	 * Returns the outmost decorator of the decorator stack
483
	 *
484
	 * @return \Aimeos\MShop\Common\Manager\Iface Outmost decorator object
485
	 */
486
	protected function object() : \Aimeos\MShop\Common\Manager\Iface
487
	{
488
		return $this->object ?? $this;
489
	}
490
}
491