Standard   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 393
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 25
eloc 62
dl 0
loc 393
rs 10
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 3 1
A domain() 0 4 1
A compare() 0 4 1
A has() 0 9 3
A parse() 0 7 2
A find() 0 3 1
A function() 0 3 1
A property() 0 5 1
A attribute() 0 7 2
A __construct() 0 6 1
A __clone() 0 4 1
A uses() 0 4 1
A search() 0 11 1
A sort() 0 21 4
A slice() 0 5 1
A type() 0 7 2
A getManager() 0 3 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2025
6
 * @package Controller
7
 * @subpackage Frontend
8
 */
9
10
11
namespace Aimeos\Controller\Frontend\Attribute;
12
13
14
/**
15
 * Default implementation of the attribute frontend controller
16
 *
17
 * @package Controller
18
 * @subpackage Frontend
19
 */
20
class Standard
21
	extends \Aimeos\Controller\Frontend\Base
22
	implements Iface, \Aimeos\Controller\Frontend\Common\Iface
23
{
24
	/** controller/frontend/attribute/name
25
	 * Class name of the used attribute frontend controller implementation
26
	 *
27
	 * Each default frontend controller can be replace by an alternative imlementation.
28
	 * To use this implementation, you have to set the last part of the class
29
	 * name as configuration value so the controller factory knows which class it
30
	 * has to instantiate.
31
	 *
32
	 * For example, if the name of the default class is
33
	 *
34
	 *  \Aimeos\Controller\Frontend\Attribute\Standard
35
	 *
36
	 * and you want to replace it with your own version named
37
	 *
38
	 *  \Aimeos\Controller\Frontend\Attribute\Myattribute
39
	 *
40
	 * then you have to set the this configuration option:
41
	 *
42
	 *  controller/jobs/frontend/attribute/name = Myattribute
43
	 *
44
	 * The value is the last part of your own class name and it's case sensitive,
45
	 * so take care that the configuration value is exactly named like the last
46
	 * part of the class name.
47
	 *
48
	 * The allowed characters of the class name are A-Z, a-z and 0-9. No other
49
	 * characters are possible! You should always start the last part of the class
50
	 * name with an upper case character and continue only with lower case characters
51
	 * or numbers. Avoid chamel case names like "MyAttribute"!
52
	 *
53
	 * @param string Last part of the class name
54
	 * @since 2014.03
55
	 * @category Developer
56
	 */
57
58
	/** controller/frontend/attribute/decorators/excludes
59
	 * Excludes decorators added by the "common" option from the attribute frontend controllers
60
	 *
61
	 * Decorators extend the functionality of a class by adding new aspects
62
	 * (e.g. log what is currently done), executing the methods of the underlying
63
	 * class only in certain conditions (e.g. only for logged in users) or
64
	 * modify what is returned to the caller.
65
	 *
66
	 * This option allows you to remove a decorator added via
67
	 * "controller/frontend/common/decorators/default" before they are wrapped
68
	 * around the frontend controller.
69
	 *
70
	 *  controller/frontend/attribute/decorators/excludes = array( 'decorator1' )
71
	 *
72
	 * This would remove the decorator named "decorator1" from the list of
73
	 * common decorators ("\Aimeos\Controller\Frontend\Common\Decorator\*") added via
74
	 * "controller/frontend/common/decorators/default" for the attribute frontend controller.
75
	 *
76
	 * @param array List of decorator names
77
	 * @since 2014.03
78
	 * @category Developer
79
	 * @see controller/frontend/common/decorators/default
80
	 * @see controller/frontend/attribute/decorators/global
81
	 * @see controller/frontend/attribute/decorators/local
82
	 */
83
84
	/** controller/frontend/attribute/decorators/global
85
	 * Adds a list of globally available decorators only to the attribute frontend controllers
86
	 *
87
	 * Decorators extend the functionality of a class by adding new aspects
88
	 * (e.g. log what is currently done), executing the methods of the underlying
89
	 * class only in certain conditions (e.g. only for logged in users) or
90
	 * modify what is returned to the caller.
91
	 *
92
	 * This option allows you to wrap global decorators
93
	 * ("\Aimeos\Controller\Frontend\Common\Decorator\*") around the frontend controller.
94
	 *
95
	 *  controller/frontend/attribute/decorators/global = array( 'decorator1' )
96
	 *
97
	 * This would add the decorator named "decorator1" defined by
98
	 * "\Aimeos\Controller\Frontend\Common\Decorator\Decorator1" only to the frontend controller.
99
	 *
100
	 * @param array List of decorator names
101
	 * @since 2014.03
102
	 * @category Developer
103
	 * @see controller/frontend/common/decorators/default
104
	 * @see controller/frontend/attribute/decorators/excludes
105
	 * @see controller/frontend/attribute/decorators/local
106
	 */
107
108
	/** controller/frontend/attribute/decorators/local
109
	 * Adds a list of local decorators only to the attribute frontend controllers
110
	 *
111
	 * Decorators extend the functionality of a class by adding new aspects
112
	 * (e.g. log what is currently done), executing the methods of the underlying
113
	 * class only in certain conditions (e.g. only for logged in users) or
114
	 * modify what is returned to the caller.
115
	 *
116
	 * This option allows you to wrap local decorators
117
	 * ("\Aimeos\Controller\Frontend\Attribute\Decorator\*") around the frontend controller.
118
	 *
119
	 *  controller/frontend/attribute/decorators/local = array( 'decorator2' )
120
	 *
121
	 * This would add the decorator named "decorator2" defined by
122
	 * "\Aimeos\Controller\Frontend\Attribute\Decorator\Decorator2" only to the frontend
123
	 * controller.
124
	 *
125
	 * @param array List of decorator names
126
	 * @since 2014.03
127
	 * @category Developer
128
	 * @see controller/frontend/common/decorators/default
129
	 * @see controller/frontend/attribute/decorators/excludes
130
	 * @see controller/frontend/attribute/decorators/global
131
	 */
132
133
134
	private array $domains = [];
135
	private string $domain = 'product';
136
	private \Aimeos\Base\Criteria\Iface $filter;
137
	private \Aimeos\MShop\Common\Manager\Iface $manager;
138
139
140
	/**
141
	 * Common initialization for controller classes
142
	 *
143
	 * @param \Aimeos\MShop\ContextIface $context Common MShop context object
144
	 */
145
	public function __construct( \Aimeos\MShop\ContextIface $context )
146
	{
147
		parent::__construct( $context );
148
149
		$this->manager = \Aimeos\MShop::create( $context, 'attribute' );
150
		$this->filter = $this->manager->filter( true );
151
	}
152
153
154
	/**
155
	 * Clones objects in controller
156
	 */
157
	public function __clone()
158
	{
159
		$this->filter = clone $this->filter;
160
		parent::__clone();
161
	}
162
163
164
	/**
165
	 * Adds attribute IDs for filtering
166
	 *
167
	 * @param array|string $attrIds Attribute ID or list of IDs
168
	 * @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface
169
	 * @since 2019.04
170
	 */
171
	public function attribute( $attrIds ) : Iface
172
	{
173
		if( !empty( $attrIds ) ) {
174
			$this->addExpression( $this->filter->compare( '==', 'attribute.id', $attrIds ) );
175
		}
176
177
		return $this;
178
	}
179
180
181
	/**
182
	 * Adds generic condition for filtering attributes
183
	 *
184
	 * @param string $operator Comparison operator, e.g. "==", "!=", "<", "<=", ">=", ">", "=~", "~="
185
	 * @param string $key Search key defined by the attribute manager, e.g. "attribute.status"
186
	 * @param array|string $value Value or list of values to compare to
187
	 * @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface
188
	 * @since 2019.04
189
	 */
190
	public function compare( string $operator, string $key, $value ) : Iface
191
	{
192
		$this->addExpression( $this->filter->compare( $operator, $key, $value ) );
193
		return $this;
194
	}
195
196
197
	/**
198
	 * Adds the domain of the attributes for filtering
199
	 *
200
	 * @param string $domain Domain of the attributes
201
	 * @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface
202
	 * @since 2019.04
203
	 */
204
	public function domain( string $domain ) : Iface
205
	{
206
		$this->domain = $domain;
207
		return $this;
208
	}
209
210
211
	/**
212
	 * Returns the attribute for the given attribute code
213
	 *
214
	 * @param string $code Unique attribute code
215
	 * @param string $type Type assigned to the attribute
216
	 * @return \Aimeos\MShop\Attribute\Item\Iface Attribute item including the referenced domains items
217
	 * @since 2019.04
218
	 */
219
	public function find( string $code, string $type ) : \Aimeos\MShop\Attribute\Item\Iface
220
	{
221
		return $this->manager->find( $code, $this->domains, $this->domain, $type, null );
222
	}
223
224
225
	/**
226
	 * Creates a search function string for the given name and parameters
227
	 *
228
	 * @param string $name Name of the search function without parenthesis, e.g. "attribute:prop"
229
	 * @param array $params List of parameters for the search function with numeric keys starting at 0
230
	 * @return string Search function string that can be used in compare()
231
	 */
232
	public function function( string $name, array $params ) : string
233
	{
234
		return $this->filter->make( $name, $params );
235
	}
236
237
238
	/**
239
	 * Returns the attribute for the given attribute ID
240
	 *
241
	 * @param string $id Unique attribute ID
242
	 * @return \Aimeos\MShop\Attribute\Item\Iface Attribute item including the referenced domains items
243
	 * @since 2019.04
244
	 */
245
	public function get( string $id ) : \Aimeos\MShop\Attribute\Item\Iface
246
	{
247
		return $this->manager->get( $id, $this->domains, null );
248
	}
249
250
251
	/**
252
	 * Adds a filter to return only items containing a reference to the given ID
253
	 *
254
	 * @param string $domain Domain name of the referenced item, e.g. "price"
255
	 * @param string|null $type Type code of the reference, e.g. "default" or null for all types
256
	 * @param string|null $refId ID of the referenced item of the given domain or null for all references
257
	 * @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface
258
	 * @since 2019.04
259
	 */
260
	public function has( string $domain, ?string $type = null, ?string $refId = null ) : Iface
261
	{
262
		$params = [$domain];
263
		!$type ?: $params[] = $type;
264
		!$refId ?: $params[] = $refId;
265
266
		$func = $this->filter->make( 'attribute:has', $params );
267
		$this->addExpression( $this->filter->compare( '!=', $func, null ) );
268
		return $this;
269
	}
270
271
272
	/**
273
	 * Parses the given array and adds the conditions to the list of conditions
274
	 *
275
	 * @param array $conditions List of conditions, e.g. ['&&' => [['>' => ['attribute.status' => 0]], ['==' => ['attribute.type' => 'color']]]]
276
	 * @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface
277
	 * @since 2019.04
278
	 */
279
	public function parse( array $conditions ) : Iface
280
	{
281
		if( ( $cond = $this->filter->parse( $conditions ) ) !== null ) {
282
			$this->addExpression( $cond );
283
		}
284
285
		return $this;
286
	}
287
288
289
	/**
290
	 * Adds a filter to return only items containing the property
291
	 *
292
	 * @param string $type Type code of the property, e.g. "htmlcolor"
293
	 * @param string|null $value Exact value of the property
294
	 * @param string|null $langId ISO country code (en or en_US) or null if not language specific
295
	 * @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface
296
	 * @since 2019.04
297
	 */
298
	public function property( string $type, ?string $value = null, ?string $langId = null ) : Iface
299
	{
300
		$func = $this->filter->make( 'attribute:prop', [$type, $langId, $value] );
301
		$this->addExpression( $this->filter->compare( '!=', $func, null ) );
302
		return $this;
303
	}
304
305
306
	/**
307
	 * Returns the attributes filtered by the previously assigned conditions
308
	 *
309
	 * @param int &$total Parameter where the total number of found attributes will be stored in
310
	 * @return \Aimeos\Map Ordered list of attribute items implementing \Aimeos\MShop\Attribute\Item\Iface
311
	 * @since 2019.04
312
	 */
313
	public function search( ?int &$total = null ) : \Aimeos\Map
314
	{
315
		$filter = clone $this->filter;
316
317
		$this->addExpression( $this->filter->compare( '==', 'attribute.domain', $this->domain ) );
318
		$this->addExpression( $this->filter->getConditions() );
319
320
		$filter->add( $this->filter->and( $this->getConditions() ) );
321
		$filter->setSortations( $this->getSortations() );
322
323
		return $this->manager->search( $filter, $this->domains, $total );
324
	}
325
326
327
	/**
328
	 * Sets the start value and the number of returned attributes for slicing the list of found attributes
329
	 *
330
	 * @param int $start Start value of the first attribute in the list
331
	 * @param int $limit Number of returned attributes
332
	 * @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface
333
	 * @since 2019.04
334
	 */
335
	public function slice( int $start, int $limit ) : Iface
336
	{
337
		$maxsize = $this->context()->config()->get( 'controller/frontend/common/max-size', 500 );
338
		$this->filter->slice( $start, min( $limit, $maxsize ) );
339
		return $this;
340
	}
341
342
343
	/**
344
	 * Sets the sorting of the result list
345
	 *
346
	 * @param string|null $key Sorting of the result list like "position", null for no sorting
347
	 * @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface
348
	 * @since 2019.04
349
	 */
350
	public function sort( ?string $key = null ) : Iface
351
	{
352
		$list = $this->splitKeys( $key );
353
354
		foreach( $list as $sortkey )
355
		{
356
			$direction = ( $sortkey[0] === '-' ? '-' : '+' );
357
			$sortkey = ltrim( $sortkey, '+-' );
358
359
			switch( $sortkey )
360
			{
361
				case 'position':
362
					$this->addExpression( $this->filter->sort( $direction, 'attribute.type' ) );
363
					$this->addExpression( $this->filter->sort( $direction, 'attribute.position' ) );
364
					break;
365
				default:
366
				$this->addExpression( $this->filter->sort( $direction, $sortkey ) );
367
			}
368
		}
369
370
		return $this;
371
	}
372
373
374
	/**
375
	 * Adds attribute types for filtering
376
	 *
377
	 * @param array|string $codes Attribute ID or list of IDs
378
	 * @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface
379
	 * @since 2019.04
380
	 */
381
	public function type( $codes ) : Iface
382
	{
383
		if( !empty( $codes ) ) {
384
			$this->addExpression( $this->filter->compare( '==', 'attribute.type', $codes ) );
385
		}
386
387
		return $this;
388
	}
389
390
391
	/**
392
	 * Sets the referenced domains that will be fetched too when retrieving items
393
	 *
394
	 * @param array $domains Domain names of the referenced items that should be fetched too
395
	 * @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface
396
	 * @since 2019.04
397
	 */
398
	public function uses( array $domains ) : Iface
399
	{
400
		$this->domains = $domains;
401
		return $this;
402
	}
403
404
405
	/**
406
	 * Returns the manager used by the controller
407
	 *
408
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object
409
	 */
410
	protected function getManager() : \Aimeos\MShop\Common\Manager\Iface
411
	{
412
		return $this->manager;
413
	}
414
}
415