Passed
Push — master ( 6cf50b...92e939 )
by Aimeos
01:45
created

Standard::has()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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