Passed
Push — master ( cb2ca1...d63df3 )
by Aimeos
08:16
created

Base::__construct()   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 Metaways Infosystems GmbH, 2012
6
 * @copyright Aimeos (aimeos.org), 2015-2022
7
 * @package Controller
8
 * @subpackage Frontend
9
 */
10
11
12
namespace Aimeos\Controller\Frontend;
13
14
15
/**
16
 * Common methods for frontend controller classes.
17
 *
18
 * @package Controller
19
 * @subpackage Frontend
20
 */
21
abstract class Base
22
	implements \Aimeos\Macro\Iface
23
{
24
	use \Aimeos\Macro\Macroable;
25
26
27
	private $object;
28
	private $context;
29
	private $cond = [];
30
	private $sort = [];
31
32
33
	/**
34
	 * Common initialization for controller classes.
35
	 *
36
	 * @param \Aimeos\MShop\ContextIface $context Common MShop context object
37
	 */
38
	public function __construct( \Aimeos\MShop\ContextIface $context )
39
	{
40
		$this->context = $context;
41
	}
42
43
44
	/**
45
	 * Clones objects in controller
46
	 */
47
	public function __clone()
48
	{
49
		foreach( $this->cond as $key => $cond )
50
		{
51
			if( is_object( $cond ) ) {
52
				$this->cond[$key] = clone $cond;
53
			}
54
		}
55
56
		foreach( $this->sort as $key => $sort )
57
		{
58
			if( is_object( $sort ) ) {
59
				$this->sort[$key] = clone $sort;
60
			}
61
		}
62
	}
63
64
65
	/**
66
	 * Adds the given compare, combine or sort expression to the list of expressions
67
	 *
68
	 * @param \Aimeos\Base\Criteria\Expression\Iface|null $expr Compare, combine or sort expression
69
	 * @return \Aimeos\Controller\Frontend\Iface Controller object for chaining method calls
70
	 */
71
	public function addExpression( \Aimeos\Base\Criteria\Expression\Iface $expr = null ) : Iface
72
	{
73
		if( $expr instanceof \Aimeos\Base\Criteria\Expression\Sort\Iface ) {
74
			$this->sort[] = $expr;
75
		} elseif( $expr ) {
76
			$this->cond[] = $expr;
77
		}
78
79
		return $this;
80
	}
81
82
83
	/**
84
	 * Returns the compare and combine expressions added by addExpression()
85
	 *
86
	 * @return array List of compare and combine expressions
87
	 */
88
	public function getConditions() : array
89
	{
90
		return $this->cond;
91
	}
92
93
94
	/**
95
	 * Returns the compare and combine expressions added by addExpression()
96
	 *
97
	 * @return array List of sort expressions
98
	 */
99
	public function getSortations() : array
100
	{
101
		return $this->sort;
102
	}
103
104
105
	/**
106
	 * Returns the context object.
107
	 *
108
	 * @return \Aimeos\MShop\ContextIface Context object implementing \Aimeos\MShop\ContextIface
109
	 */
110
	protected function context() : \Aimeos\MShop\ContextIface
111
	{
112
		return $this->context;
113
	}
114
115
116
	/**
117
	 * Returns the outmost decorator of the decorator stack
118
	 *
119
	 * @return \Aimeos\Controller\Frontend\Iface Outmost decorator object
120
	 */
121
	protected function object() : Iface
122
	{
123
		if( $this->object !== null ) {
124
			return $this->object;
125
		}
126
127
		return $this;
128
	}
129
130
131
	/**
132
	 * Injects the reference of the outmost object
133
	 *
134
	 * @param \Aimeos\Controller\Frontend\Iface $object Reference to the outmost controller or decorator
135
	 * @return \Aimeos\Controller\Frontend\Iface Controller object for chaining method calls
136
	 */
137
	public function setObject( \Aimeos\Controller\Frontend\Iface $object ) : Iface
138
	{
139
		$this->object = $object;
140
		return $this;
141
	}
142
143
144
	/**
145
	 * Splits search keys by comma
146
	 *
147
	 * @param string|null $keys Comma separated string of search keys
148
	 * @return array List of search keys
149
	 */
150
	protected function splitKeys( ?string $keys ) : array
151
	{
152
		$list = [];
153
154
		if( preg_match_all( '/(?P<key>[^(,]+(\(("([^"]|\")*")?[^)]*\))?),?/', (string) $keys, $list ) !== false ) {
155
			return $list['key'] ?? [];
156
		}
157
158
		return [];
159
	}
160
}
161