Passed
Push — master ( c9dfc7...a4ce90 )
by Aimeos
01:38
created

Standard   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 256
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 23
eloc 59
dl 0
loc 256
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 3 1
A domain() 0 4 1
A uses() 0 4 1
A search() 0 6 1
A sort() 0 26 5
A slice() 0 4 1
A compare() 0 4 1
A has() 0 9 3
A parse() 0 4 1
A find() 0 3 1
A type() 0 7 2
A __clone() 0 3 1
A property() 0 5 1
A attribute() 0 7 2
A __construct() 0 7 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2018
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
	private $conditions = [];
25
	private $domain = 'product';
26
	private $domains = [];
27
	private $filter;
28
	private $manager;
29
30
31
	/**
32
	 * Common initialization for controller classes
33
	 *
34
	 * @param \Aimeos\MShop\Context\Item\Iface $context Common MShop context object
35
	 */
36
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
37
	{
38
		parent::__construct( $context );
39
40
		$this->manager = \Aimeos\MShop::create( $context, 'attribute' );
41
		$this->filter = $this->manager->createSearch( true );
42
		$this->conditions[] = $this->filter->getConditions();
43
	}
44
45
46
	/**
47
	 * Clones objects in controller and resets values
48
	 */
49
	public function __clone()
50
	{
51
		$this->filter = clone $this->filter;
52
	}
53
54
55
	/**
56
	 * Adds attribute IDs for filtering
57
	 *
58
	 * @param array|string $attrIds Attribute ID or list of IDs
59
	 * @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface
60
	 * @since 2019.04
61
	 */
62
	public function attribute( $attrIds )
63
	{
64
		if( !empty( $attrIds ) ) {
65
			$this->conditions[] = $this->filter->compare( '==', 'attribute.id', $attrIds );
66
		}
67
68
		return $this;
69
	}
70
71
72
	/**
73
	 * Adds generic condition for filtering attributes
74
	 *
75
	 * @param string $operator Comparison operator, e.g. "==", "!=", "<", "<=", ">=", ">", "=~", "~="
76
	 * @param string $key Search key defined by the attribute manager, e.g. "attribute.status"
77
	 * @param array|string $value Value or list of values to compare to
78
	 * @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface
79
	 * @since 2019.04
80
	 */
81
	public function compare( $operator, $key, $value )
82
	{
83
		$this->conditions[] = $this->filter->compare( $operator, $key, $value );
84
		return $this;
85
	}
86
87
88
	/**
89
	 * Adds the domain of the attributes for filtering
90
	 *
91
	 * @param string $domain Domain of the attributes
92
	 * @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface
93
	 * @since 2019.04
94
	 */
95
	public function domain( $domain )
96
	{
97
		$this->domain = $domain;
98
		return $this;
99
	}
100
101
102
	/**
103
	 * Returns the attribute for the given attribute code
104
	 *
105
	 * @param string $code Unique attribute code
106
	 * @param string $type Type assigned to the attribute
107
	 * @return \Aimeos\MShop\Attribute\Item\Iface Attribute item including the referenced domains items
108
	 * @since 2019.04
109
	 */
110
	public function find( $code, $type )
111
	{
112
		return $this->manager->findItem( $code, $this->domains, $this->domain, $type, true );
113
	}
114
115
116
	/**
117
	 * Returns the attribute for the given attribute ID
118
	 *
119
	 * @param string $id Unique attribute ID
120
	 * @return \Aimeos\MShop\Attribute\Item\Iface Attribute item including the referenced domains items
121
	 * @since 2019.04
122
	 */
123
	public function get( $id )
124
	{
125
		return $this->manager->getItem( $id, $this->domains, true );
126
	}
127
128
129
	/**
130
	 * Adds a filter to return only items containing a reference to the given ID
131
	 *
132
	 * @param string $domain Domain name of the referenced item, e.g. "price"
133
	 * @param string|null $type Type code of the reference, e.g. "default" or null for all types
134
	 * @param string|null $refId ID of the referenced item of the given domain or null for all references
135
	 * @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface
136
	 * @since 2019.04
137
	 */
138
	public function has( $domain, $type = null, $refId = null )
