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 Common |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace Aimeos\MShop\Common\Item; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Common methods for all item objects. |
17
|
|
|
* |
18
|
|
|
* @package MShop |
19
|
|
|
* @subpackage Common |
20
|
|
|
*/ |
21
|
|
|
abstract class Base |
22
|
|
|
extends \Aimeos\MW\Common\Item\Base |
|
|
|
|
23
|
|
|
implements \Aimeos\MShop\Common\Item\Iface |
24
|
|
|
{ |
25
|
|
|
private $bdata; |
26
|
|
|
private $prefix; |
27
|
|
|
private $available = true; |
28
|
|
|
private $modified = false; |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Initializes the class properties. |
33
|
|
|
* |
34
|
|
|
* @param string $prefix Prefix for the keys returned by toArray() |
35
|
|
|
* @param array $values Associative list of key/value pairs of the item properties |
36
|
|
|
*/ |
37
|
|
|
public function __construct( $prefix, array $values ) |
38
|
|
|
{ |
39
|
|
|
$this->prefix = (string) $prefix; |
40
|
|
|
$this->bdata = $values; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Returns the item property for the given name |
46
|
|
|
* |
47
|
|
|
* @param string $name Name of the property |
48
|
|
|
* @return mixed|null Property value or null if property is unknown |
49
|
|
|
*/ |
50
|
|
|
public function __get( $name ) |
51
|
|
|
{ |
52
|
|
|
if( isset( $this->bdata[$name] ) ) { |
53
|
|
|
return $this->bdata[$name]; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Tests if the item property for the given name is available |
60
|
|
|
* |
61
|
|
|
* @param string $name Name of the property |
62
|
|
|
* @return boolean True if the property exists, false if not |
63
|
|
|
*/ |
64
|
|
|
public function __isset( $name ) |
65
|
|
|
{ |
66
|
|
|
if( array_key_exists( $name, $this->bdata ) ) { |
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return false; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns the ID of the item if available. |
76
|
|
|
* |
77
|
|
|
* @return string|null ID of the item |
78
|
|
|
*/ |
79
|
|
|
public function getId() |
80
|
|
|
{ |
81
|
|
|
$key = $this->prefix . 'id'; |
82
|
|
|
|
83
|
|
|
if( isset( $this->bdata[$key] ) && $this->bdata[$key] != '' ) { |
84
|
|
|
return (string) $this->bdata[$key]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if( isset( $this->bdata['id'] ) && $this->bdata['id'] != '' ) { |
88
|
|
|
return (string) $this->bdata['id']; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Sets the new ID of the item. |
95
|
|
|
* |
96
|
|
|
* @param string|null $id ID of the item |
97
|
|
|
* @return \Aimeos\MShop\Common\Item\Iface Item for chaining method calls |
98
|
|
|
*/ |
99
|
|
|
public function setId( $id ) |
100
|
|
|
{ |
101
|
|
|
$key = $this->prefix . 'id'; |
102
|
|
|
|
103
|
|
|
if( ( $this->bdata[$key] = \Aimeos\MShop\Common\Item\Base::checkId( $this->getId(), $id ) ) === null ) { |
|
|
|
|
104
|
|
|
$this->modified = true; |
105
|
|
|
} else { |
106
|
|
|
$this->modified = false; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$this->bdata['id'] = $this->bdata[$key]; |
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Returns the site ID of the item. |
116
|
|
|
* |
117
|
|
|
* @return string|null Site ID or null if no site id is available |
118
|
|
|
*/ |
119
|
|
|
public function getSiteId() |
120
|
|
|
{ |
121
|
|
|
$key = $this->prefix . 'siteid'; |
122
|
|
|
|
123
|
|
|
if( isset( $this->bdata[$key] ) ) { |
124
|
|
|
return (string) $this->bdata[$key]; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if( isset( $this->bdata['siteid'] ) ) { |
128
|
|
|
return (string) $this->bdata['siteid']; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Returns modify date/time of the order coupon. |
135
|
|
|
* |
136
|
|
|
* @return string|null Modification time (YYYY-MM-DD HH:mm:ss) |
137
|
|
|
*/ |
138
|
|
|
public function getTimeModified() |
139
|
|
|
{ |
140
|
|
|
$key = $this->prefix . 'mtime'; |
141
|
|
|
|
142
|
|
|
if( isset( $this->bdata[$key] ) ) { |
143
|
|
|
return (string) $this->bdata[$key]; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
if( isset( $this->bdata['mtime'] ) ) { |
147
|
|
|
return (string) $this->bdata['mtime']; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Returns the create date of the item. |
154
|
|
|
* |
155
|
|
|
* @return string|null ISO date in YYYY-MM-DD hh:mm:ss format |
156
|
|
|
*/ |
157
|
|
|
public function getTimeCreated() |
158
|
|
|
{ |
159
|
|
|
$key = $this->prefix . 'ctime'; |
160
|
|
|
|
161
|
|
|
if( isset( $this->bdata[$key] ) ) { |
162
|
|
|
return (string) $this->bdata[$key]; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
if( isset( $this->bdata['ctime'] ) ) { |
166
|
|
|
return (string) $this->bdata['ctime']; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Returns the name of editor who created/modified the item at last. |
173
|
|
|
* |
174
|
|
|
* @return string Name of editor who created/modified the item at last |
175
|
|
|
*/ |
176
|
|
|
public function getEditor() |
177
|
|
|
{ |
178
|
|
|
$key = $this->prefix . 'editor'; |
179
|
|
|
|
180
|
|
|
if( isset( $this->bdata[$key] ) ) { |
181
|
|
|
return (string) $this->bdata[$key]; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
if( isset( $this->bdata['editor'] ) ) { |
185
|
|
|
return (string) $this->bdata['editor']; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return ''; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Tests if the item is available based on status, time, language and currency |
194
|
|
|
* |
195
|
|
|
* @return boolean True if available, false if not |
196
|
|
|
*/ |
197
|
|
|
public function isAvailable() |
198
|
|
|
{ |
199
|
|
|
return $this->available; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Sets the general availability of the item |
205
|
|
|
* |
206
|
|
|
* @return boolean $value True if available, false if not |
|
|
|
|
207
|
|
|
*/ |
208
|
|
|
public function setAvailable( $value ) |
209
|
|
|
{ |
210
|
|
|
$this->available = (bool) $value; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Tests if this Item object was modified. |
216
|
|
|
* |
217
|
|
|
* @return boolean True if modified, false if not |
218
|
|
|
*/ |
219
|
|
|
public function isModified() |
220
|
|
|
{ |
221
|
|
|
return $this->modified; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Sets the modified flag of the object. |
227
|
|
|
* |
228
|
|
|
* @return \Aimeos\MShop\Common\Item\Iface Item for chaining method calls |
229
|
|
|
*/ |
230
|
|
|
public function setModified() |
231
|
|
|
{ |
232
|
|
|
$this->modified = true; |
233
|
|
|
return $this; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Sets the item values from the given array. |
239
|
|
|
* |
240
|
|
|
* @param array Associative list of item keys and their values |
241
|
|
|
* @return array Associative list of keys and their values that are unknown |
242
|
|
|
*/ |
243
|
|
|
public function fromArray( array $list ) |
244
|
|
|
{ |
245
|
|
|
if( array_key_exists( $this->prefix . 'id', $list ) ) |
246
|
|
|
{ |
247
|
|
|
$this->setId( $list[$this->prefix . 'id'] ); |
248
|
|
|
unset( $list[$this->prefix . 'id'] ); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
unset( $list[$this->prefix . 'siteid'] ); |
252
|
|
|
unset( $list[$this->prefix . 'ctime'] ); |
253
|
|
|
unset( $list[$this->prefix . 'mtime'] ); |
254
|
|
|
unset( $list[$this->prefix . 'editor'] ); |
255
|
|
|
|
256
|
|
|
return $list; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Returns the item values as array. |
262
|
|
|
* |
263
|
|
|
* @param boolean True to return private properties, false for public only |
264
|
|
|
* @return array Associative list of item properties and their values |
265
|
|
|
*/ |
266
|
|
|
public function toArray( $private = false ) |
267
|
|
|
{ |
268
|
|
|
$list = [$this->prefix . 'id' => $this->getId()]; |
269
|
|
|
|
270
|
|
|
if( $private === true ) |
271
|
|
|
{ |
272
|
|
|
$list[$this->prefix . 'siteid'] = $this->getSiteId(); |
273
|
|
|
$list[$this->prefix . 'ctime'] = $this->getTimeCreated(); |
274
|
|
|
$list[$this->prefix . 'mtime'] = $this->getTimeModified(); |
275
|
|
|
$list[$this->prefix . 'editor'] = $this->getEditor(); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
return $list; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Checks if the new ID is valid for the item. |
284
|
|
|
* |
285
|
|
|
* @param string $old Current ID of the item |
286
|
|
|
* @param string $new New ID which should be set in the item |
287
|
|
|
* @return string Value of the new ID |
288
|
|
|
* @throws \Aimeos\MShop\Common\Exception if the ID is not null or not the same as the old one |
289
|
|
|
*/ |
290
|
|
|
public static function checkId( $old, $new ) |
291
|
|
|
{ |
292
|
|
|
if( $new != null && $old != null && $old != $new ) { |
293
|
|
|
throw new \Aimeos\MShop\Exception( sprintf( 'New ID "%1$s" for item differs from old ID "%2$s"', $new, $old ) ); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
return $new; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Tests if the date parameter represents an ISO format. |
302
|
|
|
* |
303
|
|
|
* @param string|null $date ISO date in yyyy-mm-dd HH:ii:ss format or null |
304
|
|
|
* @return string|null Clean date or null for no date |
305
|
|
|
* @throws \Aimeos\MShop\Exception If the date is invalid |
306
|
|
|
*/ |
307
|
|
|
protected function checkDateFormat( $date ) |
308
|
|
|
{ |
309
|
|
|
$regex = '/^[0-9]{4}-[0-1][0-9]-[0-3][0-9](( |T)[0-2][0-9]:[0-5][0-9](:[0-5][0-9])?)?$/'; |
310
|
|
|
|
311
|
|
|
if( $date != null ) |
312
|
|
|
{ |
313
|
|
|
if( preg_match( $regex, (string) $date ) !== 1 ) { |
314
|
|
|
throw new \Aimeos\MShop\Exception( sprintf( 'Invalid characters in date "%1$s". ISO format "YYYY-MM-DD hh:mm:ss" expected.', $date ) ); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
return str_replace( 'T', ' ', (string) $date ); |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Tests if the code is valid. |
324
|
|
|
* |
325
|
|
|
* @param string $code New code for an item |
326
|
|
|
* @return string Item code |
327
|
|
|
* @throws \Aimeos\MShop\Exception If the code is invalid |
328
|
|
|
*/ |
329
|
|
|
protected function checkCode( $code ) |
330
|
|
|
{ |
331
|
|
|
if( strlen( $code ) > 32 ) { |
332
|
|
|
throw new \Aimeos\MShop\Exception( sprintf( 'Code "%1$s" must not be longer than 32 characters', $code ) ); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
return (string) $code; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Tests if the country ID parameter represents an ISO country format. |
341
|
|
|
* |
342
|
|
|
* @param string|null $countryid Two letter ISO country format, e.g. DE |
343
|
|
|
* @param boolean $null True if null is allowed, false if not |
344
|
|
|
* @return string|null Two letter ISO country ID or null for no country |
345
|
|
|
* @throws \Aimeos\MShop\Exception If the country ID is invalid |
346
|
|
|
*/ |
347
|
|
|
protected function checkCountryId( $countryid, $null = true ) |
348
|
|
|
{ |
349
|
|
|
if( $null === false && $countryid == null ) { |
|
|
|
|
350
|
|
|
throw new \Aimeos\MShop\Exception( sprintf( 'Invalid ISO country code "%1$s"', '<null>' ) ); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
if( $countryid != null ) |
|
|
|
|
354
|
|
|
{ |
355
|
|
|
if( preg_match( '/^[A-Za-z]{2}$/', $countryid ) !== 1 ) { |
356
|
|
|
throw new \Aimeos\MShop\Exception( sprintf( 'Invalid ISO country code "%1$s"', $countryid ) ); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
return strtoupper( $countryid ); |
360
|
|
|
} |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* Tests if the currency ID parameter represents an ISO currency format. |
366
|
|
|
* |
367
|
|
|
* @param string|null $currencyid Three letter ISO currency format, e.g. EUR |
368
|
|
|
* @param boolean $null True if null is allowed, false if not |
369
|
|
|
* @return string|null Three letter ISO currency ID or null for no currency |
370
|
|
|
* @throws \Aimeos\MShop\Exception If the currency ID is invalid |
371
|
|
|
*/ |
372
|
|
|
protected function checkCurrencyId( $currencyid, $null = true ) |
373
|
|
|
{ |
374
|
|
|
if( $null === false && $currencyid == null ) { |
375
|
|
|
throw new \Aimeos\MShop\Exception( sprintf( 'Invalid ISO currency code "%1$s"', '<null>' ) ); |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
if( $currencyid != null ) |
379
|
|
|
{ |
380
|
|
|
if( preg_match( '/^[A-Z]{3}$/', $currencyid ) !== 1 ) { |
381
|
|
|
throw new \Aimeos\MShop\Exception( sprintf( 'Invalid ISO currency code "%1$s"', $currencyid ) ); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
return strtoupper( $currencyid ); |
385
|
|
|
} |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* Tests if the language ID parameter represents an ISO language format. |
391
|
|
|
* |
392
|
|
|
* @param string|null $langid ISO language format, e.g. de or de_DE |
393
|
|
|
* @param boolean $null True if null is allowed, false if not |
394
|
|
|
* @return string|null ISO language ID or null for no language |
395
|
|
|
* @throws \Aimeos\MShop\Exception If the language ID is invalid |
396
|
|
|
*/ |
397
|
|
|
protected function checkLanguageId( $langid, $null = true ) |
398
|
|
|
{ |
399
|
|
|
if( $null === false && $langid == null ) { |
|
|
|
|
400
|
|
|
throw new \Aimeos\MShop\Exception( sprintf( 'Invalid ISO language code "%1$s"', '<null>' ) ); |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
if( $langid != null ) |
404
|
|
|
{ |
405
|
|
|
if( preg_match( '/^[a-zA-Z]{2}(_[a-zA-Z]{2})?$/', $langid ) !== 1 ) { |
406
|
|
|
throw new \Aimeos\MShop\Exception( sprintf( 'Invalid ISO language code "%1$s"', $langid ) ); |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
$parts = explode( '_', $langid ); |
410
|
|
|
$parts[0] = strtolower( $parts[0] ); |
411
|
|
|
|
412
|
|
|
if( isset( $parts[1] ) ) { |
413
|
|
|
$parts[1] = strtoupper( $parts[1] ); |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
return implode( '_', $parts ); |
417
|
|
|
} |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
|
421
|
|
|
/** |
422
|
|
|
* Returns the raw value list. |
423
|
|
|
* |
424
|
|
|
* @return array Associative list of key/value pairs |
425
|
|
|
*/ |
426
|
|
|
protected function getRawValues() |
427
|
|
|
{ |
428
|
|
|
return $this->bdata; |
429
|
|
|
} |
430
|
|
|
} |
431
|
|
|
|