Passed
Push — master ( 6ee4ae...fdac5c )
by Aimeos
03:28
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), 2017-2020
6
 * @package Controller
7
 * @subpackage Frontend
8
 */
9
10
11
namespace Aimeos\Controller\Frontend\Stock;
12
13
14
/**
15
 * Default implementation of the stock 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 $filter;
26
	private $manager;
27
28
29
	/**
30
	 * Common initialization for controller classes
31
	 *
32
	 * @param \Aimeos\MShop\Context\Item\Iface $context Common MShop context object
33
	 */
34
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
35
	{
36
		parent::__construct( $context );
37
38
		$this->manager = \Aimeos\MShop::create( $context, 'stock' );
39
		$this->filter = $this->manager->filter( true );
40
		$this->conditions[] = $this->filter->getConditions();
41
	}
42
43
44
	/**
45
	 * Clones objects in controller and resets values
46
	 */
47
	public function __clone()
48
	{
49
		$this->filter = clone $this->filter;
50
	}
51
52
53
	/**
54
	 * Adds the IDs of the products for filtering
55
	 *
56
	 * @param array|string $ids Codes of the products
57
	 * @return \Aimeos\Controller\Frontend\Stock\Iface Stock controller for fluent interface
58
	 * @since 2021.01
59
	 */
60
	public function product( $ids ) : Iface
61
	{
62
		if( !empty( $ids ) ) {
63
			$this->conditions[] = $this->filter->compare( '==', 'stock.productid', $ids );
64
		}
65
66
		return $this;
67
	}
68
69
70
	/**
71
	 * Adds generic condition for filtering
72
	 *
73
	 * @param string $operator Comparison operator, e.g. "==", "!=", "<", "<=", ">=", ">", "=~", "~="
74
	 * @param string $key Search key defined by the stock manager, e.g. "stock.dateback"
75
	 * @param array|string $value Value or list of values to compare to
76
	 * @return \Aimeos\Controller\Frontend\Stock\Iface Stock controller for fluent interface
77
	 * @since 2019.04
78
	 */
79
	public function compare( string $operator, string $key, $value ) : Iface
80
	{
81
		$this->conditions[] = $this->filter->compare( $operator, $key, $value );
82
		return $this;
83
	}
84
85
86
	/**
87
	 * Returns the stock item for the given stock ID
88
	 *
89
	 * @param string $id Unique stock ID
90
	 * @return \Aimeos\MShop\Stock\Item\Iface Stock item
91
	 * @since 2019.04
92
	 */
93
	public function get( string $id ) : \Aimeos\MShop\Stock\Item\Iface
94
	{
95
		return $this->manager->get( $id, [], true );
96
	}
97
98
99
	/**
100
	 * Parses the given array and adds the conditions to the list of conditions
101
	 *
102
	 * @param array $conditions List of conditions, e.g. ['>' => ['stock.dateback' => '2000-01-01 00:00:00']]
103
	 * @return \Aimeos\Controller\Frontend\Stock\Iface Stock controller for fluent interface
104
	 * @since 2019.04
105
	 */
106
	public function parse( array $conditions ) : Iface
107
	{
108
		if( ( $cond = $this->filter->toConditions( $conditions ) ) !== null ) {
109
			$this->conditions[] = $cond;
110
		}
111
112
		return $this;
113
	}
114
115
116
	/**
117
	 * Returns the stock items filtered by the previously assigned conditions
118
	 *
119
	 * @param int &$total Parameter where the total number of found stock items will be stored in
120
	 * @return \Aimeos\Map Ordered list of stock items implementing \Aimeos\MShop\Stock\Item\Iface
121
	 * @since 2019.04
122
	 */
123
	public function search( int &$total = null ) : \Aimeos\Map
124
	{
125
		$this->filter->setConditions( $this->filter->combine( '&&', $this->conditions ) );
126
		return $this->manager->search( $this->filter, [], $total );
127
	}
128
129
130
	/**
131
	 * Sets the start value and the number of returned stock items for slicing the list of found stock items
132
	 *
133
	 * @param integer $start Start value of the first stock item in the list
134
	 * @param integer $limit Number of returned stock items
135
	 * @return \Aimeos\Controller\Frontend\Stock\Iface Stock controller for fluent interface
136
	 * @since 2019.04
137
	 */
138
	public function slice( int $start, int $limit ) : Iface
139
	{
140
		$this->filter->setSlice( $start, $limit );
141
		return $this;
142
	}
143
144
145
	/**
146
	 * Sets the sorting of the result list
147
	 *
148
	 * @param string|null $key Sorting of the result list like "stock.type", null for no sorting
149
	 * @return \Aimeos\Controller\Frontend\Stock\Iface Stock controller for fluent interface
150
	 * @since 2019.04
151
	 */
152
	public function sort( string $key = null ) : Iface
153
	{
154
		$sort = [];
155
		$list = ( $key ? explode( ',', $key ) : [] );
156
157
		foreach( $list as $sortkey )
158
		{
159
			$direction = ( $sortkey[0] === '-' ? '-' : '+' );
160
			$sortkey = ltrim( $sortkey, '+-' );
161
162
			switch( $sortkey )
163
			{
164
				case 'stock':
165
					$sort[] = $this->filter->sort( $direction, 'stock.type' );
166
					$sort[] = $this->filter->sort( $direction, 'stock.stocklevel' );
167
					break;
168
				default:
169
					$sort[] = $this->filter->sort( $direction, $sortkey );
170
				}
171
		}
172
173
		$this->filter->setSortations( $sort );
174
		return $this;
175
	}
176
177
178
	/**
179
	 * Adds stock types for filtering
180
	 *
181
	 * @param array|string $types Stock type codes
182
	 * @return \Aimeos\Controller\Frontend\Stock\Iface Stock controller for fluent interface
183
	 * @since 2019.04
184
	 */
185
	public function type( $types ) : Iface
186
	{
187
		if( !empty( $types ) ) {
188
			$this->conditions[] = $this->filter->compare( '==', 'stock.type', $types );
189
		}
190
191
		return $this;
192
	}
193
}
194