Passed
Push — master ( 216152...7bdf82 )
by Aimeos
02:00
created

Standard::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018
6
 * @package Controller
7
 * @subpackage Frontend
8
 */
9
10
11
namespace Aimeos\Controller\Frontend\Supplier;
12
13
14
/**
15
 * Default implementation of the supplier 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 $domains = [];
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, 'supplier' );
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 generic condition for filtering
56
	 *
57
	 * @param string $operator Comparison operator, e.g. "==", "!=", "<", "<=", ">=", ">", "=~", "~="
58
	 * @param string $key Search key defined by the supplier manager, e.g. "supplier.status"
59
	 * @param array|string $value Value or list of values to compare to
60
	 * @return \Aimeos\Controller\Frontend\Supplier\Iface Supplier controller for fluent interface
61
	 * @since 2019.04
62
	 */
63
	public function compare( $operator, $key, $value )
64
	{
65
		$this->conditions[] = $this->filter->compare( $operator, $key, $value );
66
		return $this;
67
	}
68
69
70
	/**
71
	 * Returns the supplier for the given supplier code
72
	 *
73
	 * @param string $code Unique supplier code
74
	 * @return \Aimeos\MShop\Supplier\Item\Iface Supplier item including the referenced domains items
75
	 * @since 2019.04
76
	 */
77
	public function find( $code )
78
	{
79
		return $this->manager->findItem( $code, $this->domains, null, null, true );
80
	}
81
82
83
	/**
84
	 * Returns the supplier for the given supplier ID
85
	 *
86
	 * @param string $id Unique supplier ID
87
	 * @return \Aimeos\MShop\Supplier\Item\Iface Supplier item including the referenced domains items
88
	 * @since 2019.04
89
	 */
90
	public function get( $id )
91
	{
92
		return $this->manager->getItem( $id, $this->domains, true );
93
	}
94
95
96
	/**
97
	 * Adds a filter to return only items containing a reference to the given ID
98
	 *
99
	 * @param string $domain Domain name of the referenced item, e.g. "product"
100
	 * @param string|null $type Type code of the reference, e.g. "default" or null for all types
101
	 * @param string|null $refId ID of the referenced item of the given domain or null for all references
102
	 * @return \Aimeos\Controller\Frontend\Supplier\Iface Supplier controller for fluent interface
103
	 * @since 2019.10
104
	 */
105
	public function has( $domain, $type = null, $refId = null )
106
	{
107
		$params = [$domain];
108
		!$type ?: $params[] = $type;
109
		!$refId ?: $params[] = $refId;
110
111
		$func = $this->filter->createFunction( 'supplier:has', $params );
112
		$this->conditions[] = $this->filter->compare( '!=', $func, null );
113
		return $this;
114
	}
115
116
117
	/**
118
	 * Parses the given array and adds the conditions to the list of conditions
119
	 *
120
	 * @param array $conditions List of conditions, e.g. ['=~' => ['supplier.label' => 'test']]
121
	 * @return \Aimeos\Controller\Frontend\Supplier\Iface Supplier controller for fluent interface
122
	 * @since 2019.04
123
	 */
124
	public function parse( array $conditions )
125
	{
126
		$this->conditions[] = $this->filter->toConditions( $conditions );
127
		return $this;
128
	}
129
130
131
	/**
132
	 * Returns the suppliers filtered by the previously assigned conditions
133
	 *
134
	 * @param integer &$total Parameter where the total number of found suppliers will be stored in
135
	 * @return \Aimeos\MShop\Supplier\Item\Iface[] Ordered list of supplier items
136
	 * @since 2019.04
137
	 */
138
	public function search( &$total = null )
139
	{
140
		$this->filter->setConditions( $this->filter->combine( '&&', $this->conditions ) );
141
		return $this->manager->searchItems( $this->filter, $this->domains, $total );
142
	}
143
144
145
	/**
146
	 * Sets the start value and the number of returned supplier items for slicing the list of found supplier items
147
	 *
148
	 * @param integer $start Start value of the first supplier item in the list
149
	 * @param integer $limit Number of returned supplier items
150
	 * @return \Aimeos\Controller\Frontend\Supplier\Iface Supplier controller for fluent interface
151
	 * @since 2019.04
152
	 */
153
	public function slice( $start, $limit )
154
	{
155
		$this->filter->setSlice( $start, $limit );
156
		return $this;
157
	}
158
159
160
	/**
161
	 * Sets the sorting of the result list
162
	 *
163
	 * @param string|null $key Sorting key of the result list like "supplier.label", null for no sorting
164
	 * @return \Aimeos\Controller\Frontend\Supplier\Iface Supplier controller for fluent interface
165
	 * @since 2019.04
166
	 */
167
	public function sort( $key = null )
168
	{
169
		$sort = [];
170
		$list = ( $key ? explode( ',', $key ) : [] );
171
172
		foreach( $list as $sortkey )
173
		{
174
			$direction = ( $sortkey[0] === '-' ? '-' : '+' );
175
			$sort[] = $this->filter->sort( $direction, ltrim( $sortkey, '+-' ) );
176
		}
177
178
		$this->filter->setSortations( $sort );
179
		return $this;
180
	}
181
182
183
	/**
184
	 * Sets the referenced domains that will be fetched too when retrieving items
185
	 *
186
	 * @param array $domains Domain names of the referenced items that should be fetched too
187
	 * @return \Aimeos\Controller\Frontend\Supplier\Iface Supplier controller for fluent interface
188
	 * @since 2019.04
189
	 */
190
	public function uses( array $domains )
191
	{
192
		$this->domains = $domains;
193
		return $this;
194
	}
195
}
196