Passed
Push — master ( 3bf164...51d5ad )
by Aimeos
04:31
created

Standard::setDateEnd()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018-2024
6
 * @package MShop
7
 * @subpackage Subscription
8
 */
9
10
11
namespace Aimeos\MShop\Subscription\Item;
12
13
14
/**
15
 * Default implementation of subscription item
16
 *
17
 * @package MShop
18
 * @subpackage Subscription
19
 */
20
class Standard
21
	extends \Aimeos\MShop\Common\Item\Base
22
	implements \Aimeos\MShop\Subscription\Item\Iface
23
{
24
	/**
25
	 * Returns the associated order item
26
	 *
27
	 * @return \Aimeos\MShop\Order\Item\Iface|null Order item
28
	 */
29
	public function getOrderItem() : ?\Aimeos\MShop\Order\Item\Iface
30
	{
31
		return $this->get( '.orderitem' );
32
	}
33
34
35
	/**
36
	 * Returns the ID of the order
37
	 *
38
	 * @return string|null ID of the order
39
	 */
40
	public function getOrderId() : ?string
41
	{
42
		return $this->get( 'subscription.orderid' );
43
	}
44
45
46
	/**
47
	 * Sets the ID of the order item which the customer bought
48
	 *
49
	 * @param string $id ID of the order
50
	 * @return \Aimeos\MShop\Subscription\Item\Iface Subscription item for chaining method calls
51
	 */
52
	public function setOrderId( string $id ) : \Aimeos\MShop\Subscription\Item\Iface
53
	{
54
		return $this->set( 'subscription.orderid', $id );
55
	}
56
57
58
	/**
59
	 * Returns the ID of the ordered product
60
	 *
61
	 * @return string|null ID of the ordered product
62
	 */
63
	public function getOrderProductId() : ?string
64
	{
65
		return $this->get( 'subscription.ordprodid' );
66
	}
67
68
69
	/**
70
	 * Sets the ID of the ordered product item which the customer subscribed for
71
	 *
72
	 * @param string $id ID of the ordered product
73
	 * @return \Aimeos\MShop\Subscription\Item\Iface Subscription item for chaining method calls
74
	 */
75
	public function setOrderProductId( string $id ) : \Aimeos\MShop\Subscription\Item\Iface
76
	{
77
		return $this->set( 'subscription.ordprodid', $id );
78
	}
79
80
81
	/**
82
	 * Returns the date of the next subscription renewal
83
	 *
84
	 * @return string|null ISO date in "YYYY-MM-DD HH:mm:ss" format
85
	 */
86
	public function getDateNext() : ?string
87
	{
88
		$value = $this->get( 'subscription.datenext' );
89
		return $value ? substr( $value, 0, 19 ) : null;
90
	}
91
92
93
	/**
94
	 * Sets the date of the next subscription renewal
95
	 *
96
	 * @param string $date ISO date in "YYYY-MM-DD HH:mm:ss" format
97
	 * @return \Aimeos\MShop\Subscription\Item\Iface Subscription item for chaining method calls
98
	 */
99
	public function setDateNext( string $date ) : \Aimeos\MShop\Subscription\Item\Iface
100
	{
101
		return $this->set( 'subscription.datenext', $this->checkDateFormat( $date ) );
102
	}
103
104
105
	/**
106
	 * Returns the date when the subscription renewal ends
107
	 *
108
	 * @return string|null ISO date in "YYYY-MM-DD HH:mm:ss" format
109
	 */
110
	public function getDateEnd() : ?string
111
	{
112
		$value = $this->get( 'subscription.dateend' );
113
		return $value ? substr( $value, 0, 19 ) : null;
114
	}
115
116
117
	/**
118
	 * Sets the delivery date of the invoice.
119
	 *
120
	 * @param string|null $date ISO date in "YYYY-MM-DD HH:mm:ss" format
121
	 * @return \Aimeos\MShop\Subscription\Item\Iface Subscription item for chaining method calls
122
	 */
123
	public function setDateEnd( ?string $date ) : \Aimeos\MShop\Subscription\Item\Iface
124
	{
125
		return $this->set( 'subscription.dateend', $this->checkDateFormat( $date ) );
126
	}
127
128
129
	/**
130
	 * Returns the time interval to pass between the subscription renewals
131
	 *
132
	 * @return string PHP time interval, e.g. "P1M2W"
133
	 */
134
	public function getInterval() : string
135
	{
136
		return $this->get( 'subscription.interval', '' );
137
	}
138
139
140
	/**
141
	 * Sets the time interval to pass between the subscription renewals
142
	 *
143
	 * @param string $value PHP time interval, e.g. "P1M2W"
144
	 * @return \Aimeos\MShop\Subscription\Item\Iface Subscription item for chaining method calls
145
	 */
146
	public function setInterval( string $value ) : \Aimeos\MShop\Subscription\Item\Iface
147
	{
148
		if( strlen( $value ) > 1 && preg_match( '/^P([0-9]+Y)?([0-9]+M)?([0-9]+W)?([0-9]+D)?(T?[0-9]+H)?$/', $value ) !== 1 ) {
149
			throw new \Aimeos\MShop\Subscription\Exception( sprintf( 'Invalid time interval format "%1$s"', $value ) );
150
		}
151
152
		return $this->set( 'subscription.interval', $value );
153
	}
154
155
156
	/**
157
	 * Returns the current renewal period of the subscription product
158
	 *
159
	 * @return int Current renewal period
160
	 */
161
	public function getPeriod() : int
162
	{
163
		return $this->get( 'subscription.period', 1 );
164
	}
165
166
167
	/**
168
	 * Sets the current renewal period of the subscription product
169
	 *
170
	 * @param int $value Current renewal period
171
	 * @return \Aimeos\MShop\Subscription\Item\Iface Subscription item for chaining method calls
172
	 */
173
	public function setPeriod( int $value ) : \Aimeos\MShop\Subscription\Item\Iface
174
	{
175
		return $this->set( 'subscription.period', $value );
176
	}
177
178
179
	/**
180
	 * Returns the product ID of the subscription product
181
	 *
182
	 * @return string Product ID
183
	 */
184
	public function getProductId() : string
185
	{
186
		return $this->get( 'subscription.productid', '' );
187
	}
188
189
190
	/**
191
	 * Sets the product ID of the subscription product
192
	 *
193
	 * @param string $value Product ID
194
	 * @return \Aimeos\MShop\Subscription\Item\Iface Subscription item for chaining method calls
195
	 */
196
	public function setProductId( string $value ) : \Aimeos\MShop\Subscription\Item\Iface
197
	{
198
		return $this->set( 'subscription.productid', $value );
199
	}
200
201
202
	/**
203
	 * Returns the reason for the end of the subscriptions
204
	 *
205
	 * @return int|null Reason code or NULL for no reason
206
	 */
207
	public function getReason() : ?int
208
	{
209
		return $this->get( 'subscription.reason' );
210
	}
211
212
213
	/**
214
	 * Sets the reason for the end of the subscriptions
215
	 *
216
	 * @param int|null $value Reason code or NULL for no reason
217
	 * @return \Aimeos\MShop\Subscription\Item\Iface Subscription item for chaining method calls
218
	 */
219
	public function setReason( ?int $value ) : \Aimeos\MShop\Subscription\Item\Iface
220
	{
221
		return $this->set( 'subscription.reason', $value );
222
	}
223
224
225
	/**
226
	 * Returns the status of the subscriptions
227
	 *
228
	 * @return int Subscription status, i.e. "1" for enabled, "0" for disabled
229
	 */
230
	public function getStatus() : int
231
	{
232
		return $this->get( 'subscription.status', 1 );
233
	}
234
235
236
	/**
237
	 * Sets the status of the subscriptions
238
	 *
239
	 * @return int Subscription status, i.e. "1" for enabled, "0" for disabled
240
	 * @return \Aimeos\MShop\Subscription\Item\Iface Subscription item for chaining method calls
241
	 */
242
	public function setStatus( int $status ) : \Aimeos\MShop\Subscription\Item\Iface
243
	{
244
		return $this->set( 'subscription.status', $status );
245
	}
246
247
248
	/*
249
	 * Sets the item values from the given array and removes that entries from the list
250
	 *
251
	 * @param array &$list Associative list of item keys and their values
252
	 * @param bool True to set private properties too, false for public only
253
	 * @return \Aimeos\MShop\Subscription\Item\Iface Subscription item for chaining method calls
254
	 */
255
	public function fromArray( array &$list, bool $private = false ) : \Aimeos\MShop\Common\Item\Iface
256
	{
257
		$item = parent::fromArray( $list, $private );
258
259
		foreach( $list as $key => $value )
260
		{
261
			switch( $key )
262
			{
263
				case 'subscription.orderid': $item = $item->setOrderId( $value ); break;
264
				case 'subscription.ordprodid': $item = $item->setOrderProductId( $value ); break;
265
				case 'subscription.productid': $item = $item->setProductId( $value ); break;
266
				case 'subscription.datenext': $item = $item->setDateNext( $value ); break;
267
				case 'subscription.dateend': $item = $item->setDateEnd( $value ); break;
268
				case 'subscription.interval': $item = $item->setInterval( $value ); break;
269
				case 'subscription.period': $item = $item->setPeriod( (int) $value ); break;
270
				case 'subscription.status': $item = $item->setStatus( (int) $value ); break;
271
				case 'subscription.reason': $item = $item->setReason( $value !== null ? (int) $value : null ); break;
272
				default: continue 2;
273
			}
274
275
			unset( $list[$key] );
276
		}
277
278
		return $item;
279
	}
280
281
282
	/**
283
	 * Returns the item values as associative list.
284
	 *
285
	 * @param bool True to return private properties, false for public only
286
	 * @return array Associative list of item properties and their values
287
	 */
288
	public function toArray( bool $private = false ) : array
289
	{
290
		$list = parent::toArray( $private );
291
292
		$list['subscription.orderid'] = $this->getOrderId();
293
		$list['subscription.ordprodid'] = $this->getOrderProductId();
294
		$list['subscription.productid'] = $this->getProductId();
295
		$list['subscription.datenext'] = $this->getDateNext();
296
		$list['subscription.dateend'] = $this->getDateEnd();
297
		$list['subscription.interval'] = $this->getInterval();
298
		$list['subscription.period'] = $this->getPeriod();
299
		$list['subscription.status'] = $this->getStatus();
300
		$list['subscription.reason'] = $this->getReason();
301
302
		return $list;
303
	}
304
}
305