Completed
Push — master ( a13539...bd1b6d )
by Aimeos
04:27
created

Base::log()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2020
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm;
12
13
sprintf( 'type' ); // for translation
14
15
16
/**
17
 * Common abstract class for all admin client classes.
18
 *
19
 * @package Admin
20
 * @subpackage JQAdm
21
 */
22
abstract class Base
23
	implements \Aimeos\Admin\JQAdm\Iface
24
{
25
	private $view;
26
	private $aimeos;
27
	private $context;
28
	private $subclients;
29
30
31
	/**
32
	 * Initializes the class instance.
33
	 *
34
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
35
	 */
36
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
37
	{
38
		$this->context = $context;
39
	}
40
41
42
	/**
43
	 * Catch unknown methods
44
	 *
45
	 * @param string $name Name of the method
46
	 * @param array $param List of method parameter
47
	 * @throws \Aimeos\Admin\JQAdm\Exception If method call failed
48
	 */
49
	public function __call( string $name, array $param )
50
	{
51
		throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Unable to call method "%1$s"', $name ) );
52
	}
53
54
55
	/**
56
	 * Returns the Aimeos bootstrap object
57
	 *
58
	 * @return \Aimeos\Bootstrap The Aimeos bootstrap object
59
	 */
60
	public function getAimeos() : \Aimeos\Bootstrap
61
	{
62
		if( !isset( $this->aimeos ) ) {
63
			throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Aimeos object not available' ) );
64
		}
65
66
		return $this->aimeos;
67
	}
68
69
70
	/**
71
	 * Sets the Aimeos bootstrap object
72
	 *
73
	 * @param \Aimeos\Bootstrap $aimeos The Aimeos bootstrap object
74
	 * @return \Aimeos\Admin\JQAdm\Iface Reference to this object for fluent calls
75
	 */
76
	public function setAimeos( \Aimeos\Bootstrap $aimeos ) : \Aimeos\Admin\JQAdm\Iface
77
	{
78
		$this->aimeos = $aimeos;
79
		return $this;
80
	}
81
82
83
	/**
84
	 * Returns the view object that will generate the admin output.
85
	 *
86
	 * @return \Aimeos\MW\View\Iface The view object which generates the admin output
87
	 */
88
	public function getView() : \Aimeos\MW\View\Iface
89
	{
90
		if( !isset( $this->view ) ) {
91
			throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'No view available' ) );
92
		}
93
94
		return $this->view;
95
	}
96
97
98
	/**
99
	 * Sets the view object that will generate the admin output.
100
	 *
101
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the admin output
102
	 * @return \Aimeos\Admin\JQAdm\Iface Reference to this object for fluent calls
103
	 */
104
	public function setView( \Aimeos\MW\View\Iface $view ) : \Aimeos\Admin\JQAdm\Iface
105
	{
106
		$this->view = $view;
107
		return $this;
108
	}
109
110
111
	/**
112
	 * Copies a resource
113
	 *
114
	 * @return string|null Output to display
115
	 */
116
	public function copy() : ?string
117
	{
118
		foreach( $this->getSubClients() as $client ) {
119
			$client->copy();
120
		}
121
122
		return null;
123
	}
124
125
126
	/**
127
	 * Creates a new resource
128
	 *
129
	 * @return string|null Output to display
130
	 */
131
	public function create() : ?string
132
	{
133
		foreach( $this->getSubClients() as $client ) {
134
			$client->create();
135
		}
136
137
		return null;
138
	}
139
140
141
	/**
142
	 * Deletes a resource
143
	 *
144
	 * @return string|null Output to display
145
	 */
146
	public function delete() : ?string
147
	{
148
		foreach( $this->getSubClients() as $client ) {
149
			$client->delete();
150
		}
151
152
		return null;
153
	}
154
155
156
	/**
157
	 * Exports a resource
158
	 *
159
	 * @return string|null Output to display
160
	 */
161
	public function export() : ?string
162
	{
163
		foreach( $this->getSubClients() as $client ) {
164
			$client->export();
165
		}
166
167
		return null;
168
	}
169
170
171
	/**
172
	 * Returns a resource
173
	 *
174
	 * @return string|null Output to display
175
	 */
176
	public function get() : ?string
177
	{
178
		foreach( $this->getSubClients() as $client ) {
179
			$client->get();
180
		}
181
182
		return null;
183
	}
