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

Standard::fromArray()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 9
nc 5
nop 2
dl 0
loc 18
rs 8.8333
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, 2012
6
 * @copyright Aimeos (aimeos.org), 2015-2018
7
 * @package MShop
8
 * @subpackage Order
9
 */
10
11
12
namespace Aimeos\MShop\Order\Item\Base\Coupon;
13
14
15
/**
16
 * Default implementation for order item base coupon.
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\Coupon\Iface
24
{
25
	private $values;
26
27
	/**
28
	 * Initializes the order base coupon item.
29
	 *
30
	 * @param array $values Associative list of order coupon values
31
	 */
32
	public function __construct( array $values = [] )
33
	{
34
		parent::__construct( 'order.base.coupon.', $values );
35
36
		$this->values = $values;
37
	}
38
39
40
	/**
41
	 * Returns the base ID of the order.
42
	 *
43
	 * @return string|null Order base ID
44
	 */
45
	public function getBaseId()
46
	{
47
		if( isset( $this->values['order.base.coupon.baseid'] ) ) {
48
			return (string) $this->values['order.base.coupon.baseid'];
49
		}
50
	}
51
52
53
	/**
54
	 * Sets the Base ID of the order.
55
	 *
56
	 * @param string $baseid Order base ID.
57
	 * @return \Aimeos\MShop\Order\Item\Base\Coupon\Iface Order base coupon item for chaining method calls
58
	 */
59
	public function setBaseId( $baseid )
60
	{
61
		if( (string) $baseid !== $this->getBaseId() )
62
		{
63
			$this->values['order.base.coupon.baseid'] = (string) $baseid;
64
			$this->setModified();
65
		}
66
67
		return $this;
68
	}
69
70
71
	/**
72
	 * Returns the ID of the ordered product.
73
	 *
74
	 * @return string|null ID of the ordered product.
75
	 */
76
	public function getProductId()
77
	{
78
		if( isset( $this->values['order.base.coupon.ordprodid'] ) ) {
79
			return (string) $this->values['order.base.coupon.ordprodid'];
80
		}
81
	}
82
83
84
	/**
85
	 * Sets the ID of the ordered product.
86
	 *
87
	 * @param string $productid ID of the ordered product
88
	 * @return \Aimeos\MShop\Order\Item\Base\Coupon\Iface Order base coupon item for chaining method calls
89
	 */
90
	public function setProductId( $productid )
91
	{
92
		if( (string) $productid !== $this->getProductId() )
93
		{
94
			$this->values['order.base.coupon.ordprodid'] = (string) $productid;
95
			$this->setModified();
96
		}
97
98
		return $this;
99
	}
100
101
102
	/**
103
	 * Returns the coupon code.
104
	 *
105
	 * @return string|null Coupon code.
106
	 */
107
	public function getCode()
108
	{
109
		if( isset( $this->values['order.base.coupon.code'] ) ) {
110
			return (string) $this->values['order.base.coupon.code'];
111
		}
112
	}
113
114
115
	/**
116
	 * Sets the coupon code.
117
	 *
118
	 * @param string $code Coupon code
119
	 * @return \Aimeos\MShop\Order\Item\Base\Coupon\Iface Order base coupon item for chaining method calls
120
	 */
121
	public function setCode( $code )
122
	{
123
		if( (string) $code !== $this->getCode() )
124
		{
125
			$this->values['order.base.coupon.code'] = (string) $this->checkCode( $code );;
126
			$this->setModified();
127
		}
128
129
		return $this;
130
	}
131
132
133
	/**
134
	 * Returns the item type
135
	 *
136
	 * @return string Item type, subtypes are separated by slashes
137
	 */
138
	public function getResourceType()
139
	{
140
		return 'order/base/coupon';
141
	}
142
143
144
	/*
145
	 * Sets the item values from the given array and removes that entries from the list
146
	 *
147
	 * @param array &$list Associative list of item keys and their values
148
	 * @param boolean True to set private properties too, false for public only
149
	 * @return \Aimeos\MShop\Order\Item\Base\Coupon\Iface Order coupon item for chaining method calls
150
	 */
151
	public function fromArray( array &$list, $private = false )
152
	{
153
		$item = parent::fromArray( $list, $private );
154
155
		foreach( $list as $key => $value )
156
		{
157
			switch( $key )
158
			{
159
				case 'order.base.coupon.baseid': !$private ?: $item = $item->setBaseId( $value ); break;
160
				case 'order.base.coupon.productid': !$private ?: $item = $item->setProductId( $value ); break;
161
				case 'order.base.coupon.code': $item = $item->setCode( $value ); break;
162
				default: continue 2;
163
			}
164
165
			unset( $list[$key] );
166
		}
167
168
		return $item;
169
	}
170
171
172
	/**
173
	 * Returns the item values as array.
174
	 *
175
	 * @param boolean True to return private properties, false for public only
176
	 * @return array Associative list of item properties and their values
177
	 */
178
	public function toArray( $private = false )
179
	{
180
		$list = parent::toArray( $private );
181
182
		$list['order.base.coupon.code'] = $this->getCode();
183
		$list['order.base.coupon.productid'] = $this->getProductId();
184
185
		if( $private === true ) {
186
			$list['order.base.coupon.baseid'] = $this->getBaseId();
187
		}
188
189
		return $list;
190
	}
191
192
}
193