Passed
Push — master ( 0b8510...665276 )
by Aimeos
05:27
created

Standard::__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
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2021-2024
6
 * @package MShop
7
 * @subpackage Rule
8
 */
9
10
11
namespace Aimeos\MShop\Rule\Item;
12
13
14
/**
15
 * Default implementation of rule items.
16
 *
17
 * @package MShop
18
 * @subpackage Rule
19
 */
20
class Standard
21
	extends \Aimeos\MShop\Common\Item\Base
22
	implements \Aimeos\MShop\Rule\Item\Iface
23
{
24
	use \Aimeos\MShop\Common\Item\Config\Traits;
25
26
27
	/**
28
	 * Returns the type of the rule.
29
	 *
30
	 * @return string Rule type
31
	 */
32
	public function getType() : string
33
	{
34
		return $this->get( 'rule.type', 'catalog' );
35
	}
36
37
38
	/**
39
	 * Sets the new type of the rule item.
40
	 *
41
	 * @param string $type New rule type
42
	 * @return \Aimeos\MShop\Rule\Item\Iface Rule item for chaining method calls
43
	 */
44
	public function setType( string $type ) : \Aimeos\MShop\Common\Item\Iface
45
	{
46
		return $this->set( 'rule.type', $this->checkCode( $type ) );
47
	}
48
49
50
	/**
51
	 * Returns the provider of the rule.
52
	 *
53
	 * @return string Rule provider which is the short rule class name
54
	 */
55
	public function getProvider() : string
56
	{
57
		return $this->get( 'rule.provider', '' );
58
	}
59
60
61
	/**
62
	 * Sets the new provider of the rule item which is the short
63
	 * name of the rule class name.
64
	 *
65
	 * @param string $provider Rule provider, esp. short rule class name
66
	 * @return \Aimeos\MShop\Rule\Item\Iface Rule item for chaining method calls
67
	 */
68
	public function setProvider( string $provider ) : \Aimeos\MShop\Rule\Item\Iface
69
	{
70
		if( preg_match( '/^[A-Za-z0-9]+(,[A-Za-z0-9]+)*$/', $provider ) !== 1 ) {
71
			throw new \Aimeos\MShop\Rule\Exception( sprintf( 'Invalid provider name "%1$s"', $provider ) );
72
		}
73
74
		return $this->set( 'rule.provider', $provider );
75
	}
76
77
78
	/**
79
	 * Returns the name of the rule item.
80
	 *
81
	 * @return string Label of the rule item
82
	 */
83
	public function getLabel() : string
84
	{
85
		return $this->get( 'rule.label', '' );
86
	}
87
88
89
	/**
90
	 * Sets the new label of the rule item.
91
	 *
92
	 * @param string $label New label of the rule item
93
	 * @return \Aimeos\MShop\Rule\Item\Iface Rule item for chaining method calls
94
	 */
95
	public function setLabel( string $label ) : \Aimeos\MShop\Rule\Item\Iface
96
	{
97
		return $this->set( 'rule.label', $label );
98
	}
99
100
101
	/**
102
	 * Returns the configuration of the rule item.
103
	 *
104
	 * @return array Custom configuration values
105
	 */
106
	public function getConfig() : array
107
	{
108
		return $this->get( 'rule.config', [] );
109
	}
110
111
112
	/**
113
	 * Sets the new configuration for the rule item.
114
	 *
115
	 * @param array $config Custom configuration values
116
	 * @return \Aimeos\MShop\Rule\Item\Iface Rule item for chaining method calls
117
	 */
118
	public function setConfig( array $config ) : \Aimeos\MShop\Common\Item\Iface
119
	{
120
		return $this->set( 'rule.config', $config );
121
	}
122
123
124
	/**
125
	 * Returns the starting point of time, in which the rule is available.
126
	 *
127
	 * @return string|null ISO date in YYYY-MM-DD hh:mm:ss format
128
	 */
129
	public function getDateStart() : ?string
130
	{
131
		$value = $this->get( 'rule.datestart' );
132
		return $value ? substr( $value, 0, 19 ) : null;
133
	}
134
135
136
	/**
137
	 * Sets a new starting point of time, in which the rule is available.
138
	 *
139
	 * @param string|null $date New ISO date in YYYY-MM-DD hh:mm:ss format
140
	 * @return \Aimeos\MShop\Product\Item\Iface Product item for chaining method calls
141
	 */
142
	public function setDateStart( ?string $date ) : \Aimeos\MShop\Common\Item\Iface
143
	{
144
		return $this->set( 'rule.datestart', $this->checkDateFormat( $date ) );
145
	}
146
147
148
	/**
149
	 * Returns the ending point of time, in which the rule is available.
150
	 *
151
	 * @return string|null ISO date in YYYY-MM-DD hh:mm:ss format
152
	 */
153
	public function getDateEnd() : ?string
154
	{
155
		$value = $this->get( 'rule.dateend' );
156
		return $value ? substr( $value, 0, 19 ) : null;
157
	}
158
159
160
	/**
161
	 * Sets a new ending point of time, in which the rule is available.
162
	 *
163
	 * @param string|null $date New ISO date in YYYY-MM-DD hh:mm:ss format
164
	 * @return \Aimeos\MShop\Product\Item\Iface Product item for chaining method calls
165
	 */
166
	public function setDateEnd( ?string $date ) : \Aimeos\MShop\Common\Item\Iface
167
	{
168
		return $this->set( 'rule.dateend', $this->checkDateFormat( $date ) );
169
	}
170
171
172
	/**
173
	 * Returns the position of the rule item.
174
	 *
175
	 * @return int Position of the item
176
	 */
177
	public function getPosition() : int
178
	{
179
		return $this->get( 'rule.position', 0 );
180
	}
181
182
183
	/**
184
	 * Sets the new position of the rule item.
185
	 *
186
	 * @param int $position Position of the item
187
	 * @return \Aimeos\MShop\Rule\Item\Iface Rule item for chaining method calls
188
	 */
189
	public function setPosition( int $position ) : \Aimeos\MShop\Common\Item\Iface
190
	{
191
		return $this->set( 'rule.position', $position );
192
	}
193
194
195
	/**
196
	 * Returns the status of the rule item.
197
	 *
198
	 * @return int Status of the item
199
	 */
200
	public function getStatus() : int
201
	{
202
		return $this->get( 'rule.status', 1 );
203
	}
204
205
206
	/**
207
	 * Sets the new status of the rule item.
208
	 *
209
	 * @param int $status Status of the item
210
	 * @return \Aimeos\MShop\Rule\Item\Iface Rule item for chaining method calls
211
	 */
212
	public function setStatus( int $status ) : \Aimeos\MShop\Common\Item\Iface
213
	{
214
		return $this->set( 'rule.status', $status );
215
	}
216
217
218
	/**
219
	 * Tests if the item is available based on status, time, language and currency
220
	 *
221
	 * @return bool True if available, false if not
222
	 */
223
	public function isAvailable() : bool
224
	{
225
		return parent::isAvailable() && $this->getStatus() > 0;
226
	}
227
228
229
	/**
230
	 * Sets the item values from the given array and removes that entries from the list
231
	 *
232
	 * @param array &$list Associative list of item keys and their values
233
	 * @param bool True to set private properties too, false for public only
234
	 * @return \Aimeos\MShop\Rule\Item\Iface Rule item for chaining method calls
235
	 */
236
	public function fromArray( array &$list, bool $private = false ) : \Aimeos\MShop\Common\Item\Iface
237
	{
238
		$item = parent::fromArray( $list, $private );
239
240
		foreach( $list as $key => $value )
241
		{
242
			switch( $key )
243
			{
244
				case 'rule.type': $item = $item->setType( $value ); break;
245
				case 'rule.label': $item = $item->setLabel( $value ); break;
246
				case 'rule.provider': $item = $item->setProvider( $value ); break;
247
				case 'rule.status': $item = $item->setStatus( (int) $value ); break;
248
				case 'rule.config': $item = $item->setConfig( (array) $value ); break;
249
				case 'rule.position': $item = $item->setPosition( (int) $value ); break;
250
				case 'rule.datestart': $item = $item->setDateStart( $value ); break;
251
				case 'rule.dateend': $item = $item->setDateEnd( $value ); break;
252
				default: continue 2;
253
			}
254
255
			unset( $list[$key] );
256
		}
257
258
		return $item;
259
	}
260
261
262
	/**
263
	 * Returns the item values as array.
264
	 *
265
	 * @param bool True to return private properties, false for public only
266
	 * @return array Associative list of item properties and their values
267
	 */
268
	public function toArray( bool $private = false ) : array
269
	{
270
		$list = parent::toArray( $private );
271
272
		$list['rule.type'] = $this->getType();
273
		$list['rule.label'] = $this->getLabel();
274
		$list['rule.provider'] = $this->getProvider();
275
		$list['rule.status'] = $this->getStatus();
276
		$list['rule.config'] = $this->getConfig();
277
		$list['rule.position'] = $this->getPosition();
278
		$list['rule.datestart'] = $this->getDateStart();
279
		$list['rule.dateend'] = $this->getDateEnd();
280
281
		return $list;
282
	}
283
284
}
285