Completed
Push — master ( 977be3...987459 )
by Aimeos
10:12
created

Standard::fromArray()   C

Complexity

Conditions 9
Paths 9

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 14
nc 9
nop 1
dl 0
loc 22
rs 6.412
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-2016
7
 * @package MShop
8
 * @subpackage Customer
9
 */
10
11
12
namespace Aimeos\MShop\Customer\Item;
13
14
15
/**
16
 * Interface for customer DTO objects used by the shop.
17
 *
18
 * @package MShop
19
 * @subpackage Customer
20
 */
21
class Standard extends Base implements Iface
22
{
23
	private $values;
24
	private $helper;
25
	private $salt;
26
27
28
	/**
29
	 * Initializes the customer item object
30
	 *
31
	 * @param \Aimeos\MShop\Common\Item\Address\Iface $address Payment address item object
32
	 * @param array $values List of attributes that belong to the customer item
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 string $salt Password salt (optional)
0 ignored issues
show
Documentation introduced by
Should the type for parameter $salt not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
36
	 * @param \Aimeos\MShop\Common\Item\Helper\Password\Iface|null $helper Password encryption helper object
37
	 * @param \Aimeos\MShop\Customer\Item\Address\Iface[] $addresses List of delivery addresses
38
	 */
39
	public function __construct( \Aimeos\MShop\Common\Item\Address\Iface $address, array $values = [],
40
		array $listItems = [], array $refItems = [], $salt = null,
41
		\Aimeos\MShop\Common\Item\Helper\Password\Iface $helper = null, array $addresses = [] )
42
	{
43
		parent::__construct( $address, $values, $listItems, $refItems, $addresses );
44
45
		$this->values = $values;
46
		$this->helper = $helper;
47
		$this->salt = $salt;
48
	}
49
50
51
	/**
52
	 * Sets the new ID of the item.
53
	 *
54
	 * @param string|null $id ID of the item
55
	 */
56
	public function setId( $id )
57
	{
58
		parent::setId( $id );
59
60
		// set modified flag
61
		$addr = $this->getPaymentAddress();
62
		$addr->setId( null );
63
		$addr->setId( $this->getId() );
64
65
		return $this;
66
	}
67
68
69
	/**
70
	 * Returns the label of the customer item.
71
	 *
72
	 * @return string Label of the customer item
73
	 */
74
	public function getLabel()
75
	{
76
		if( isset( $this->values['customer.label'] ) ) {
77
			return (string) $this->values['customer.label'];
78
		}
79
80
		return '';
81
	}
82
83
84
	/**
85
	 * Sets the new label of the customer item.
86
	 *
87
	 * @param string $value Label of the customer item
88
	 * @return \Aimeos\MShop\Customer\Item\Iface Customer item for chaining method calls
89
	 */
90
	public function setLabel( $value )
91
	{
92
		if( $value == $this->getLabel() ) { return $this; }
93
94
		$this->values['customer.label'] = (string) $value;
95
		$this->setModified();
96
97
		return $this;
98
	}
99
100
101
	/**
102
	 * Returns the status of the item.
103
	 *
104
	 * @return integer Status of the item
105
	 */
106
	public function getStatus()
107
	{
108
		if( isset( $this->values['customer.status'] ) ) {
109
			return (int) $this->values['customer.status'];
110
		}
111
112
		return 0;
113
	}
114
115
116
	/**
117
	 * Sets the status of the item.
118
	 *
119
	 * @param integer $value Status of the item
120
	 * @return \Aimeos\MShop\Customer\Item\Iface Customer item for chaining method calls
121
	 */
122
	public function setStatus( $value )
123
	{
124
		if( $value == $this->getStatus() ) { return $this; }
125
126
		$this->values['customer.status'] = (int) $value;
127
		$this->setModified();
128
129
		return $this;
130
	}
131
132
133
	/**
134
	 * Returns the code of the customer item.
135
	 *
136
	 * @return string Code of the customer item
137
	 */
138
	public function getCode()
139
	{
140
		if( isset( $this->values['customer.code'] ) ) {
141
			return (string) $this->values['customer.code'];
142
		}
143
144
		return '';
145
	}
146
147
148
	/**
149
	 * Sets the new code of the customer item.
150
	 *
151
	 * @param string $value Code of the customer item
152
	 * @return \Aimeos\MShop\Customer\Item\Iface Customer item for chaining method calls
153
	 */
154
	public function setCode( $value )
155
	{
156
		if( $value == $this->getCode() ) { return $this; }
157
158
		$this->values['customer.code'] = (string) $value;
159
		$this->setModified();
160
161
		return $this;
162
	}
163
164
165
	/**
166
	 * Returns the birthday of the customer item.
167
	 *
168
	 * @return string|null Birthday in YYYY-MM-DD format
169
	 */
170
	public function getBirthday()
171
	{
172
		if( isset( $this->values['customer.birthday'] ) ) {
173
			return (string) $this->values['customer.birthday'];
174
		}
175
176
		return null;
177
	}
178
179
180
	/**
181
	 * Sets the birthday of the customer item.
182
	 *
183
	 * @param string|null $value Birthday of the customer item
184
	 * @return \Aimeos\MShop\Customer\Item\Iface Customer item for chaining method calls
185
	 */
186
	public function setBirthday( $value )
187
	{
188
		if( $value === $this->getBirthday() ) { return $this; }
189
190
		$this->values['customer.birthday'] = $this->checkDateOnlyFormat( $value );
191
		$this->setModified();
192
193
		return $this;
194
	}
195
196
197
	/**
198
	 * Returns the password of the customer item.
199
	 *
200
	 * @return string
201
	 */
202
	public function getPassword()
203
	{
204
		if( isset( $this->values['customer.password'] ) ) {
205
			return (string) $this->values['customer.password'];
206
		}
207
208
		return '';
209
	}
210
211
212
	/**
213
	 * Sets the password of the customer item.
214
	 *
215
	 * @param string $value password of the customer item
216
	 * @return \Aimeos\MShop\Customer\Item\Iface Customer item for chaining method calls
217
	 */
218
	public function setPassword( $value )
219
	{
220
		if( $value == $this->getPassword() ) { return $this; }
221
222
		if( $this->helper !== null ) {
223
			$value = $this->helper->encode( $value, $this->salt );
224
		}
225
226
		$this->values['customer.password'] = (string) $value;
227
		$this->setModified();
228
229
		return $this;
230
	}
231
232
233
	/**
234
	 * Returns the last verification date of the customer.
235
	 *
236
	 * @return string|null Last verification date of the customer (YYYY-MM-DD format) or null if unknown
237
	 */
238
	public function getDateVerified()
239
	{
240
		if( isset( $this->values['customer.dateverified'] ) ) {
241
			return (string) $this->values['customer.dateverified'];
242
		}
243
244
		return null;
245
	}
246
247
248
	/**
249
	 * Sets the latest verification date of the customer.
250
	 *
251
	 * @param string|null $value Latest verification date of the customer (YYYY-MM-DD) or null if unknown
252
	 * @return \Aimeos\MShop\Customer\Item\Iface Customer item for chaining method calls
253
	 */
254
	public function setDateVerified( $value )
255
	{
256
		if( $value === $this->getDateVerified() ) { return $this; }
257
258
		$this->values['customer.dateverified'] = $this->checkDateOnlyFormat( $value );
259
		$this->setModified();
260
261
		return $this;
262
	}
263
264
265
	/**
266
	 * Returns the group IDs the customer belongs to
267
	 *
268
	 * @return array List of group IDs
269
	 */
270
	public function getGroups()
271
	{
272
		if( !isset( $this->values['groups'] ) )
273
		{
274
			$this->values['groups'] = [];
275
276
			foreach( $this->getListItems( 'customer/group', 'default' ) as $listItem ) {
277
				$this->values['groups'][] = $listItem->getRefId();
278
			}
279
		}
280
281
		return (array) $this->values['groups'];
282
	}
283
284
	/**
285
	 * Sets the group IDs the customer belongs to
286
	 *
287
	 * @param array $ids List of group IDs
288
	 * @return \Aimeos\MShop\Customer\Item\Iface Customer item for chaining method calls
289
	 */
290
	public function setGroups( array $ids )
291
	{
292
		$this->values['groups'] = $ids;
293
		$this->setModified();
294
295
		return $this;
296
	}
297
298
299
	/**
300
	 * Sets the item values from the given array.
301
	 *
302
	 * @param array $list Associative list of item keys and their values
303
	 * @return array Associative list of keys and their values that are unknown
304
	 */
305
	public function fromArray( array $list )
306
	{
307
		$unknown = [];
308
		$list = parent::fromArray( $list );
309
310
		foreach( $list as $key => $value )
311
		{
312
			switch( $key )
313
			{
314
				case 'customer.label': $this->setLabel( $value ); break;
315
				case 'customer.code': $this->setCode( $value ); break;
316
				case 'customer.birthday': $this->setBirthday( $value ); break;
317
				case 'customer.status': $this->setStatus( $value ); break;
318
				case 'customer.groups': $this->setGroups( $value ); break;
319
				case 'customer.password': $this->setPassword( $value ); break;
320
				case 'customer.dateverified': $this->setDateVerified( $value ); break;
321
				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...
322
			}
323
		}
324
325
		return $unknown;
326
	}
327
328
329
	/**
330
	 * Returns the item values as array.
331
	 *
332
	 * @return array Associative list of item properties and their values
333
	 */
334
	public function toArray()
335
	{
336
		$list = parent::toArray();
337
338
		$list['customer.label'] = $this->getLabel();
339
		$list['customer.code'] = $this->getCode();
340
		$list['customer.birthday'] = $this->getBirthday();
341
		$list['customer.status'] = $this->getStatus();
342
		$list['customer.groups'] = $this->getGroups();
343
		$list['customer.password'] = $this->getPassword();
344
		$list['customer.dateverified'] = $this->getDateVerified();
345
346
		return $list;
347
	}
348
349
350
	/**
351
	 * Tests if the date param represents an ISO format.
352
	 *
353
	 * @param string|null $date ISO date in YYYY-MM-DD format or null for no date
354
	 */
355
	protected function checkDateOnlyFormat( $date )
356
	{
357
		if( $date !== null )
358
		{
359
			if( preg_match( '/^[0-9]{4}-[0-1][0-9]-[0-3][0-9]$/', (string) $date ) !== 1 ) {
360
				throw new \Aimeos\MShop\Exception( sprintf( 'Invalid characters in date "%1$s". ISO format "YYYY-MM-DD" expected.', $date ) );
361
			}
362
363
			return (string) $date;
364
		}
365
	}
366
}
367