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

Standard::fromArray()   C

Complexity

Conditions 13
Paths 10

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 14
nc 10
nop 2
dl 0
loc 23
rs 6.6166
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 Order
9
 */
10
11
12
namespace Aimeos\MShop\Order\Item\Base\Product\Attribute;
13
14
15
/**
16
 * Default product attribute item implementation.
17
 *
18
 * @package MShop
19
 * @subpackage Order
20
 */
21
class Standard
22
	extends \Aimeos\MShop\Common\Item\Base
23
	implements \Aimeos\MShop\Order\Item\Base\Product\Attribute\Iface
24
{
25
	private $values;
26
27
	/**
28
	 * Initializes the order product attribute instance.
29
	 *
30
	 * @param array $values Associative array of order product attribute values. Possible
31
	 * keys: 'id', 'ordprodid', 'value', 'code', 'mtime'
32
	 */
33
	public function __construct( array $values = [] )
34
	{
35
		parent::__construct( 'order.base.product.attribute.', $values );
36
37
		$this->values = $values;
38
	}
39
40
41
	/**
42
	 * Returns the ID of the site the item is stored
43
	 *
44
	 * @return string|null Site ID (or null if not available)
45
	 */
46
	public function getSiteId()
47
	{
48
		if( isset( $this->values['order.base.product.attribute.siteid'] ) ) {
49
			return (string) $this->values['order.base.product.attribute.siteid'];
50
		}
51
	}
52
53
54
	/**
55
	 * Sets the site ID of the item.
56
	 *
57
	 * @param string $value Unique site ID of the item
58
	 * @return \Aimeos\MShop\Order\Item\Base\Product\Attribute\Iface Order base product attribute item for chaining method calls
59
	 */
60
	public function setSiteId( $value )
61
	{
62
		if( (string) $value !== $this->getSiteId() )
63
		{
64
			$this->values['order.base.product.attribute.siteid'] = (string) $value;
65
			$this->setModified();
66
		}
67
68
		return $this;
69
	}
70
71
72
	/**
73
	 * Returns the original attribute ID of the product attribute item.
74
	 *
75
	 * @return string Attribute ID of the product attribute item
76
	 */
77
	public function getAttributeId()
78
	{
79
		if( isset( $this->values['order.base.product.attribute.attributeid'] ) ) {
80
			return (string) $this->values['order.base.product.attribute.attributeid'];
81
		}
82
83
		return '';
84
	}
85
86
87
	/**
88
	 * Sets the original attribute ID of the product attribute item.
89
	 *
90
	 * @param string $id Attribute ID of the product attribute item
91
	 * @return \Aimeos\MShop\Order\Item\Base\Product\Attribute\Iface Order base product attribute item for chaining method calls
92
	 */
93
	public function setAttributeId( $id )
94
	{
95
		if( (string) $id !== $this->getAttributeId() )
96
		{
97
			$this->values['order.base.product.attribute.attributeid'] = (string) $id;
98
			$this->setModified();
99
		}
100
101
		return $this;
102
	}
103
104
105
	/**
106
	 * Returns the ID of the ordered product as parent
107
	 *
108
	 * @return string|null ID of the ordered product
109
	 */
110
	public function getParentId()
111
	{
112
		if( isset( $this->values['order.base.product.attribute.parentid'] ) ) {
113
			return (string) $this->values['order.base.product.attribute.parentid'];
114
		}
115
	}
116
117
118
	/**
119
	 * Sets the ID of the ordered product as parent
120
	 *
121
	 * @param string $id ID of the ordered product
122
	 * @return \Aimeos\MShop\Order\Item\Base\Product\Attribute\Iface Order base product attribute item for chaining method calls
123
	 */
124
	public function setParentId( $id )
125
	{
126
		if( (string) $id !== $this->getParentId() )
127
		{
128
			$this->values['order.base.product.attribute.parentid'] = (string) $id;
129
			$this->setModified();
130
		}
131
132
		return $this;
133
	}
134
135
136
	/**
137
	 * Returns the value of the product attribute.
138
	 *
139
	 * @return string Value of the product attribute
140
	 */
141
	public function getType()
142
	{
143
		if( isset( $this->values['order.base.product.attribute.type'] ) ) {
144
			return (string) $this->values['order.base.product.attribute.type'];
145
		}
146
147
		return '';
148
	}
149
150
151
	/**
152
	 * Sets the value of the product attribute.
153
	 *
154
	 * @param string $type Type of the product attribute
155
	 * @return \Aimeos\MShop\Order\Item\Base\Product\Attribute\Iface Order base product attribute item for chaining method calls
156
	 */
157
	public function setType( $type )
158
	{
159
		if( (string) $type !== $this->getType() )
160
		{
161
			$this->values['order.base.product.attribute.type'] = (string) $type;
162
			$this->setModified();
163
		}
164
165
		return $this;
166
	}
167
168
169
	/**
170
	 * Returns the code of the product attibute.
171
	 *
172
	 * @return string Code of the attribute
173
	 */
174
	public function getCode()
175
	{
176
		if( isset( $this->values['order.base.product.attribute.code'] ) ) {
177
			return (string) $this->values['order.base.product.attribute.code'];
178
		}
179
180
		return '';
181
	}
182
183
184
	/**
185
	 * Sets the code of the product attribute.
186
	 *
187
	 * @param string $code Code of the attribute
188
	 * @return \Aimeos\MShop\Order\Item\Base\Product\Attribute\Iface Order base product attribute item for chaining method calls
189
	 */
190
	public function setCode( $code )
191
	{
192
		if( (string) $code !== $this->getCode() )
193
		{
194
			$this->values['order.base.product.attribute.code'] = (string) $this->checkCode( $code );
195
			$this->setModified();
196
		}
197
198
		return $this;
199
	}
200
201
202
	/**
203
	 * Returns the localized name of the product attribute.
204
	 *
205
	 * @return string Localized name of the product attribute
206
	 */
207
	public function getName()
208
	{
209
		if( isset( $this->values['order.base.product.attribute.name'] ) ) {
210
			return (string) $this->values['order.base.product.attribute.name'];
211
		}
212
213
		return '';
214
	}
215
216
217
	/**
218
	 * Sets the localized name of the product attribute.
219
	 *
220
	 * @param string $name Localized name of the product attribute
221
	 * @return \Aimeos\MShop\Order\Item\Base\Product\Attribute\Iface Order base product attribute item for chaining method calls
222
	 */
223
	public function setName( $name )
224
	{
225
		if( (string) $name !== $this->getName() )
226
		{
227
			$this->values['order.base.product.attribute.name'] = (string) $name;
228
			$this->setModified();
229
		}
230
231
		return $this;
232
	}
233
234
235
	/**
236
	 * Returns the value of the product attribute.
237
	 *
238
	 * @return string|array Value of the product attribute
239
	 */
240
	public function getValue()
241
	{
242
		if( isset( $this->values['order.base.product.attribute.value'] ) ) {
243
			return $this->values['order.base.product.attribute.value'];
244
		}
245
246
		return '';
247
	}
248
249
250
	/**
251
	 * Sets the value of the product attribute.
252
	 *
253
	 * @param string|array $value Value of the product attribute
254
	 * @return \Aimeos\MShop\Order\Item\Base\Product\Attribute\Iface Order base product attribute item for chaining method calls
255
	 */
256
	public function setValue( $value )
257
	{
258
		if( $value !== $this->getValue() )
259
		{
260
			$this->values['order.base.product.attribute.value'] = $value;
261
			$this->setModified();
262
		}
263
264
		return $this;
265
	}
266
267
268
	/**
269
	 * Returns the quantity of the product attribute.
270
	 *
271
	 * @return integer Quantity of the product attribute
272
	 */
273
	public function getQuantity()
274
	{
275
		if( isset( $this->values['order.base.product.attribute.quantity'] ) ) {
276
			return (int) $this->values['order.base.product.attribute.quantity'];
277
		}
278
279
		return 1;
280
	}
281
282
283
	/**
284
	 * Sets the quantity of the product attribute.
285
	 *
286
	 * @param integer $value Quantity of the product attribute
287
	 * @return \Aimeos\MShop\Order\Item\Base\Product\Attribute\Iface Order base product attribute item for chaining method calls
288
	 */
289
	public function setQuantity( $value )
290
	{
291
		if( (int) $value !== $this->getQuantity() )
292
		{
293
			$this->values['order.base.product.attribute.quantity'] = (int) $value;
294
			$this->setModified();
295
		}
296
297
		return $this;
298
	}
299
300
301
	/**
302
	 * Returns the item type
303
	 *
304
	 * @return string Item type, subtypes are separated by slashes
305
	 */
306
	public function getResourceType()
307
	{
308
		return 'order/base/product/attribute';
309
	}
310
311
312
	/**
313
	 * Copys all data from a given attribute item.
314
	 *
315
	 * @param \Aimeos\MShop\Attribute\Item\Iface $item Attribute item to copy from
316
	 * @return \Aimeos\MShop\Order\Item\Base\Product\Attribute\Iface Order base product attribute item for chaining method calls
317
	 */
318
	public function copyFrom( \Aimeos\MShop\Attribute\Item\Iface $item )
319
	{
320
		$this->setSiteId( $item->getSiteId() );
321
		$this->setAttributeId( $item->getId() );
322
		$this->setName( $item->getName() );
323
		$this->setCode( $item->getType() );
324
		$this->setValue( $item->getCode() );
325
326
		$this->setModified();
327
328
		return $this;
329
	}
330
331
332
	/*
333
	 * Sets the item values from the given array and removes that entries from the list
334
	 *
335
	 * @param array &$list Associative list of item keys and their values
336
	 * @param boolean True to set private properties too, false for public only
337
	 * @return \Aimeos\MShop\Order\Item\Base\Product\Attribute\Iface Order product attribute item for chaining method calls
338
	 */
339
	public function fromArray( array &$list, $private = false )
340
	{
341
		$item = parent::fromArray( $list, $private );
342
343
		foreach( $list as $key => $value )
344
		{
345
			switch( $key )
346
			{
347
				case 'order.base.product.attribute.siteid': !$private ?: $item = $item->setSiteId( $value ); break;
348
				case 'order.base.product.attribute.attrid': !$private ?: $item = $item->setAttributeId( $value ); break;
349
				case 'order.base.product.attribute.parentid': !$private ?: $item = $item->setParentId( $value ); break;
350
				case 'order.base.product.attribute.type': $item = $item->setType( $value ); break;
351
				case 'order.base.product.attribute.code': $item = $item->setCode( $value ); break;
352
				case 'order.base.product.attribute.value': $item = $item->setValue( $value ); break;
353
				case 'order.base.product.attribute.name': $item = $item->setName( $value ); break;
354
				case 'order.base.product.attribute.quantity': $item = $item->setQuantity( $value ); break;
355
				default: continue 2;
356
			}
357
358
			unset( $list[$key] );
359
		}
360
361
		return $item;
362
	}
363
364
365
	/**
366
	 * Returns the item values as array.
367
	 *
368
	 * @param boolean True to return private properties, false for public only
369
	 * @return array Associative list of item properties and their values
370
	 */
371
	public function toArray( $private = false )
372
	{
373
		$list = parent::toArray( $private );
374
375
		$list['order.base.product.attribute.type'] = $this->getType();
376
		$list['order.base.product.attribute.code'] = $this->getCode();
377
		$list['order.base.product.attribute.name'] = $this->getName();
378
		$list['order.base.product.attribute.value'] = $this->getValue();
379
		$list['order.base.product.attribute.quantity'] = $this->getQuantity();
380
381
		if( $private === true )
382
		{
383
			$list['order.base.product.attribute.attrid'] = $this->getAttributeId();
384
			$list['order.base.product.attribute.parentid'] = $this->getParentId();
385
		}
386
387
		return $list;
388
	}
389
}
390