Passed
Push — master ( 892c73...5dcb5e )
by Aimeos
04:29
created

Standard::fromArray()   C

Complexity

Conditions 12
Paths 11

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 15
nc 11
nop 2
dl 0
loc 24
rs 6.9666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2018
7
 * @package MShop
8
 * @subpackage Common
9
 */
10
11
12
namespace Aimeos\MShop\Common\Item\Lists;
13
14
15
/**
16
 * Default implementation of the list item.
17
 *
18
 * @package MShop
19
 * @subpackage Common
20
 */
21
class Standard
22
	extends \Aimeos\MShop\Common\Item\Base
23
	implements \Aimeos\MShop\Common\Item\Lists\Iface
24
{
25
	use \Aimeos\MShop\Common\Item\Config\Traits;
26
27
28
	private $prefix;
29
	private $values;
30
	private $refItem;
31
32
33
	/**
34
	 * Initializes the list item object.
35
	 *
36
	 * @param string $prefix Property prefix when converting to array
37
	 * @param array $values Associative list of key/value pairs of the list item
38
	 */
39
	public function __construct( $prefix, array $values = [] )
40
	{
41
		parent::__construct( $prefix, $values );
42
43
		$this->prefix = (string) $prefix;
44
		$this->values = $values;
45
	}
46
47
48
49
50
	/**
51
	 * Returns the parent ID of the common list item,
52
	 * like the unique ID of a product or a tree node.
53
	 *
54
	 * @return string|null Parent ID of the common list item
55
	 */
56
	public function getParentId()
57
	{
58
		if( isset( $this->values[$this->prefix . 'parentid'] ) ) {
59
			return (string) $this->values[$this->prefix . 'parentid'];
60
		}
61
62
		return null;
63
	}
64
65
66
	/**
67
	 * Sets the parent ID of the common list item,
68
	 * like the unique ID of a product or a tree node.
69
	 *
70
	 * @param string $parentid New parent ID of the common list item
71
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls
72
	 */
73
	public function setParentId( $parentid )
74
	{
75
		if( (string) $parentid !== $this->getParentId() )
76
		{
77
			$this->values[$this->prefix . 'parentid'] = (int) $parentid;
78
			$this->setModified();
79
		}
80
81
		return $this;
82
	}
83
84
85
	/**
86
	 * Returns the domain of the common list item, e.g. text or media.
87
	 *
88
	 * @return string Domain of the common list item
89
	 */
90
	public function getDomain()
91
	{
92
		if( isset( $this->values[$this->prefix . 'domain'] ) ) {
93
			return (string) $this->values[$this->prefix . 'domain'];
94
		}
95
96
		return '';
97
	}
98
99
100
	/**
101
	 * Sets the new domain of the common list item, e.g. text od media.
102
	 *
103
	 * @param string $domain New domain of the common list item
104
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls
105
	 */
106
	public function setDomain( $domain )
107
	{
108
		if( (string) $domain !== $this->getDomain() )
109
		{
110
			$this->values[$this->prefix . 'domain'] = (string) $domain;
111
			$this->setModified();
112
		}
113
114
		return $this;
115
	}
116
117
118
	/**
119
	 * Returns the reference id of the common list item, like the unique id
120
	 * of a text item or a media item.
121
	 *
122
	 * @return string Reference id of the common list item
123
	 */
124
	public function getRefId()
125
	{
126
		if( isset( $this->values[$this->prefix . 'refid'] ) ) {
127
			return (string) $this->values[$this->prefix . 'refid'];
128
		}
129
130
		return '';
131
	}
132
133
134
	/**
135
	 * Sets the new reference id of the common list item, like the unique id
136
	 * of a text item or a media item.
137
	 *
138
	 * @param string $refid New reference id of the common list item
139
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls
140
	 */
141
	public function setRefId( $refid )
142
	{
143
		if( (string) $refid !== $this->getRefId() )
144
		{
145
			$this->values[$this->prefix . 'refid'] = (string) $refid;
146
			$this->setModified();
147
		}
148
149
		return $this;
150
	}
151
152
153
	/**
154
	 * Returns the start date of the common list item (YYYY-MM-DD hh:mm:ss).
155
	 *
156
	 * @return string|null Start date of the common list item (YYYY-MM-DD hh:mm:ss)
157
	 */
158
	public function getDateStart()
159
	{
160
		if( isset( $this->values[$this->prefix . 'datestart'] ) ) {
161
			return (string) $this->values[$this->prefix . 'datestart'];
162
		}
163
164
		return null;
165
	}
166
167
168
	/**
169
	 * Sets the new start date of the common list item (YYYY-MM-DD hh:mm:ss).
170
	 *
171
	 * @param string $date New start date of the common list item (YYYY-MM-DD hh:mm:ss)
172
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls
173
	 */
174
	public function setDateStart( $date )
175
	{
176
		if( (string) $date !== $this->getDateStart() )
177
		{
178
			$this->values[$this->prefix . 'datestart'] = $this->checkDateFormat( $date );
179
			$this->setModified();
180
		}
181
182
		return $this;
183
	}
184
185
186
	/**
187
	 * Returns the end date of the common list item (YYYY-MM-DD hh:mm:ss).
188
	 *
189
	 * @return string|null End date of the common list item (YYYY-MM-DD hh:mm:ss)
190
	 */
191
	public function getDateEnd()
192
	{
193
		if( isset( $this->values[$this->prefix . 'dateend'] ) ) {
194
			return (string) $this->values[$this->prefix . 'dateend'];
195
		}
196
197
		return null;
198
	}
199
200
201
	/**
202
	 * Sets the new end date of the common list item (YYYY-MM-DD hh:mm:ss).
203
	 *
204
	 * @param string $date New end date of the common list item (YYYY-MM-DD hh:mm:ss)
205
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls
206
	 */
207
	public function setDateEnd( $date )
208
	{
209
		if( (string) $date !== $this->getDateEnd() )
210
		{
211
			$this->values[$this->prefix . 'dateend'] = $this->checkDateFormat( $date );
212
			$this->setModified();
213
		}
214
215
		return $this;
216
	}
217
218
219
	/**
220
	 * Returns the type of the list item.
221
	 *
222
	 * @return string|null Type of the list item
223
	 */
224
	public function getType()
225
	{
226
		if( isset( $this->values[$this->prefix . 'type'] ) ) {
227
			return (string) $this->values[$this->prefix . 'type'];
228
		}
229
	}
230
231
232
233
	/**
234
	 * Sets the new type of the list item.
235
	 *
236
	 * @param string $type type of the list item
237
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls
238
	 */
239
	public function setType( $type )
240
	{
241
		if( (string) $type != $this->getType() )
242
		{
243
			$this->values[$this->prefix . 'type'] = (string) $type;
244
			$this->setModified();
245
		}
246
247
		return $this;
248
	}
249
250
251
	/**
252
	 * Returns the position of the list item.
253
	 *
254
	 * @return integer Position of the list item
255
	 */
256
	public function getPosition()
257
	{
258
		if( isset( $this->values[$this->prefix . 'position'] ) ) {
259
			return (int) $this->values[$this->prefix . 'position'];
260
		}
261
262
		return 0;
263
	}
264
265
266
	/**
267
	 * Sets the new position of the list item.
268
	 *
269
	 * @param integer $pos position of the list item
270
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls
271
	 */
272
	public function setPosition( $pos )
273
	{
274
		if( (int) $pos !== $this->getPosition() )
275
		{
276
			$this->values[$this->prefix . 'position'] = (int) $pos;
277
			$this->setModified();
278
		}
279
280
		return $this;
281
	}
282
283
284
	/**
285
	 * Returns the status of the list item.
286
	 *
287
	 * @return integer Status of the item
288
	 */
289
	public function getStatus()
290
	{
291
		if( isset( $this->values[$this->prefix . 'status'] ) ) {
292
			return (int) $this->values[$this->prefix . 'status'];
293
		}
294
295
		return 1;
296
	}
297
298
299
	/**
300
	 * Sets the new status of the list item.
301
	 *
302
	 * @param integer $status Status of the item
303
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls
304
	 */
305
	public function setStatus( $status )
306
	{
307
		if( (int) $status !== $this->getStatus() )
308
		{
309
			$this->values[$this->prefix . 'status'] = (int) $status;
310
			$this->setModified();
311
		}
312
313
		return $this;
314
	}
315
316
317
	/**
318
	 * Returns the configuration of the list item.
319
	 *
320
	 * @return array Custom configuration values
321
	 */
322
	public function getConfig()
323
	{
324
		if( isset( $this->values[$this->prefix . 'config'] ) ) {
325
			return (array) $this->values[$this->prefix . 'config'];
326
		}
327
328
		return [];
329
	}
330
331
332
	/**
333
	 * Sets the new configuration for the list item.
334
	 *
335
	 * @param array $config Custom configuration values
336
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls
337
	 */
338
	public function setConfig( array $config )
339
	{
340
		$this->values[$this->prefix . 'config'] = $config;
341
		$this->setModified();
342
343
		return $this;
344
	}
345
346
347
	/**
348
	 * Returns the referenced item if it's available.
349
	 *
350
	 * @return \Aimeos\MShop\Common\Item\Iface Referenced list item
351
	 */
352
	public function getRefItem()
353
	{
354
		return $this->refItem;
355
	}
356
357
358
	/**
359
	 * Stores the item referenced by the list item.
360
	 *
361
	 * @param \Aimeos\MShop\Common\Item\Iface|null $refItem Item referenced by the list item or null for no reference
362
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface Lists item for chaining method calls
363
	 */
364
	public function setRefItem( \Aimeos\MShop\Common\Item\Iface $refItem = null )
365
	{
366
		$this->refItem = $refItem;
367
368
		return $this;
369
	}
370
371
372
	/**
373
	 * Returns the item type
374
	 *
375
	 * @return string Item type, subtypes are separated by slashes
376
	 */
377
	public function getResourceType()
378
	{
379
		return str_replace( '.', '/', rtrim( $this->prefix, '.' ) );
380
	}
381
382
383
	/**
384
	 * Tests if the item is available based on status, time, language and currency
385
	 *
386
	 * @return boolean True if available, false if not
387
	 */
388
	public function isAvailable()
389
	{
390
		return parent::isAvailable() && (bool) $this->getStatus()
391
			&& ( $this->getDateStart() === null || $this->getDateStart() < $this->values['date'] )
392
			&& ( $this->getDateEnd() === null || $this->getDateEnd() > $this->values['date'] );
393
	}
394
395
396
	/*
397
	 * Sets the item values from the given array and removes that entries from the list
398
	 *
399
	 * @param array &$list Associative list of item keys and their values
400
	 * @param boolean True to set private properties too, false for public only
401
	 * @return \Aimeos\MShop\Common\Item\Lists\Iface List item for chaining method calls
402
	 */
403
	public function fromArray( array &$list, $private = false )
404
	{
405
		$item = parent::fromArray( $list, $private );
406
407
		foreach( $list as $key => $value )
408
		{
409
			switch( $key )
410
			{
411
				case $this->prefix . 'parentid': !$private ?: $item = $item->setParentId( $value ); break;
412
				case $this->prefix . 'type': $item = $item->setType( $value ); break;
413
				case $this->prefix . 'domain': $item = $item->setDomain( $value ); break;
414
				case $this->prefix . 'refid': $item = $item->setRefId( $value ); break;
0 ignored issues
show
Bug introduced by
The method setRefId() does not exist on Aimeos\MShop\Common\Item\Base. Did you maybe mean setId()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

414
				case $this->prefix . 'refid': /** @scrutinizer ignore-call */ $item = $item->setRefId( $value ); break;

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
415
				case $this->prefix . 'datestart': $item = $item->setDateStart( $value ); break;
416
				case $this->prefix . 'dateend': $item = $item->setDateEnd( $value ); break;
417
				case $this->prefix . 'config': $item = $item->setConfig( $value ); break;
418
				case $this->prefix . 'position': $item = $item->setPosition( $value ); break;
419
				case $this->prefix . 'status': $item = $item->setStatus( $value ); break;
420
				default: continue 2;
421
			}
422
423
			unset( $list[$key] );
424
		}
425
426
		return $item;
427
	}
428
429
430
	/**
431
	 * Returns the item values as array.
432
	 *
433
	 * @param boolean True to return private properties, false for public only
434
	 * @return array Associative list of item properties and their values
435
	 */
436
	public function toArray( $private = false )
437
	{
438
		$list = parent::toArray( $private );
439
440
		$list[$this->prefix . 'domain'] = $this->getDomain();
441
		$list[$this->prefix . 'refid'] = $this->getRefId();
442
		$list[$this->prefix . 'datestart'] = $this->getDateStart();
443
		$list[$this->prefix . 'dateend'] = $this->getDateEnd();
444
		$list[$this->prefix . 'config'] = $this->getConfig();
445
		$list[$this->prefix . 'position'] = $this->getPosition();
446
		$list[$this->prefix . 'status'] = $this->getStatus();
447
		$list[$this->prefix . 'type'] = $this->getType();
448
449
		if( $private === true ) {
450
			$list[$this->prefix . 'parentid'] = $this->getParentId();
451
		}
452
453
		return $list;
454
	}
455
456
}
457