Completed
Push — master ( 94eb6c...a336e2 )
by Aimeos
02:13
created

Standard::compare()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
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 ) : Iface
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( string $operator, string $key, $value ) : Iface
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( string $domain ) : Iface
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( string $code, string $type ) : \Aimeos\MShop\Attribute\Item\Iface
111
	{
112
		return $this->manager->findItem( $code, $this->domains, $this->domain, $type, true );
113
	}
114
115
116
	/**
117
	 * Creates a search function string for the given name and parameters
118
	 *
119
	 * @param string $name Name of the search function without parenthesis, e.g. "attribute:prop"
120
	 * @param array $params List of parameters for the search function with numeric keys starting at 0
121
	 * @param string Search function string that can be used in compare()
0 ignored issues
show
Bug introduced by
The type Aimeos\Controller\Frontend\Attribute\Search was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
122
	 */
123
	public function function( string $name, array $params ) : string
124
	{
125
		return $this->filter->createFunction( $name, $params );
126
	}
127
128
129
	/**
130
	 * Returns the attribute for the given attribute ID
131
	 *
132
	 * @param string $id Unique attribute ID
133
	 * @return \Aimeos\MShop\Attribute\Item\Iface Attribute item including the referenced domains items
134
	 * @since 2019.04
135
	 */
136
	public function get( string $id ) : \Aimeos\MShop\Attribute\Item\Iface
137
	{
138
		return $this->manager->getItem( $id, $this->domains, true );
139
	}
140
141
142
	/**
143
	 * Adds a filter to return only items containing a reference to the given ID
144
	 *
145
	 * @param string $domain Domain name of the referenced item, e.g. "price"
146
	 * @param string|null $type Type code of the reference, e.g. "default" or null for all types
147
	 * @param string|null $refId ID of the referenced item of the given domain or null for all references
148
	 * @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface
149
	 * @since 2019.04
150
	 */
151
	public function has( string $domain, string $type = null, string $refId = null ) : Iface
152
	{
153
		$params = [$domain];
154
		!$type ?: $params[] = $type;
155
		!$refId ?: $params[] = $refId;
156
157
		$func = $this->filter->createFunction( 'attribute:has', $params );
158
		$this->conditions[] = $this->filter->compare( '!=', $func, null );
159
		return $this;
160
	}
161
162
163
	/**
164
	 * Parses the given array and adds the conditions to the list of conditions
165
	 *
166
	 * @param array $conditions List of conditions, e.g. ['&&' => [['>' => ['attribute.status' => 0]], ['==' => ['attribute.type' => 'color']]]]
167
	 * @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface
168
	 * @since 2019.04
169
	 */
170
	public function parse( array $conditions ) : Iface
171
	{
172
		if( ( $cond = $this->filter->toConditions( $conditions ) ) !== null ) {
173
			$this->conditions[] = $cond;
174
		}
175
176
		return $this;
177
	}
178
179
180
	/**
181
	 * Adds a filter to return only items containing the property
182
	 *
183
	 * @param string $type Type code of the property, e.g. "htmlcolor"
184
	 * @param string|null $value Exact value of the property
185
	 * @param string|null $langId ISO country code (en or en_US) or null if not language specific
186
	 * @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface
187
	 * @since 2019.04
188
	 */
189
	public function property( string $type, string $value = null, string $langId = null ) : Iface
190
	{
191
		$func = $this->filter->createFunction( 'attribute:prop', [$type, $langId, $value] );
192
		$this->conditions[] = $this->filter->compare( '!=', $func, null );
193
		return $this;
194
	}
195
196
197
	/**
198
	 * Returns the attributes filtered by the previously assigned conditions
199
	 *
200
	 * @param int &$total Parameter where the total number of found attributes will be stored in
201
	 * @return \Aimeos\MShop\Attribute\Item\Iface[] Ordered list of attribute items
202
	 * @since 2019.04
203
	 */
204
	public function search( int &$total = null )
205
	{
206
		$expr = array_merge( $this->conditions, [$this->filter->compare( '==', 'attribute.domain', $this->domain )] );
207
		$this->filter->setConditions( $this->filter->combine( '&&', $expr ) );
208
209
		return $this->manager->searchItems( $this->filter, $this->domains, $total );
210
	}
211
212
213
	/**
214
	 * Sets the start value and the number of returned attributes for slicing the list of found attributes
215
	 *
216
	 * @param int $start Start value of the first attribute in the list
217
	 * @param int $limit Number of returned attributes
218
	 * @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface
219
	 * @since 2019.04
220
	 */
221
	public function slice( int $start, int $limit ) : Iface
222
	{
223
		$this->filter->setSlice( $start, $limit );
224
		return $this;
225
	}
226
227
228
	/**
229
	 * Sets the sorting of the result list
230
	 *
231
	 * @param string|null $key Sorting of the result list like "position", null for no sorting
232
	 * @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface
233
	 * @since 2019.04
234
	 */
235
	public function sort( string $key = null ) : Iface
236
	{
237
		$sort = [];
238
		$list = ( $key ? explode( ',', $key ) : [] );
239
240
		foreach( $list as $sortkey )
241
		{
242
			$direction = ( $sortkey[0] === '-' ? '-' : '+' );
243
			$sortkey = ltrim( $sortkey, '+-' );
244
245
			switch( $sortkey )
246
			{
247
				case 'position':
248
					$sort[] = $this->filter->sort( $direction, 'attribute.type' );
249
					$sort[] = $this->filter->sort( $direction, 'attribute.position' );
250
					break;
251
				default:
252
					$sort[] = $this->filter->sort( $direction, $sortkey );
253
			}
254
		}
255
256
		$this->filter->setSortations( $sort );
257
		return $this;
258
	}
259
260
261
	/**
262
	 * Adds attribute types for filtering
263
	 *
264
	 * @param array|string $codes Attribute ID or list of IDs
265
	 * @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface
266
	 * @since 2019.04
267
	 */
268
	public function type( $codes ) : Iface
269
	{
270
		if( !empty( $codes ) ) {
271
			$this->conditions[] = $this->filter->compare( '==', 'attribute.type', $codes );
272
		}
273
274
		return $this;
275
	}
276
277
278
	/**
279
	 * Sets the referenced domains that will be fetched too when retrieving items
280
	 *
281
	 * @param array $domains Domain names of the referenced items that should be fetched too
282
	 * @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface
283
	 * @since 2019.04
284
	 */
285
	public function uses( array $domains ) : Iface
286
	{
287
		$this->domains = $domains;
288
		return $this;
289
	}
290
}
291