Completed
Push — master ( 69ae10...ab8a43 )
by Aimeos
09:58
created

Standard::getPropertyItems()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 7
nc 3
nop 2
dl 0
loc 15
rs 8.8571
c 0
b 0
f 0
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-2017
7
 * @package MShop
8
 * @subpackage Product
9
 */
10
11
12
namespace Aimeos\MShop\Product\Item;
13
14
15
/**
16
 * Default impelementation of a product item.
17
 *
18
 * @package MShop
19
 * @subpackage Product
20
 */
21
class Standard
22
	extends \Aimeos\MShop\Common\Item\ListRef\Base
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces between "Base" and comma; 1 found
Loading history...
23
	implements \Aimeos\MShop\Product\Item\Iface
24
{
25
	private $values;
26
	private $propItems;
27
28
29
	/**
30
	 * Initializes the item object.
31
	 *
32
	 * @param array $values Parameter for initializing the basic properties
33
	 * @param \Aimeos\MShop\Common\Lists\Item\Iface[] $listItems List of list items
34
	 * @param \Aimeos\MShop\Common\Item\Iface[] $refItems List of referenced items
35
	 * @param \Aimeos\MShop\Common\Item\Property\Iface[] $propItems List of property items
36
	 */
37
	public function __construct( array $values = [], array $listItems = [],
38
		array $refItems = [], array $propItems = [] )
39
	{
40
		parent::__construct( 'product.', $values, $listItems, $refItems );
41
42
		$this->propItems = $propItems;
43
		$this->values = $values;
44
	}
45
46
47
	/**
48
	 * Returns the property items of the product
49
	 *
50
	 * @param string|null $type Name of the property item type or null for all
51
	 * @param boolean $active True to return only active items, false to return all
52
	 * @return \Aimeos\MShop\Product\Item\Property\Iface[] Associative list of property IDs as keys and property items as values
53
	 */
54
	public function getPropertyItems( $type = null, $active = true )
55
	{
56
		$list = [];
57
58
		foreach( $this->propItems as $propId => $propItem )
59
		{
60
			if( ( $type === null || $propItem->getType() === $type )
61
				&& ( $active === false || $propItem->isAvailable() )
62
			) {
63
				$list[$propId] = $propItem;
64
			}
65
		}
66
67
		return $list;
68
	}
69
70
71
	/**
72
	 * Returns the type ID of the product item.
73
	 *
74
	 * @return string|null Type ID of the product item
75
	 */
76
	public function getTypeId()
77
	{
78
		if( isset( $this->values['product.typeid'] ) ) {
79
			return (string) $this->values['product.typeid'];
80
		}
81
	}
82
83
84
	/**
85
	 * Sets the new type ID of the product item.
86
	 *
87
	 * @param string $typeid New type ID of the product item
88
	 * @return \Aimeos\MShop\Product\Item\Iface Product item for chaining method calls
89
	 */
90
	public function setTypeId( $typeid )
91
	{
92
		if( (string) $typeid !== $this->getTypeId() )
93
		{
94
			$this->values['product.typeid'] = (string) $typeid;
95
			$this->setModified();
96
		}
97
98
		return $this;
99
	}
100
101
102
	/**
103
	 * Returns the type of the product item.
104
	 *
105
	 * @return string|null Type of the product item
106
	 */
107
	public function getType()
108
	{
109
		if( isset( $this->values['product.type'] ) ) {
110
			return (string) $this->values['product.type'];
111
		}
112
	}
113
114
115
	/**
116
	 * Returns the localized name of the type
117
	 *
118
	 * @return string|null Localized name of the type
119
	 */
120
	public function getTypeName()
121
	{
122
		if( isset( $this->values['product.typename'] ) ) {
123
			return (string) $this->values['product.typename'];
124
		}
125
	}
126
127
128
	/**
129
	 * Returns the status of the product item.
130
	 *
131
	 * @return integer Status of the product item
132
	 */
133
	public function getStatus()
134
	{
135
		if( isset( $this->values['product.status'] ) ) {
136
			return (int) $this->values['product.status'];
137
		}
138
139
		return 0;
140
	}
141
142
143
	/**
144
	 * Sets the new status of the product item.
145
	 *
146
	 * @param integer $status New status of the product item
147
	 * @return \Aimeos\MShop\Product\Item\Iface Product item for chaining method calls
148
	 */
149
	public function setStatus( $status )
150
	{
151
		if( (int) $status !== $this->getStatus() )
152
		{
153
			$this->values['product.status'] = (int) $status;
154
			$this->setModified();
155
		}
156
157
		return $this;
158
	}
159
160
161
	/**
162
	 * Returns the code of the product item.
163
	 *
164
	 * @return string Code of the product item
165
	 */
166
	public function getCode()
167
	{
168
		if( isset( $this->values['product.code'] ) ) {
169
			return (string) $this->values['product.code'];
170
		}
171
172
		return '';
173
	}
174
175
176
	/**
177
	 * Sets the new code of the product item.
178
	 *
179
	 * @param string $code New code of product item
180
	 * @return \Aimeos\MShop\Product\Item\Iface Product item for chaining method calls
181
	 */
182
	public function setCode( $code )
183
	{
184
		if( (string) $code !== $this->getCode() )
185
		{
186
			$this->values['product.code'] = (string) $this->checkCode( $code );
187
			$this->setModified();
188
		}
189
190
		return $this;
191
	}
192
193
194
	/**
195
	 * Returns the label of the product item.
196
	 *
197
	 * @return string Label of the product item
198
	 */
199
	public function getLabel()
200
	{
201
		if( isset( $this->values['product.label'] ) ) {
202
			return (string) $this->values['product.label'];
203
		}
204
205
		return '';
206
	}
207
208
209
	/**
210
	 * Sets a new label of the product item.
211
	 *
212
	 * @param string $label New label of the product item
213
	 * @return \Aimeos\MShop\Product\Item\Iface Product item for chaining method calls
214
	 */
215
	public function setLabel( $label )
216
	{
217
		if( (string) $label !== $this->getLabel() )
218
		{
219
			$this->values['product.label'] = (string) $label;
220
			$this->setModified();
221
		}
222
223
		return $this;
224
	}
225
226
227
	/**
228
	 * Returns the starting point of time, in which the product is available.
229
	 *
230
	 * @return string|null ISO date in YYYY-MM-DD hh:mm:ss format
231
	 */
232
	public function getDateStart()
233
	{
234
		if( isset( $this->values['product.datestart'] ) ) {
235
			return (string) $this->values['product.datestart'];
236
		}
237
	}
238
239
240
	/**
241
	 * Sets a new starting point of time, in which the product is available.
242
	 *
243
	 * @param string|null New ISO date in YYYY-MM-DD hh:mm:ss format
244
	 * @return \Aimeos\MShop\Product\Item\Iface Product item for chaining method calls
245
	 */
246
	public function setDateStart( $date )
247
	{
248
		if( $date !== $this->getDateStart() )
249
		{
250
			$this->values['product.datestart'] = $this->checkDateFormat( $date );
251
			$this->setModified();
252
		}
253
254
		return $this;
255
	}
256
257
258
	/**
259
	 * Returns the ending point of time, in which the product is available.
260
	 *
261
	 * @return string|null ISO date in YYYY-MM-DD hh:mm:ss format
262
	 */
263
	public function getDateEnd()
264
	{
265
		if( isset( $this->values['product.dateend'] ) ) {
266
			return (string) $this->values['product.dateend'];
267
		}
268
	}
269
270
271
	/**
272
	 * Sets a new ending point of time, in which the product is available.
273
	 *
274
	 * @param string|null New ISO date in YYYY-MM-DD hh:mm:ss format
275
	 * @return \Aimeos\MShop\Product\Item\Iface Product item for chaining method calls
276
	 */
277
	public function setDateEnd( $date )
278
	{
279
		if( $date !== $this->getDateEnd() )
280
		{
281
			$this->values['product.dateend'] = $this->checkDateFormat( $date );
282
			$this->setModified();
283
		}
284
285
		return $this;
286
	}
287
288
289
	/**
290
	 * Returns the configuration values of the item
291
	 *
292
	 * @return array Configuration values
293
	 */
294
	public function getConfig()
295
	{
296
		if( isset( $this->values['product.config'] ) ) {
297
			return (array) $this->values['product.config'];
298
		}
299
300
		return [];
301
	}
302
303
304
	/**
305
	 * Sets the configuration values of the item.
306
	 *
307
	 * @param array $config Configuration values
308
	 * @return \Aimeos\MShop\Product\Item\Iface Product item for chaining method calls
309
	 */
310
	public function setConfig( array $config )
311
	{
312
		$this->values['product.config'] = $config;
313
		$this->setModified();
314
315
		return $this;
316
	}
317
318
319
	/**
320
	 * Returns the URL target specific for that product
321
	 *
322
	 * @return string URL target specific for that product
323
	 */
324
	public function getTarget()
325
	{
326
		if( isset( $this->values['product.target'] ) ) {
327
			return (string) $this->values['product.target'];
328
		}
329
330
		return '';
331
	}
332
333
334
	/**
335
	 * Sets a new URL target specific for that product
336
	 *
337
	 * @param string $value New URL target specific for that product
338
	 * @return \Aimeos\MShop\Product\Item\Iface Product item for chaining method calls
339
	 */
340
	public function setTarget( $value )
341
	{
342
		if( (string) $value !== $this->getTarget() )
343
		{
344
			$this->values['product.target'] = (string) $value;
345
			$this->setModified();
346
		}
347
348
		return $this;
349
	}
350
351
352
	/**
353
	 * Returns the item type
354
	 *
355
	 * @return string Item type, subtypes are separated by slashes
356
	 */
357
	public function getResourceType()
358
	{
359
		return 'product';
360
	}
361
362
363
	/**
364
	 * Tests if the item is available based on status, time, language and currency
365
	 *
366
	 * @return boolean True if available, false if not
367
	 */
368
	public function isAvailable()
369
	{
370
		return (bool) $this->getStatus()
371
			&& ( $this->getDateStart() === null || $this->getDateStart() < $this->values['date'] )
372
			&& ( $this->getDateEnd() === null || $this->getDateEnd() > $this->values['date'] );
373
	}
374
375
376
	/**
377
	 * Sets the item values from the given array.
378
	 *
379
	 * @param array $list Associative list of item keys and their values
380
	 * @return array Associative list of keys and their values that are unknown
381
	 */
382
	public function fromArray( array $list )
383
	{
384
		$unknown = [];
385
		$list = parent::fromArray( $list );
386
		unset( $list['product.type'], $list['product.typename'] );
387
388
		foreach( $list as $key => $value )
389
		{
390
			switch( $key )
391
			{
392
				case 'product.typeid': $this->setTypeId( $value ); break;
393
				case 'product.code': $this->setCode( $value ); break;
394
				case 'product.label': $this->setLabel( $value ); break;
395
				case 'product.status': $this->setStatus( $value ); break;
396
				case 'product.datestart': $this->setDateStart( $value ); break;
397
				case 'product.dateend': $this->setDateEnd( $value ); break;
398
				case 'product.config': $this->setConfig( $value ); break;
399
				case 'product.target': $this->setTarget( $value ); break;
400
				default: $unknown[$key] = $value;
0 ignored issues
show
Coding Style introduced by
The default body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a default statement must start on the line immediately following the statement.

switch ($expr) {
    default:
        doSomething(); //right
        break;
}


switch ($expr) {
    default:

        doSomething(); //wrong
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
401
			}
402
		}
403
404
		return $unknown;
405
	}
406
407
408
	/**
409
	 * Returns the item values as array.
410
	 *
411
	 * @param boolean True to return private properties, false for public only
412
	 * @return array Associative list of item properties and their values
413
	 */
414
	public function toArray( $private = false )
415
	{
416
		$list = parent::toArray( $private );
417
418
		$list['product.typename'] = $this->getTypeName();
419
		$list['product.type'] = $this->getType();
420
		$list['product.code'] = $this->getCode();
421
		$list['product.label'] = $this->getLabel();
422
		$list['product.status'] = $this->getStatus();
423
		$list['product.datestart'] = $this->getDateStart();
424
		$list['product.dateend'] = $this->getDateEnd();
425
		$list['product.config'] = $this->getConfig();
426
427
		if( $private === true )
428
		{
429
			$list['product.typeid'] = $this->getTypeId();
430
			$list['product.target'] = $this->getTarget();
431
		}
432
433
		return $list;
434
	}
435
}
436