Completed
Push — master ( 709bda...74f662 )
by Aimeos
02:08
created

Standard::sort()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 9.2248
c 0
b 0
f 0
cc 5
nc 6
nop 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\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 $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, 'locale' );
39
		$this->filter = $this->manager->createSearch( true );
40
41
		$this->conditions[] = $this->filter->compare( '==', 'locale.siteid', $context->getLocale()->getSitePath() );
42
		$this->conditions[] = $this->filter->getConditions();
43
	}
44
45
46
	/**
47
	 * Clones objects in controller and resets values
48
	 */
49
	public function __clone()
50
	{
51
		$this->filter = clone $this->filter;
52
	}
53
54
55
	/**
56
	 * Adds generic condition for filtering
57
	 *
58
	 * @param string $operator Comparison operator, e.g. "==", "!=", "<", "<=", ">=", ">", "=~", "~="
59
	 * @param string $key Search key defined by the locale manager, e.g. "locale.status"
60
	 * @param array|string $value Value or list of values to compare to
61
	 * @return \Aimeos\Controller\Frontend\Locale\Iface Locale controller for fluent interface
62
	 * @since 2019.04
63
	 */
64
	public function compare( $operator, $key, $value )
65
	{
66
		$this->conditions[] = $this->filter->compare( $operator, $key, $value );
67
		return $this;
68
	}
69
70
71
	/**
72
	 * Returns the locale for the given locale ID
73
	 *
74
	 * @param string $id Unique locale ID
75
	 * @return \Aimeos\MShop\Locale\Item\Iface Locale item including the referenced domains items
76
	 * @since 2019.04
77
	 */
78
	public function get( $id )
79
	{
80
		return $this->manager->getItem( $id, [], true );
81
	}
82
83
84
	/**
85
	 * Parses the given array and adds the conditions to the list of conditions
86
	 *
87
	 * @param array $conditions List of conditions, e.g. ['>' => ['locale.interval' => 'P0Y1M0W0D']]
88
	 * @return \Aimeos\Controller\Frontend\Locale\Iface Locale controller for fluent interface
89
	 * @since 2019.04
90
	 */
91
	public function parse( array $conditions )
92
	{
93
		$this->conditions[] = $this->filter->toConditions( $conditions );
94
		return $this;
95
	}
96
97
98
	/**
99
	 * Returns the locales filtered by the previously assigned conditions
100
	 *
101
	 * @param integer &$total Parameter where the total number of found locales will be stored in
102
	 * @return \Aimeos\MShop\Locale\Item\Iface[] Ordered list of locale items
103
	 * @since 2019.04
104
	 */
105
	public function search( &$total = null )
106
	{
107
		$this->filter->setConditions( $this->filter->combine( '&&', $this->conditions ) );
108
		return $this->manager->searchItems( $this->filter, [], $total );
109
	}
110
111
112
	/**
113
	 * Sets the start value and the number of returned locale items for slicing the list of found locale items
114
	 *
115
	 * @param integer $start Start value of the first locale item in the list
116
	 * @param integer $limit Number of returned locale items
117
	 * @return \Aimeos\Controller\Frontend\Locale\Iface Locale controller for fluent interface
118
	 * @since 2019.04
119
	 */
120
	public function slice( $start, $limit )
121
	{
122
		$this->filter->setSlice( $start, $limit );
123
		return $this;
124
	}
125
126
127
	/**
128
	 * Sets the sorting of the result list
129
	 *
130
	 * @param string|null $key Sorting key of the result list like "position", null for no sorting
131
	 * @return \Aimeos\Controller\Frontend\Locale\Iface Locale controller for fluent interface
132
	 * @since 2019.04
133
	 */
134
	public function sort( $key = null )
135
	{
136
		$direction = '+';
137
138
		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...
139
		{
140
			$key = substr( $key, 1 );
141
			$direction = '-';
142
		}
143
144
		switch( $key )
145
		{
146
			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...
147
				$this->filter->setSortations( [] );
148
				break;
149
			case 'position':
150
				$this->filter->setSortations( [$this->filter->sort( $direction, 'locale.position' )] );
151
				break;
152
			default:
153
				$this->filter->setSortations( [$this->filter->sort( $direction, $key )] );
154
		}
155
156
		return $this;
157
	}
158
}
159