184
185
186
	/**
187
	 * Imports a resource
188
	 *
189
	 * @return string|null Output to display
190
	 */
191
	public function import() : ?string
192
	{
193
		foreach( $this->getSubClients() as $client ) {
194
			$client->import();
195
		}
196
197
		return null;
198
	}
199
200
201
	/**
202
	 * Saves the data
203
	 *
204
	 * @return string|null Output to display
205
	 */
206
	public function save() : ?string
207
	{
208
		foreach( $this->getSubClients() as $client ) {
209
			$client->save();
210
		}
211
212
		return null;
213
	}
214
215
216
	/**
217
	 * Returns a list of resource according to the conditions
218
	 *
219
	 * @return string|null Output to display
220
	 */
221
	public function search() : ?string
222
	{
223
		foreach( $this->getSubClients() as $client ) {
224
			$client->search();
225
		}
226
227
		return null;
228
	}
229
230
231
	/**
232
	 * Adds the decorators to the client object
233
	 *
234
	 * @param \Aimeos\Admin\JQAdm\Iface $client Admin object
235
	 * @param array $decorators List of decorator name that should be wrapped around the client
236
	 * @param string $classprefix Decorator class prefix, e.g. "\Aimeos\Admin\JQAdm\Catalog\Decorator\"
237
	 * @return \Aimeos\Admin\JQAdm\Iface Admin object
238
	 */
239
	protected function addDecorators( \Aimeos\Admin\JQAdm\Iface $client, array $decorators, string $classprefix ) : \Aimeos\Admin\JQAdm\Iface
240
	{
241
		foreach( $decorators as $name )
242
		{
243
			if( ctype_alnum( $name ) === false )
244
			{
245
				$classname = is_string( $name ) ? $classprefix . $name : '<not a string>';
246
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Invalid class name "%1$s"', $classname ) );
247
			}
248
249
			$classname = $classprefix . $name;
250
251
			if( class_exists( $classname ) === false ) {
252
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Class "%1$s" not found', $classname ) );
253
			}
254
255
			$client = new $classname( $client, $this->context );
256
257
			\Aimeos\MW\Common\Base::checkClass( '\\Aimeos\\Admin\\JQAdm\\Common\\Decorator\\Iface', $client );
258
		}
259
260
		return $client;
261
	}
262
263
264
	/**
265
	 * Adds the decorators to the client object
266
	 *
267
	 * @param \Aimeos\Admin\JQAdm\Iface $client Admin object
268
	 * @param string $path Admin string in lower case, e.g. "catalog/detail/basic"
269
	 * @return \Aimeos\Admin\JQAdm\Iface Admin object
270
	 */
271
	protected function addClientDecorators( \Aimeos\Admin\JQAdm\Iface $client, string $path ) : \Aimeos\Admin\JQAdm\Iface
272
	{
273
		if( !is_string( $path ) || $path === '' ) {
0 ignored issues
show
introduced by
The condition is_string($path) is always true.
Loading history...
274
			throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Invalid domain "%1$s"', $path ) );
275
		}
276
277
		$localClass = str_replace( ' ', '\\', ucwords( str_replace( '/', ' ', $path ) ) );
278
		$config = $this->context->getConfig();
279
280
		$decorators = $config->get( 'admin/jqadm/common/decorators/default', [] );
281
		$excludes = $config->get( 'admin/jqadm/' . $path . '/decorators/excludes', [] );
282
283
		foreach( $decorators as $key => $name )
284
		{
285
			if( in_array( $name, $excludes ) ) {
286
				unset( $decorators[$key] );
287
			}
288
		}
289
290
		$classprefix = '\\Aimeos\\Admin\\JQAdm\\Common\\Decorator\\';
291
		$client = $this->addDecorators( $client, $decorators, $classprefix );
292
293
		$classprefix = '\\Aimeos\\Admin\\JQAdm\\Common\\Decorator\\';
294
		$decorators = $config->get( 'admin/jqadm/' . $path . '/decorators/global', [] );
295
		$client = $this->addDecorators( $client, $decorators, $classprefix );
296
297
		$classprefix = '\\Aimeos\\Admin\\JQAdm\\' . $localClass . '\\Decorator\\';
298
		$decorators = $config->get( 'admin/jqadm/' . $path . '/decorators/local', [] );
299
		$client = $this->addDecorators( $client, $decorators, $classprefix );
300
301
		return $client;
302
	}
