Passed
Push — master ( 9a0039...2f021f )
by Aimeos
07:58
created

Standard::slice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2021
6
 * @package Controller
7
 * @subpackage Frontend
8
 */
9
10
11
namespace Aimeos\Controller\Frontend\Locale;
12
13
14
/**
15
 * Default implementation of the locale 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 $filter;
25
	private $manager;
26
27
28
	/**
29
	 * Common initialization for controller classes
30
	 *
31
	 * @param \Aimeos\MShop\Context\Item\Iface $context Common MShop context object
32
	 */
33
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
34
	{
35
		parent::__construct( $context );
36
37
		$this->manager = \Aimeos\MShop::create( $context, 'locale' );
38
		$this->filter = $this->manager->filter( true );
39
40
		$this->addExpression( $this->filter->compare( '==', 'locale.siteid', $context->getLocale()->getSitePath() ) );
41
		$this->addExpression( $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 locale manager, e.g. "locale.status"
59
	 * @param array|string $value Value or list of values to compare to
60
	 * @return \Aimeos\Controller\Frontend\Locale\Iface Locale controller for fluent interface
61
	 * @since 2019.04
62
	 */
63
	public function compare( string $operator, string $key, $value ) : Iface
64
	{
65
		$this->addExpression( $this->filter->compare( $operator, $key, $value ) );
66
		return $this;
67
	}
68
69
70
	/**
71
	 * Returns the locale for the given locale ID
72
	 *
73
	 * @param string $id Unique locale ID
74
	 * @return \Aimeos\MShop\Locale\Item\Iface Locale item including the referenced domains items
75
	 * @since 2019.04
76
	 */
77
	public function get( string $id ) : \Aimeos\MShop\Locale\Item\Iface
78
	{
79
		return $this->manager->get( $id, [], true );
80
	}
81
82
83
	/**
84
	 * Parses the given array and adds the conditions to the list of conditions
85
	 *
86
	 * @param array $conditions List of conditions, e.g. ['>' => ['locale.languageid' => 'de']]
87
	 * @return \Aimeos\Controller\Frontend\Locale\Iface Locale controller for fluent interface
88
	 * @since 2019.04
89
	 */
90
	public function parse( array $conditions ) : Iface
91
	{
92
		if( ( $cond = $this->filter->parse( $conditions ) ) !== null ) {
93
			$this->addExpression( $cond );
94
		}
95
96
		return $this;
97
	}
98
99
100
	/**
101
	 * Returns the locales filtered by the previously assigned conditions
102
	 *
103
	 * @param int|null &$total Parameter where the total number of found locales will be stored in
104
	 * @return \Aimeos\Map Ordered list of locale items implementing \Aimeos\MShop\Locale\Item\Iface
105
	 * @since 2019.04
106
	 */
107
	public function search( int &$total = null ) : \Aimeos\Map
108
	{
109
		$this->filter->setConditions( $this->filter->and( $this->getConditions() ) );
110
		$this->filter->setSortations( $this->getSortations() );
111
112
		return $this->manager->search( $this->filter, [], $total );
113
	}
114
115
116
	/**
117
	 * Sets the start value and the number of returned locale items for slicing the list of found locale items
118
	 *
119
	 * @param int $start Start value of the first locale item in the list
120
	 * @param int $limit Number of returned locale items
121
	 * @return \Aimeos\Controller\Frontend\Locale\Iface Locale controller for fluent interface
122
	 * @since 2019.04
123
	 */
124
	public function slice( int $start, int $limit ) : Iface
125
	{
126
		$maxsize = $this->getContext()->config()->get( 'controller/frontend/common/max-size', 250 );
127
		$this->filter->slice( $start, min( $limit, $maxsize ) );
128
		return $this;
129
	}
130
131
132
	/**
133
	 * Sets the sorting of the result list
134
	 *
135
	 * @param string|null $key Sorting key of the result list like "position", null for no sorting
136
	 * @return \Aimeos\Controller\Frontend\Locale\Iface Locale controller for fluent interface
137
	 * @since 2019.04
138
	 */
139
	public function sort( string $key = null ) : Iface
140
	{
141
		$list = $this->splitKeys( $key );
142
143
		foreach( $list as $sortkey )
144
		{
145
			$direction = ( $sortkey[0] === '-' ? '-' : '+' );
146
			$sortkey = ltrim( $sortkey, '+-' );
147
148
			switch( $sortkey )
149
			{
150
				case 'position':
151
					$this->addExpression( $this->filter->sort( $direction, 'locale.position' ) );
152
					break;
153
				default:
154
				$this->addExpression( $this->filter->sort( $direction, $sortkey ) );
155
			}
156
		}
157
158
		return $this;
159
	}
160
161
162
	/**
163
	 * Returns the manager used by the controller
164
	 *
165
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object
166
	 */
167
	protected function getManager() : \Aimeos\MShop\Common\Manager\Iface
168
	{
169
		return $this->manager;
170
	}
171
}
172