Passed
Push — master ( 994c79...baa5ed )
by Aimeos
05:05
created

Methods::getPrefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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( $this->getPrefix(), $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
			$fields['internalcode'] = $fields['internalcode'] ?? $key;
303
			$attr[$key] = new \Aimeos\Base\Criteria\Attribute\Standard( $fields );
304
		}
305
306
		return $attr;
307
	}
308
309
310
	/**
311
	 * Returns the table alias name.
312
	 *
313
	 * @return string Table alias name
314
	 */
315
	protected function getAlias() : string
316
	{
317
		$parts = explode( '/', $this->getSubPath() );
318
		$str = 'm' . substr( $this->getDomain(), 0, 3 );
319
320
		foreach( $parts as $part ) {
321
			$str .= substr( $part, 0, 2 );
322
		}
323
324
		return $str;
325
	}
326
327
328
	/**
329
	 * Returns the full configuration key for the passed last part
330
	 *
331
	 * @param string $name Configuration last part
332
	 * @return string Full configuration key
333
	 */
334
	protected function getConfigKey( string $name ) : string
335
	{
336
		$subPath = $this->getSubPath();
337
		return 'mshop/' . $this->getDomain() . '/manager/' . ( $subPath ? $subPath . '/' : '' ) . $name;
338
	}
339
340
341
	/**
342
	 * Returns the manager domain
343
	 *
344
	 * @return string Manager domain e.g. "product"
345
	 */
346
	protected function getDomain() : string
347
	{
348
		if( !isset( $this->domain ) ) {
349
			$this->initMethods();
350
		}
351
352
		return $this->domain;
353
	}
354
355
356
	/**
357
	 * Returns the manager path
358
	 *
359
	 * @return string Manager path e.g. "product/lists/type"
360
	 */
361
	protected function getManagerPath() : string
362
	{
363
		$subPath = $this->getSubPath();
364
		return $this->getDomain() . ( $subPath ? '/' . $subPath : '' );
365
	}
366
367
368
	/**
369
	 * Returns the prefix for the item properties and search keys.
370
	 *
371
	 * @return string Prefix for the item properties and search keys
372
	 */
373
	protected function getPrefix() : string
374
	{
375
		return '';
376
	}
377
378
379
	/**
380
	 * Returns the attribute helper functions for searching defined by the manager.
381
	 *
382
	 * @param \Aimeos\Base\Criteria\Attribute\Iface[] $attributes List of search attribute items
383
	 * @return array Associative array of attribute code and helper function
384
	 */
385
	protected function getSearchFunctions( array $attributes ) : array
386
	{
387
		$list = [];
388
		$iface = \Aimeos\Base\Criteria\Attribute\Iface::class;
389
390
		foreach( $attributes as $key => $item )
391
		{
392
			if( $item instanceof $iface ) {
393
				$list[$item->getCode()] = $item->getFunction();
394
			} else if( isset( $item['code'] ) ) {
395
				$list[$item['code']] = $item['function'] ?? null;
396
			} else {
397
				throw new \Aimeos\MShop\Exception( sprintf( 'Invalid attribute at position "%1$d"', $key ) );
398
			}
399
		}
400
401
		return $list;
402
	}
403
404
405
	/**
406
	 * Returns the item search key for the passed name
407
	 *
408
	 * @return string Item prefix e.g. "product.lists.type.id"
409
	 */
410
	protected function getSearchKey( string $name = '' ) : string
411
	{
412
		$subPath = $this->getSubPath();
413
		return $this->getDomain() . ( $subPath ? '.' . $subPath : '' ) . ( $name ? '.' . $name : '' );
414
	}
415
416
417
	/**
418
	 * Returns the attribute translations for searching defined by the manager.
419
	 *
420
	 * @param \Aimeos\Base\Criteria\Attribute\Iface[] $attributes List of search attribute items
421
	 * @return array Associative array of attribute code and internal attribute code
422
	 */
423
	protected function getSearchTranslations( array $attributes ) : array
424
	{
425
		$translations = [];
426
		$iface = \Aimeos\Base\Criteria\Attribute\Iface::class;
427
428
		foreach( $attributes as $key => $item )
429
		{
430
			if( $item instanceof $iface ) {
431
				$translations[$item->getCode()] = $item->getInternalCode();
432
			} else if( isset( $item['code'] ) ) {
433
				$translations[$item['code']] = $item['internalcode'];
434
			} else {
435
				throw new \Aimeos\MShop\Exception( sprintf( 'Invalid attribute at position "%1$d"', $key ) );
436
			}
437
		}
438
439
		return $translations;
440
	}
441
442
443
	/**
444
	 * Returns the attribute types for searching defined by the manager.
445
	 *
446
	 * @param \Aimeos\Base\Criteria\Attribute\Iface[] $attributes List of search attribute items
447
	 * @return array Associative array of attribute code and internal attribute type
448
	 */
449
	protected function getSearchTypes( array $attributes ) : array
450
	{
451
		$types = [];
452
		$iface = \Aimeos\Base\Criteria\Attribute\Iface::class;
453
454
		foreach( $attributes as $key => $item )
455
		{
456
			if( $item instanceof $iface ) {
457
				$types[$item->getCode()] = $item->getInternalType();
458
			} else if( isset( $item['code'] ) ) {
459
				$types[$item['code']] = $item['internaltype'];
460
			} else {
461
				throw new \Aimeos\MShop\Exception( sprintf( 'Invalid attribute at position "%1$d"', $key ) );
462
			}
463
		}
464
465
		return $types;
466
	}
467
468
469
	/**
470
	 * Returns the manager domain sub-path
471
	 *
472
	 * @return string Manager domain sub-path e.g. "lists/type"
473
	 */
474
	protected function getSubPath() : string
475
	{
476
		if( !isset( $this->subpath ) ) {
477
			$this->initMethods();
478
		}
479
480
		return $this->subpath;
481
	}
482
483
484
	/**
485
	 * Returns the name of the used table
486
	 *
487
	 * @return string Table name e.g. "mshop_product_lists_type"
488
	 */
489
	protected function getTable() : string
490
	{
491
		$subPath = $this->getSubPath();
492
		return 'mshop_' . $this->getDomain() . ( $subPath ? '_' . str_replace( '/', '_', $subPath ) : '' );
493
	}
494
495
496
	/**
497
	 * Initializes the trait
498
	 */
499
	protected function initMethods()
500
	{
501
		$parts = explode( '\\', strtolower( get_class( $this ) ) );
502
		array_shift( $parts ); array_shift( $parts ); // remove "Aimeos\MShop"
503
		array_pop( $parts );
504
505
		$this->domain = array_shift( $parts ) ?: '';
506
		array_shift( $parts ); // remove "manager"
507
		$this->subpath = join( '/', $parts );
508
	}
509
510
511
	/**
512
	 * Returns the outmost decorator of the decorator stack
513
	 *
514
	 * @return \Aimeos\MShop\Common\Manager\Iface Outmost decorator object
515
	 */
516
	protected function object() : \Aimeos\MShop\Common\Manager\Iface
517
	{
518
		return $this->object ?? $this;
519
	}
520
}
521