303
304
305
	/**
306
	 * Returns the sub-client given by its name.
307
	 *
308
	 * @param string $path Name of the sub-part in lower case (can contain a path like catalog/filter/tree)
309
	 * @param string|null $name Name of the implementation, will be from configuration (or Default) if null
310
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-part object
311
	 */
312
	protected function createSubClient( string $path, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
313
	{
314
		$path = strtolower( $path );
315
316
		if( $name === null ) {
317
			$name = $this->context->getConfig()->get( 'admin/jqadm/' . $path . '/name', 'Standard' );
318
		}
319
320
		if( empty( $name ) || ctype_alnum( $name ) === false ) {
321
			throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Invalid characters in client name "%1$s"', $name ) );
322
		}
323
324
		$subnames = str_replace( ' ', '\\', ucwords( str_replace( '/', ' ', $path ) ) );
325
326
		$classname = '\\Aimeos\\Admin\\JQAdm\\' . $subnames . '\\' . $name;
327
328
		if( class_exists( $classname ) === false ) {
329
			throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Class "%1$s" not available', $classname ) );
330
		}
331
332
		$object = new $classname( $this->context );
333
334
		\Aimeos\MW\Common\Base::checkClass( '\\Aimeos\\Admin\\JQAdm\\Iface', $object );
335
336
		$object = $this->addClientDecorators( $object, $path );
337
		$object->setAimeos( $this->aimeos );
0 ignored issues
show
Bug introduced by
The method setAimeos() does not exist on Aimeos\Admin\JQAdm\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\Admin\JQAdm\Common\Admin\Factory\Iface or Aimeos\Admin\JQAdm\Common\Decorator\Iface. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

337
		$object->/** @scrutinizer ignore-call */ 
338
           setAimeos( $this->aimeos );
Loading history...
338
		$object->setView( $this->view );
339
340
		return $object;
341
	}
342
343
344
	/**
345
	 * Returns the value for the given key in the array
346
	 *
347
	 * @param array $values Multi-dimensional associative list of key/value pairs
348
	 * @param string $key Parameter key like "name" or "list/test" for associative arrays
349
	 * @param mixed $default Returned value if no one for key is available
350
	 * @return mixed Value from the array or default value if not present in array
351
	 */
352
	protected function getValue( array $values, $key, $default = null )
353
	{
354
		foreach( explode( '/', trim( $key, '/' ) ) as $part )
355
		{
356
			if( isset( $values[$part] ) ) {
357
				$values = $values[$part];
358
			} else {
359
				return $default;
360
			}
361
		}
362
363
		return $values;
364
	}
365
366
367
	/**
368
	 * Returns the known client parameters and their values
369
	 *
370
	 * @param array $names List of parameter names
371
	 * @return array Associative list of parameters names as key and their values
372
	 */
373
	protected function getClientParams( $names = ['id', 'resource', 'site', 'lang'] ) : array
374
	{
375
		$list = [];
376
377
		foreach( $names as $name )
378
		{
379
			if( ( $val = $this->view->param( $name ) ) !== null ) {
380
				$list[$name] = $val;
381
			}
382
		}
383
384
		return $list;
385
	}
386
387
388
	/**
389
	 * Returns the context object.
390
	 *
391
	 * @return \Aimeos\MShop\Context\Item\Iface Context object
392
	 */
393
	protected function getContext() : \Aimeos\MShop\Context\Item\Iface
394
	{
395
		return $this->context;
396
	}
397
398
399
	/**
400
	 * Returns the list of sub-client names configured for the client.
401
	 *
402
	 * @return array List of admin client names
403
	 */
404
	abstract protected function getSubClientNames() : array;
405
406
407
	/**
408
	 * Returns the available class names without namespace that are stored in the given path
409
	 *
410
	 * @param string $relpath Path relative to the include paths
411
	 * @param string[] $excludes List of file names to execlude
412
	 * @return string[] List of available class names
413
	 */
414
	protected function getClassNames( string $relpath, array $excludes = ['Base.php', 'Iface.php', 'Example.php', 'None.php'] ) : array
415
	{
416
		$list = [];
417
418
		foreach( $this->getAimeos()->getIncludePaths() as $path )
419
		{
420
			$path .= DIRECTORY_SEPARATOR . $relpath;
421
422
			if( is_dir( $path ) )
423
			{
424
				foreach( new \DirectoryIterator( $path ) as $entry )
425
				{
426
					if( $entry->isFile() && !in_array( $entry->getFileName(), $excludes ) ) {
427
						$list[] = pathinfo( $entry->getFileName(), PATHINFO_FILENAME );
428
					}
429
				}
430
			}
431
		}
432
433
		sort( $list );
434
		return $list;
435
	}
436
437
438
	/**
439
	 * Returns the array of criteria conditions based on the given parameters
440
	 *
441
	 * @param array $params List of criteria data with condition, sorting and paging
442
	 * @return array Multi-dimensional associative list of criteria conditions
443
	 */
444
	protected function getCriteriaConditions( array $params ) : array
445
	{
446
		$expr = [];
447
448
		if( isset( $params['key'] ) )
449
		{
450
			foreach( (array) $params['key'] as $idx => $key )
451
			{
452
				if( $key != '' && isset( $params['op'][$idx] ) && $params['op'][$idx] != ''
453
					&& isset( $params['val'][$idx] ) && $params['val'][$idx] != ''
454
				) {
455
					$expr[] = [$params['op'][$idx] => [$key => $params['val'][$idx]]];
456
				}
457
			}
458
459
			if( !empty( $expr ) ) {
460
				$expr = ['&&' => $expr];
461
			}
462
		}
463
464
		return $expr;
465
	}
466
467
468
	/**
469
	 * Returns the array of criteria sortations based on the given parameters
470
	 *
471
	 * @param array $params List of criteria data with condition, sorting and paging
472
	 * @return array Associative list of criteria sortations
473
	 */
474
	protected function getCriteriaSortations( array $params ) : array
475
	{
476
		$sortation = [];
477
478
		foreach( $params as $sort )
479
		{
480
			if( $sort[0] === '-' ) {
481
				$sortation[substr( $sort, 1 )] = '-';
482
			} else {
483
				$sortation[$sort] = '+';
484
			}
485
		}
486
487
		return $sortation;
488
	}
489
490
491
	/**
492
	 * Returns the configured sub-clients or the ones named in the default parameter if none are configured.
493
	 *
494
	 * @return array List of sub-clients implementing \Aimeos\Admin\JQAdm\Iface ordered in the same way as the names
495
	 */
496
	protected function getSubClients() : array
497
	{
498
		if( !isset( $this->subclients ) )
499
		{
500
			$this->subclients = [];
501
502
			foreach( $this->getSubClientNames() as $name ) {
503
				$this->subclients[] = $this->getSubClient( $name );
504
			}
505
		}
506
507
		return $this->subclients;
508
	}
509
510
511
	/**
512
	 * Initializes the criteria object based on the given parameter
513
	 *
514
	 * @param \Aimeos\MW\Criteria\Iface $criteria Criteria object
515
	 * @param array $params List of criteria data with condition, sorting and paging
516
	 * @return \Aimeos\MW\Criteria\Iface Initialized criteria object
517
	 */
518
	protected function initCriteria( \Aimeos\MW\Criteria\Iface $criteria, array $params ) : \Aimeos\MW\Criteria\Iface
519
	{
520
		if( isset( $params['filter'] ) ) {
521
			$criteria = $this->initCriteriaConditions( $criteria, (array) $params['filter'] );
522
		}
523
524
		if( isset( $params['sort'] ) ) {
525
			$criteria = $this->initCriteriaSortations( $criteria, (array) $params['sort'] );
526
		}
527
528
		$page = [];
529
		if( isset( $params['page'] ) ) {
530
			$page = (array) $params['page'];
531
		}
532
533
		return $this->initCriteriaSlice( $criteria, $page );
534
	}
535
536
537
	/**
538
	 * Writes the exception details to the log
539
	 *
540
	 * @param \Exception $e Exception object
541
	 * @return \Aimeos\Admin\JQAdm\Iface Reference to this object for fluent calls
542
	 */
543
	protected function log( \Exception $e ) : Iface
544
	{
545
		$logger = $this->context->getLogger();
546
		$logger->log( $e->getMessage(), \Aimeos\MW\Logger\Base::ERR, 'admin/jqadm' );
547
		$logger->log( $e->getTraceAsString(), \Aimeos\MW\Logger\Base::ERR, 'admin/jqadm' );
548
549
		return $this;
550
	}
551
552
553
	/**
554
	 * Returns a map of code/item pairs
555
	 *
556
	 * @param \Aimeos\MShop\Common\Item\Type\Iface[] $items Associative list of type items
557
	 * @return \Aimeos\MShop\Common\Item\Type\Iface[] Associative list of codes as keys and items as values
558
	 */
559
	protected function map( \Aimeos\Map $items ) : array
560
	{
561
		$list = [];
562
563
		foreach( $items as $item ) {
564
			$list[$item->getCode()] = $item;
565
		}
566
567
		return $list;
568
	}
569
570
571
	/**
572
	 * Adds a redirect to the response for the next action
573
	 *
574
	 * @param \Aimeos\MW\View\Iface $view View object
575
	 * @param string|null $action Next action
576
	 * @param string $resource Resource name
577
	 * @param string|null $id ID of the next resource item
578
	 * @param string|null $act Current action name
579
	 * @return \Aimeos\MW\View\Iface Modified view object
580
	 */
581
	protected function nextAction( \Aimeos\MW\View\Iface $view, ?string $action, string $resource,
582
		string $id = null, string $act = null ) : \Aimeos\MW\View\Iface
583
	{
584
		$params = $this->getClientParams();
585
		$params['resource'] = $resource;
586
		unset( $params['id'] );
587
588
		if( $act ) {
589
			$params['act'] = $act;
590
		}
591
592
		switch( $action )
593
		{
594
			case 'search':
595
				$target = $view->config( 'admin/jqadm/url/search/target' );
596
				$cntl = $view->config( 'admin/jqadm/url/search/controller', 'Jqadm' );
597
				$action = $view->config( 'admin/jqadm/url/search/action', 'search' );
598
				$conf = $view->config( 'admin/jqadm/url/search/config', [] );
599
				$url = $view->url( $target, $cntl, $action, $params, [], $conf );
600
				break;
601
			case 'create':
602
				$params['parentid'] = $id;
603
				$target = $view->config( 'admin/jqadm/url/create/target' );
604
				$cntl = $view->config( 'admin/jqadm/url/create/controller', 'Jqadm' );
605
				$action = $view->config( 'admin/jqadm/url/create/action', 'create' );
606
				$conf = $view->config( 'admin/jqadm/url/create/config', [] );
607
				$url = $view->url( $target, $cntl, $action, $params, [], $conf );
608
				break;
609
			case 'copy':
610
				$target = $view->config( 'admin/jqadm/url/copy/target' );
611
				$cntl = $view->config( 'admin/jqadm/url/copy/controller', 'Jqadm' );
612
				$action = $view->config( 'admin/jqadm/url/copy/action', 'copy' );
613
				$conf = $view->config( 'admin/jqadm/url/copy/config', [] );
614
				$url = $view->url( $target, $cntl, $action, ['id' => $id] + $params, [], $conf );
615
				break;
616
			default:
617
				$target = $view->config( 'admin/jqadm/url/get/target' );
618
				$cntl = $view->config( 'admin/jqadm/url/get/controller', 'Jqadm' );
619
				$action = $view->config( 'admin/jqadm/url/get/action', 'get' );
620
				$conf = $view->config( 'admin/jqadm/url/get/config', [] );
621
				$url = $view->url( $target, $cntl, $action, ['id' => $id] + $params, [], $conf );
622
		}
623
624
		$view->response()->withStatus( 302 );
625
		$view->response()->withHeader( 'Location', $url );
626
		$view->response()->withHeader( 'Cache-Control', 'no-store' );
627
628
		return $view;
629
	}
630
631
632
	/**
633
	 * Writes the exception details to the log
634
	 *
635
	 * @param \Exception $e Exception object
636
	 * @param string $method Method it's called from
637
	 * @return \Aimeos\Admin\JQAdm\Iface Reference to this object for fluent calls
638
	 */
639
	protected function report( \Exception $e, string $method ) : Iface
640
	{
641
		$view = $this->view;
642
		$i18n = $this->context->getI18n();
643
644
		if( $e instanceof \Aimeos\Admin\JQAdm\Exception )
645
		{
646
			$view->errors = array_merge( $view->get( 'errors', [] ), [$e->getMessage()] );
647
			return $this;
648
		}
649
		elseif( $e instanceof \Aimeos\MShop\Exception )
650
		{
651
			$view->errors = array_merge( $view->get( 'errors', [] ), [$i18n->dt( 'mshop', $e->getMessage() )] );
652
			return $this;
653
		}
654
655
		switch( $method )
656
		{
657
			case 'save': $msg = $i18n->dt( 'admin', 'Error saving data' );
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
Unused Code introduced by
The assignment to $msg is dead and can be removed.
Loading history...
658
			case 'delete': $msg = $i18n->dt( 'admin', 'Error deleting data' );
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
659
			default: $msg = $i18n->dt( 'admin', 'Error retrieving data' );
660
		}
661
662
		$view->errors = array_merge( $view->get( 'errors', [] ), [$msg] );
663
664
		return $this->log( $e );
665
	}
666
667
668
	/**
669
	 * Stores and returns the parameters used for searching items
670
	 *
671
	 * @param array $params GET/POST parameter set
672
	 * @param string $name Name of the panel/subpanel
673
	 * @return array Associative list of parameters for searching items
674
	 */
675
	protected function storeSearchParams( array $params, string $name ) : array
676
	{
677
		$key = 'aimeos/admin/jqadm/' . $name;
678
		$session = $this->getContext()->getSession();
679
680
		if( isset( $params['filter'] ) ) {
681
			$session->set( $key . '/filter', $params['filter'] );
682
		}
683
684
		if( isset( $params['sort'] ) ) {
685
			$session->set( $key . '/sort', $params['sort'] );
686
		}
687
688
		if( isset( $params['page'] ) ) {
689
			$session->set( $key . '/page', $params['page'] );
690
		}
691
692
		if( isset( $params['fields'] ) ) {
693
			$session->set( $key . '/fields', $params['fields'] );
694
		}
695
696
		return [
697
			'fields' => $session->get( $key . '/fields' ),
698
			'filter' => $session->get( $key . '/filter' ),
699
			'page' => $session->get( $key . '/page' ),
700
			'sort' => $session->get( $key . '/sort' ),
701
		];
702
	}
703
704
705
	/**
706
	 * Initializes the criteria object with conditions based on the given parameter
707
	 *
708
	 * @param \Aimeos\MW\Criteria\Iface $criteria Criteria object
709
	 * @param array $params List of criteria data with condition, sorting and paging
710
	 * @return \Aimeos\MW\Criteria\Iface Initialized criteria object
711
	 */
712
	private function initCriteriaConditions( \Aimeos\MW\Criteria\Iface $criteria, array $params ) : \Aimeos\MW\Criteria\Iface
713
	{
714
		if( ( $cond = $criteria->toConditions( $this->getCriteriaConditions( $params ) ) ) !== null ) {
715
			return $criteria->setConditions( $criteria->combine( '&&', [$cond, $criteria->getConditions()] ) );
716
		}
717
718
		return $criteria;
719
	}
720
721
722
	/**
723
	 * Initializes the criteria object with the slice based on the given parameter.
724
	 *
725
	 * @param \Aimeos\MW\Criteria\Iface $criteria Criteria object
726
	 * @param array $params List of criteria data with condition, sorting and paging
727
	 * @return \Aimeos\MW\Criteria\Iface Initialized criteria object
728
	 */
729
	private function initCriteriaSlice( \Aimeos\MW\Criteria\Iface $criteria, array $params ) : \Aimeos\MW\Criteria\Iface
730
	{
731
		$start = ( isset( $params['offset'] ) ? $params['offset'] : 0 );
732
		$size = ( isset( $params['limit'] ) ? $params['limit'] : 25 );
733
734
		return $criteria->setSlice( $start, $size );
735
	}
736
737
738
	/**
739
	 * Initializes the criteria object with sortations based on the given parameter
740
	 *
741
	 * @param \Aimeos\MW\Criteria\Iface $criteria Criteria object
742
	 * @param array $params List of criteria data with condition, sorting and paging
743
	 * @return \Aimeos\MW\Criteria\Iface Initialized criteria object
744
	 */
745
	private function initCriteriaSortations( \Aimeos\MW\Criteria\Iface $criteria, array $params ) : \Aimeos\MW\Criteria\Iface
746
	{
747
		return $criteria->setSortations( $criteria->toSortations( $this->getCriteriaSortations( $params ) ) );
748
	}
749
}
750