Passed
Push — master ( c9c069...cc1d41 )
by Aimeos
05:10
created

Base::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 7
rs 10
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 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
	 * Creates a deep clone of all objects
46
	 */
47
	public function __clone()
48
	{
49
	}
50
51
52
	/**
53
	 * Returns the item property for the given name
54
	 *
55
	 * @param string $name Name of the property
56
	 * @return mixed|null Property value or null if property is unknown
57
	 */
58
	public function __get( $name )
59
	{
60
		if( isset( $this->bdata[$name] ) ) {
61
			return $this->bdata[$name];
62
		}
63
	}
64
65
66
	/**
67
	 * Tests if the item property for the given name is available
68
	 *
69
	 * @param string $name Name of the property
70
	 * @return boolean True if the property exists, false if not
71
	 */
72
	public function __isset( $name )
73
	{
74
		if( array_key_exists( $name, $this->bdata ) ) {
75
			return true;
76
		}
77
78
		return false;
79
	}
80
81
82
	/**
83
	 * Sets the new item property for the given name
84
	 *
85
	 * @param string $name Name of the property
86
	 * @param mixed $value New property value
87
	 */
88
	public function __set( $name, $value )
89
	{
90
		if( !isset( $this->bdata[$name] ) || $this->bdata[$name] !== $value ) {
91
			$this->setModified();
92
		}
93
94
		$this->bdata[$name] = $value;
95
	}
96
97
98
	/**
99
	 * Returns the item property for the given name
100
	 *
101
	 * @param string $name Name of the property
102
	 * @param mixed $default Default value if property is unknown
103
	 * @return mixed|null Property value or default value if property is unknown
104
	 */
105
	public function get( $name, $default = null )
106
	{
107
		if( isset( $this->bdata[$name] ) ) {
108
			return $this->bdata[$name];
109
		}
110
111
		return $default;
112
	}
113
114
115
	/**
116
	 * Sets the new item property for the given name
117
	 *
118
	 * @param string $name Name of the property
119
	 * @param mixed $value New property value
120
	 * @return \Aimeos\MShop\Order\Item\Base\Iface Order base item for method chaining
121
	 */
122
	public function set( $name, $value )
123
	{
124
		if( !isset( $this->bdata[$name] ) || $this->bdata[$name] !== $value ) {
125
			$this->setModified();
126
		}
127
128
		$this->bdata[$name] = $value;
129
		return $this;
130
	}
131
132
133
	/**
134
	 * Returns the ID of the item if available.
135
	 *
136
	 * @return string|null ID of the item
137
	 */
138
	public function getId()
139
	{
140
		$key = $this->prefix . 'id';
141
142
		if( isset( $this->bdata[$key] ) && $this->bdata[$key] != '' ) {
143
			return (string) $this->bdata[$key];
144
		}
145
146
		if( isset( $this->bdata['id'] ) && $this->bdata['id'] != '' ) {
147
			return (string) $this->bdata['id'];
148
		}
149
	}
150
151
152
	/**
153
	 * Sets the new ID of the item.
154
	 *
155
	 * @param string|null $id ID of the item
156
	 * @return \Aimeos\MShop\Common\Item\Iface Item for chaining method calls
157
	 */
158
	public function setId( $id )
159
	{
160
		$key = $this->prefix . 'id';
161
162
		if( ( $this->bdata[$key] = \Aimeos\MShop\Common\Item\Base::checkId( $this->getId(), $id ) ) === null ) {
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
163
			$this->modified = true;
164
		} else {
165
			$this->modified = false;
166
		}
167
168
		$this->bdata['id'] = $this->bdata[$key];
169
		return $this;
170
	}
171
172
173
	/**
174
	 * Returns the site ID of the item.
175
	 *
176
	 * @return string|null Site ID or null if no site id is available
177
	 */
178
	public function getSiteId()
179
	{
180
		$key = $this->prefix . 'siteid';
181
182
		if( isset( $this->bdata[$key] ) ) {
183
			return (string) $this->bdata[$key];
184
		}
185
186
		if( isset( $this->bdata['siteid'] ) ) {
187
			return (string) $this->bdata['siteid'];
188
		}
189
	}
190
191
192
	/**
193
	 * Returns modify date/time of the order coupon.
194
	 *
195
	 * @return string|null Modification time (YYYY-MM-DD HH:mm:ss)
196
	 */
197
	public function getTimeModified()
198
	{
199
		$key = $this->prefix . 'mtime';
200
201
		if( isset( $this->bdata[$key] ) ) {
202
			return (string) $this->bdata[$key];
203
		}
204
205
		if( isset( $this->bdata['mtime'] ) ) {
206
			return (string) $this->bdata['mtime'];
207
		}
208
	}
