Completed
Push — master ( 921503...9109fd )
by Aimeos
02:03
created

Standard::getItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 1
nc 1
nop 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A Standard::domain() 0 5 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 $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 ID
103
	 *
104
	 * @param string $id Unique attribute ID
105
	 * @param string[] $domains Domain names of items that are associated with the attributes and that should be fetched too
106
	 * @return \Aimeos\MShop\Attribute\Item\Iface Attribute item including the referenced domains items
107
	 * @since 2019.04
108
	 */
109
	public function get( $id, $domains = ['media', 'price', 'text'] )
110
	{
111
		return $this->manager->getItem( $id, $domains, true );
112
	}
113
114
115
	/**
116
	 * Returns the attribute for the given attribute code
117
	 *
118
	 * @param string $code Unique attribute code
119
	 * @param string[] $domains Domain names of items that are associated with the attributes and that should be fetched too
120
	 * @param string $type Type assigned to the attribute
121
	 * @return \Aimeos\MShop\Attribute\Item\Iface Attribute item including the referenced domains items
122
	 * @since 2019.04
123
	 */
124
	public function find( $code, $domains = ['media', 'price', 'text'], $type )
125
	{
126
		return $this->manager->findItem( $code, $domains, $this->domain, $type, true );
127
	}
128
129
130
	/**
131
	 * Parses the given array and adds the conditions to the list of conditions
132
	 *
133
	 * @param array $conditions List of conditions, e.g. ['&&' => [['>' => ['attribute.status' => 0]], ['==' => ['attribute.type' => 'color']]]]
134
	 * @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface
135
	 * @since 2019.04
136
	 */
137
	public function parse( array $conditions )
138
	{
139
		$this->conditions[] = $this->filter->toConditions( $conditions );
140
		return $this;
141
	}
142
143
144
	/**
145
	 * Returns the attributes filtered by the previously assigned conditions
146
	 *
147
	 * @param string[] $domains Domain names of items that are associated with the attributes and that should be fetched too
148
	 * @param integer &$total Parameter where the total number of found attributes will be stored in
149
	 * @return \Aimeos\MShop\Attribute\Item\Iface[] Ordered list of attribute items
150
	 * @since 2019.04
151
	 */
152
	public function search( $domains = ['media', 'price', 'text'], &$total = null )
153
	{
154
		$expr = array_merge( $this->conditions, [$this->filter->compare( '==', 'attribute.domain', $this->domain )] );
155
		$this->filter->setConditions( $this->filter->combine( '&&', $expr ) );
156
157
		return $this->manager->searchItems( $this->filter, $domains, $total );
158
	}
159
160
161
	/**
162
	 * Sets the start value and the number of returned attributes for slicing the list of found attributes
163
	 *
164
	 * @param integer $start Start value of the first attribute in the list
165
	 * @param integer $limit Number of returned attributes
166
	 * @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface
167
	 * @since 2019.04
168
	 */
169
	public function slice( $start, $limit )
170
	{
171
		$this->filter->setSlice( $start, $limit );
172
		return $this;
173
	}
174
175
176
	/**
177
	 * Sets the sorting of the attribute list
178
	 *
179
	 * @param string|null $key Sorting of the attribute list like "position" or "-position", null for no sortation
180
	 * @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface
181
	 * @since 2019.04
182
	 */
183
	public function sort( $key = null )
184
	{
185
		$direction = '+';
186
187
		if( $key != null && $key[0] === '-' )
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $key of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
188
		{
189
			$key = substr( $key, 1 );
190
			$direction = '-';
191
		}
192
193
		switch( $key )
194
		{
195
			case null:
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $key of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
196
				$this->filter->setSortations( [] );
197
				break;
198
			case 'position':
199
				$this->filter->setSortations( [
200
					$this->filter->sort( $direction, 'attribute.type' ),
201
					$this->filter->sort( $direction, 'attribute.position' )
202
				] );
203
				break;
204
			default:
205
				$this->filter->setSortations( [$this->filter->sort( $direction, $key )] );
206
		}
207
208
		return $this;
209
	}
210
211
212
	/**
213
	 * Adds attribute types for filtering
214
	 *
215
	 * @param array|string $codes Attribute ID or list of IDs
216
	 * @return \Aimeos\Controller\Frontend\Attribute\Iface Attribute controller for fluent interface
217
	 * @since 2019.04
218
	 */
219
	public function type( $codes )
220
	{
221
		if( !empty( $codes ) ) {
222
			$this->conditions[] = $this->filter->compare( '==', 'attribute.type', $codes );
223
		}
224
225
		return $this;
226
	}
227
}
228