Passed
Push — master ( 3ac2eb...9ad972 )
by Aimeos
05:39
created

Standard::isAvailable()   B

Complexity

Conditions 7
Paths 11

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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