209
210
211
	/**
212
	 * Returns the create date of the item.
213
	 *
214
	 * @return string|null ISO date in YYYY-MM-DD hh:mm:ss format
215
	 */
216
	public function getTimeCreated()
217
	{
218
		$key = $this->prefix . 'ctime';
219
220
		if( isset( $this->bdata[$key] ) ) {
221
			return (string) $this->bdata[$key];
222
		}
223
224
		if( isset( $this->bdata['ctime'] ) ) {
225
			return (string) $this->bdata['ctime'];
226
		}
227
	}
228
229
230
	/**
231
	 * Returns the name of editor who created/modified the item at last.
232
	 *
233
	 * @return string Name of editor who created/modified the item at last
234
	 */
235
	public function getEditor()
236
	{
237
		$key = $this->prefix . 'editor';
238
239
		if( isset( $this->bdata[$key] ) ) {
240
			return (string) $this->bdata[$key];
241
		}
242
243
		if( isset( $this->bdata['editor'] ) ) {
244
			return (string) $this->bdata['editor'];
245
		}
246
247
		return '';
248
	}
249
250
251
	/**
252
	 * Tests if the item is available based on status, time, language and currency
253
	 *
254
	 * @return boolean True if available, false if not
255
	 */
256
	public function isAvailable()
257
	{
258
		return $this->available;
259
	}
260
261
262
	/**
263
	 * Sets the general availability of the item
264
	 *
265
	 * @return boolean $value True if available, false if not
266
	 */
267
	public function setAvailable( $value )
268
	{
269
		$this->available = (bool) $value;
270
	}
271
272
273
	/**
274
	 * Tests if this Item object was modified.
275
	 *
276
	 * @return boolean True if modified, false if not
277
	 */
278
	public function isModified()
279
	{
280
		return $this->modified;
281
	}
282
283
284
	/**
285
	 * Sets the modified flag of the object.
286
	 *
287
	 * @return \Aimeos\MShop\Common\Item\Iface Item for chaining method calls
288
	 */
289
	public function setModified()
290
	{
291
		$this->modified = true;
292
		return $this;
293
	}
294
295
296
	/**
297
	 * Sets the item values from the given array and removes that entries from the list
298
	 *
299
	 * @param array $list Associative list of item keys and their values
300
	 * @param boolean True to set private properties too, false for public only
301
	 * @return \Aimeos\MShop\Common\Item\Iface Item for chaining method calls
302
	 */
303
	public function fromArray( array &$list, $private = false )
304
	{
305
		$item = $this;
306
307
		if( $private && array_key_exists( $this->prefix . 'id', $list ) )
308
		{
309
			$item = $item->setId( $list[$this->prefix . 'id'] );
310
			unset( $list[$this->prefix . 'id'] );
311
		}
312
313
		return $item;
314
	}
315
316
317
	/**
318
	 * Returns the item values as array.
319
	 *
320
	 * @param boolean True to return private properties, false for public only
321
	 * @return array Associative list of item properties and their values
322
	 */
323
	public function toArray( $private = false )
324
	{
325
		$list = [$this->prefix . 'id' => $this->getId()];
326
327
		if( $private === true )
328
		{
329
			$list[$this->prefix . 'siteid'] = $this->getSiteId();
330
			$list[$this->prefix . 'ctime'] = $this->getTimeCreated();
331
			$list[$this->prefix . 'mtime'] = $this->getTimeModified();
332
			$list[$this->prefix . 'editor'] = $this->getEditor();
333
		}
334
335
		return $list;
336
	}
337
338
339
	/**
340
	 * Checks if the new ID is valid for the item.
341
	 *
342
	 * @param string $old Current ID of the item
343
	 * @param string $new New ID which should be set in the item
344
	 * @return string Value of the new ID
345
	 * @throws \Aimeos\MShop\Exception if the ID is not null or not the same as the old one
346
	 */
347
	public static function checkId( $old, $new )
348
	{
349
		if( $new != null && $old != null && $old != $new ) {
350
			throw new \Aimeos\MShop\Exception( sprintf( 'New ID "%1$s" for item differs from old ID "%2$s"', $new, $old ) );
351
		}
352
353
		return $new;
354
	}
355
356
357
	/**
358
	 * Tests if the date parameter represents an ISO format.
359
	 *
360
	 * @param string|null $date ISO date in yyyy-mm-dd HH:ii:ss format or null
361
	 * @return string|null Clean date or null for no date
362
	 * @throws \Aimeos\MShop\Exception If the date is invalid
363
	 */
364
	protected function checkDateFormat( $date )
365
	{
366
		$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])?)?$/';