139
	{
140
		$params = [$domain];
141
		!$type ?: $params[] = $type;
142
		!$refId ?: $params[] = $refId;
143
144
		$func = $this->filter->createFunction( 'attribute:has', $params );
145
		$this->conditions[] = $this->filter->compare( '!=', $func, null );
146
		return $this;
147
	}
148
149
150
	/**
151
	 * Parses the given array and adds the conditions to the list of conditions
152
	 *
153
	 * @param array $conditions List of conditions, e.g. ['&&' => [['>' => ['attribute.status' => 0]], ['==' => ['attribute.type' => 'color']]]]
154
	 * @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface
155
	 * @since 2019.04
156
	 */
157
	public function parse( array $conditions )
158
	{
159
		$this->conditions[] = $this->filter->toConditions( $conditions );
160
		return $this;
161
	}
162
163
164
	/**
165
	 * Adds a filter to return only items containing the property
166
	 *
167
	 * @param string $type Type code of the property, e.g. "htmlcolor"
168
	 * @param string|null $value Exact value of the property
169
	 * @param string|null $langId ISO country code (en or en_US) or null if not language specific
170
	 * @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface
171
	 * @since 2019.04
172
	 */
173
	public function property( $type, $value = null, $langId = null )
174
	{
175
		$func = $this->filter->createFunction( 'attribute:prop', [$type, $langId, $value] );
176
		$this->conditions[] = $this->filter->compare( '!=', $func, null );
177
		return $this;
178
	}
179
180
181
	/**
182
	 * Returns the attributes filtered by the previously assigned conditions
183
	 *
184
	 * @param integer &$total Parameter where the total number of found attributes will be stored in
185
	 * @return \Aimeos\MShop\Attribute\Item\Iface[] Ordered list of attribute items
186
	 * @since 2019.04
187
	 */
188
	public function search( &$total = null )
189
	{
190
		$expr = array_merge( $this->conditions, [$this->filter->compare( '==', 'attribute.domain', $this->domain )] );
191
		$this->filter->setConditions( $this->filter->combine( '&&', $expr ) );
192
193
		return $this->manager->searchItems( $this->filter, $this->domains, $total );
194
	}
195
196
197
	/**
198
	 * Sets the start value and the number of returned attributes for slicing the list of found attributes
199
	 *
200
	 * @param integer $start Start value of the first attribute in the list
201
	 * @param integer $limit Number of returned attributes
202
	 * @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface
203
	 * @since 2019.04
204
	 */
205
	public function slice( $start, $limit )
206
	{
207
		$this->filter->setSlice( $start, $limit );
208
		return $this;
209
	}
210
211
212
	/**
213
	 * Sets the sorting of the result list
214
	 *
215
	 * @param string|null $key Sorting of the result list like "position", null for no sorting
216
	 * @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface
217
	 * @since 2019.04
218
	 */
219
	public function sort( $key = null )
220
	{
221
		$direction = '+';
222
223
		if( $key != null && $key[0] === '-' )
224
		{
225
			$key = substr( $key, 1 );
226
			$direction = '-';
227
		}
228
229
		switch( $key )
230
		{
231
			case null:
232
				$this->filter->setSortations( [] );
233
				break;
234
			case 'position':
235
				$this->filter->setSortations( [
236
					$this->filter->sort( $direction, 'attribute.type' ),
237
					$this->filter->sort( $direction, 'attribute.position' )
238
				] );
239
				break;
240
			default:
241
				$this->filter->setSortations( [$this->filter->sort( $direction, $key )] );
242
		}
243
244
		return $this;
245
	}
246
247
248
	/**
249
	 * Adds attribute types for filtering
250
	 *
251
	 * @param array|string $codes Attribute ID or list of IDs
252
	 * @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface
253
	 * @since 2019.04
254
	 */
255
	public function type( $codes )
256
	{
257
		if( !empty( $codes ) ) {
258
			$this->conditions[] = $this->filter->compare( '==', 'attribute.type', $codes );
259
		}
260
261
		return $this;
262
	}
263
264
265
	/**
266
	 * Sets the referenced domains that will be fetched too when retrieving items
267
	 *
268
	 * @param array $domains Domain names of the referenced items that should be fetched too
269
	 * @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface
270
	 * @since 2019.04
271
	 */
272
	public function uses( array $domains )
273
	{
274
		$this->domains = $domains;
275
		return $this;
276
	}
277
}
278