367
368
		if( $date != null )
369
		{
370
			if( preg_match( $regex, (string) $date ) !== 1 ) {
371
				throw new \Aimeos\MShop\Exception( sprintf( 'Invalid characters in date "%1$s". ISO format "YYYY-MM-DD hh:mm:ss" expected.', $date ) );
372
			}
373
374
			return str_replace( 'T', ' ', (string) $date );
375
		}
376
	}
377
378
379
	/**
380
	 * Tests if the code is valid.
381
	 *
382
	 * @param string $code New code for an item
383
	 * @return string Item code
384
	 * @throws \Aimeos\MShop\Exception If the code is invalid
385
	 */
386
	protected function checkCode( $code )
387
	{
388
		if( strlen( $code ) > 64 ) {
389
			throw new \Aimeos\MShop\Exception( sprintf( 'Code "%1$s" must not be longer than 64 characters', $code ) );
390
		}
391
392
		return (string) $code;
393
	}
394
395
396
	/**
397
	 * Tests if the country ID parameter represents an ISO country format.
398
	 *
399
	 * @param string|null $countryid Two letter ISO country format, e.g. DE
400
	 * @param boolean $null True if null is allowed, false if not
401
	 * @return string|null Two letter ISO country ID or null for no country
402
	 * @throws \Aimeos\MShop\Exception If the country ID is invalid
403
	 */
404
	protected function checkCountryId( $countryid, $null = true )
405
	{
406
		if( $null === false && $countryid == null ) {
407
			throw new \Aimeos\MShop\Exception( sprintf( 'Invalid ISO country code "%1$s"', '<null>' ) );
408
		}
409
410
		if( $countryid != null )
411
		{
412
			if( preg_match( '/^[A-Za-z]{2}$/', $countryid ) !== 1 ) {
413
				throw new \Aimeos\MShop\Exception( sprintf( 'Invalid ISO country code "%1$s"', $countryid ) );
414
			}
415
416
			return strtoupper( $countryid );
417
		}
418
	}
419
420
421
	/**
422
	 * Tests if the currency ID parameter represents an ISO currency format.
423
	 *
424
	 * @param string|null $currencyid Three letter ISO currency format, e.g. EUR
425
	 * @param boolean $null True if null is allowed, false if not
426
	 * @return string|null Three letter ISO currency ID or null for no currency
427
	 * @throws \Aimeos\MShop\Exception If the currency ID is invalid
428
	 */
429
	protected function checkCurrencyId( $currencyid, $null = true )
430
	{
431
		if( $null === false && $currencyid == null ) {
432
			throw new \Aimeos\MShop\Exception( sprintf( 'Invalid ISO currency code "%1$s"', '<null>' ) );
433
		}
434
435
		if( $currencyid != null )
436
		{
437
			if( preg_match( '/^[A-Z]{3}$/', $currencyid ) !== 1 ) {
438
				throw new \Aimeos\MShop\Exception( sprintf( 'Invalid ISO currency code "%1$s"', $currencyid ) );
439
			}
440
441
			return strtoupper( $currencyid );
442
		}
443
	}
444
445
446
	/**
447
	 * Tests if the language ID parameter represents an ISO language format.
448
	 *
449
	 * @param string|null $langid ISO language format, e.g. de or de_DE
450
	 * @param boolean $null True if null is allowed, false if not
451
	 * @return string|null ISO language ID or null for no language
452
	 * @throws \Aimeos\MShop\Exception If the language ID is invalid
453
	 */
454
	protected function checkLanguageId( $langid, $null = true )
455
	{
456
		if( $null === false && $langid == null ) {
457
			throw new \Aimeos\MShop\Exception( sprintf( 'Invalid ISO language code "%1$s"', '<null>' ) );
458
		}
459
460
		if( $langid != null )
461
		{
462
			if( preg_match( '/^[a-zA-Z]{2}(_[a-zA-Z]{2})?$/', $langid ) !== 1 ) {
463
				throw new \Aimeos\MShop\Exception( sprintf( 'Invalid ISO language code "%1$s"', $langid ) );
464
			}
465
466
			$parts = explode( '_', $langid );
467
			$parts[0] = strtolower( $parts[0] );
468
469
			if( isset( $parts[1] ) ) {
470
				$parts[1] = strtoupper( $parts[1] );
471
			}
472
473
			return implode( '_', $parts );
474
		}
475
	}
476
477
478
	/**
479
	 * Returns the raw value list.
480
	 *
481
	 * @return array Associative list of key/value pairs
482
	 * @deprecated 2020.01
483
	 */
484
	protected function getRawValues()
485
	{
486
		return $this->bdata;
487
	}
488
}
489