Completed
Push — master ( 75f1fb...9dfb2a )
by Aimeos
08:01
created

Base::checkLanguageId()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 13
nc 10
nop 2
dl 0
loc 26
rs 6.7272
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 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
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces between "Base" and comma; 1 found
Loading history...
23
	implements \Aimeos\MShop\Common\Item\Iface
24
{
25
	private $prefix;
26
	private $values;
27
	private $modified = false;
28
29
	/**
30
	 * Initializes the class properties.
31
	 *
32
	 * @param string $prefix Prefix for the keys returned by toArray()
33
	 * @param array $values Associative list of key/value pairs of the item properties
34
	 */
35
	public function __construct( $prefix, array $values )
36
	{
37
		$this->prefix = (string) $prefix;
38
		$this->values = $values;
39
	}
40
41
42
	/**
43
	 * Returns the ID of the item if available.
44
	 *
45
	 * @return string|null ID of the item
46
	 */
47
	public function getId()
48
	{
49
		if( isset( $this->values['id'] ) && $this->values['id'] != '' ) {
50
			return (string) $this->values['id'];
51
		}
52
53
		$key = $this->prefix . 'id';
54
		return ( isset( $this->values[$key] ) && $this->values[$key] != '' ? (string) $this->values[$key] : null );
55
	}
56
57
58
	/**
59
	 * Sets the new ID of the item.
60
	 *
61
	 * @param string|null $id ID of the item
62
	 * @return \Aimeos\MShop\Common\Item\Iface Item for chaining method calls
63
	 */
64
	public function setId( $id )
65
	{
66
		$key = $this->prefix . 'id';
67
68
		if( ( $this->values[$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...
69
			$this->modified = true;
70
		} else {
71
			$this->modified = false;
72
		}
73
74
		$this->values['id'] = $this->values[$key];
75
		return $this;
76
	}
77
78
79
	/**
80
	 * Returns the site ID of the item.
81
	 *
82
	 * @return integer|null Site ID or null if no site id is available
83
	 */
84
	public function getSiteId()
85
	{
86
		if( isset( $this->values['siteid'] ) ) {
87
			return (int) $this->values['siteid'];
88
		}
89
90
		$key = $this->prefix . 'siteid';
91
		return ( isset( $this->values[$key] ) ? (int) $this->values[$key] : null );
92
	}
93
94
95
	/**
96
	 * Returns modification time of the order coupon.
97
	 *
98
	 * @return string|null Modification time (YYYY-MM-DD HH:mm:ss)
99
	 */
100
	public function getTimeModified()
101
	{
102
		if( isset( $this->values['mtime'] ) ) {
103
			return (string) $this->values['mtime'];
104
		}
105
106
		$key = $this->prefix . 'mtime';
107
		return ( isset( $this->values[$key] ) ? (string) $this->values[$key] : null );
108
	}
109
110
111
	/**
112
	 * Returns the create date of the item.
113
	 *
114
	 * @return string|null ISO date in YYYY-MM-DD hh:mm:ss format
115
	 */
116
	public function getTimeCreated()
117
	{
118
		if( isset( $this->values['ctime'] ) ) {
119
			return (string) $this->values['ctime'];
120
		}
121
122
		$key = $this->prefix . 'ctime';
123
		return ( isset( $this->values[$key] ) ? (string) $this->values[$key] : null );
124
	}
125
126
127
	/**
128
	 * Returns the name of editor who created/modified the item at last.
129
	 *
130
	 * @return string Name of editor who created/modified the item at last
131
	 */
132
	public function getEditor()
133
	{
134
		if( isset( $this->values['editor'] ) ) {
135
			return (string) $this->values['editor'];
136
		}
137
138
		$key = $this->prefix . 'editor';
139
		return ( isset( $this->values[$key] ) ? (string) $this->values[$key] : '' );
140
	}
141
142
143
	/**
144
	 * Tests if this Item object was modified.
145
	 *
146
	 * @return boolean True if modified, false if not
147
	 */
148
	public function isModified()
149
	{
150
		return $this->modified;
151
	}
152
153
154
	/**
155
	 * Sets the modified flag of the object.
156
	 *
157
	 * @return \Aimeos\MShop\Common\Item\Iface Item for chaining method calls
158
	 */
159
	public function setModified()
160
	{
161
		$this->modified = true;
162
		return $this;
163
	}
164
165
166
	/**
167
	 * Sets the item values from the given array.
168
	 *
169
	 * @param array Associative list of item keys and their values
170
	 * @return array Associative list of keys and their values that are unknown
171
	 */
172
	public function fromArray( array $list )
173
	{
174
		if( array_key_exists( $this->prefix . 'id', $list ) )
175
		{
176
			$this->setId( $list[$this->prefix . 'id'] );
177
			unset( $list[$this->prefix . 'id'] );
178
		}
179
180
		unset( $list[$this->prefix . 'siteid'] );
181
		unset( $list[$this->prefix . 'ctime'] );
182
		unset( $list[$this->prefix . 'mtime'] );
183
		unset( $list[$this->prefix . 'editor'] );
184
185
		return $list;
186
	}
187
188
189
	/**
190
	 * Returns the item values as array.
191
	 *
192
	 * @param boolean True to return private properties, false for public only
193
	 * @return array Associative list of item properties and their values
194
	 */
195
	public function toArray( $private = false )
196
	{
197
		$list = [$this->prefix . 'id' => $this->getId()];
198
199
		if( $private === true )
200
		{
201
			$list[$this->prefix . 'siteid'] = $this->getSiteId();
202
			$list[$this->prefix . 'ctime'] = $this->getTimeCreated();
203
			$list[$this->prefix . 'mtime'] = $this->getTimeModified();
204
			$list[$this->prefix . 'editor'] = $this->getEditor();
205
		}
206
207
		return $list;
208
	}
209
210
211
	/**
212
	 * Checks if the new ID is valid for the item.
213
	 *
214
	 * @param string $old Current ID of the item
215
	 * @param string $new New ID which should be set in the item
216
	 * @return string Value of the new ID
217
	 * @throws \Aimeos\MShop\Common\Exception if the ID is not null or not the same as the old one
218
	 */
219
	public static function checkId( $old, $new )
220
	{
221
		if( $new != null && $old != null && $old != $new ) {
222
			throw new \Aimeos\MShop\Exception( sprintf( 'New ID "%1$s" for item differs from old ID "%2$s"', $new, $old ) );
223
		}
224
225
		return $new;
226
	}
227
228
229
	/**
230
	 * Tests if the date parameter represents an ISO format.
231
	 *
232
	 * @param string|null $date ISO date in yyyy-mm-dd HH:ii:ss format or null
233
	 * @return string|null Clean date or null for no date
234
	 * @throws \Aimeos\MShop\Exception If the date is invalid
235
	 */
236
	protected function checkDateFormat( $date )
237
	{
238
		$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])?$/';
239
240
		if( $date != null )
241
		{
242
			if( preg_match( $regex, (string) $date ) !== 1 ) {
243
				throw new \Aimeos\MShop\Exception( sprintf( 'Invalid characters in date "%1$s". ISO format "YYYY-MM-DD hh:mm:ss" expected.', $date ) );
244
			}
245
246
			return str_replace( 'T', ' ', (string) $date );
247
		}
248
	}
249
250
251
	/**
252
	 * Tests if the code is valid.
253
	 *
254
	 * @param string $code New code for an item
255
	 * @return string Item code
256
	 * @throws \Aimeos\MShop\Exception If the code is invalid
257
	 */
258
	protected function checkCode( $code )
259
	{
260
		if( strlen( $code ) > 32 ) {
261
			throw new \Aimeos\MShop\Exception( sprintf( 'Code "%1$s" must not be longer than 32 characters', $code ) );
262
		}
263
264
		return (string) $code;
265
	}
266
267
268
	/**
269
	 * Tests if the currency ID parameter represents an ISO currency format.
270
	 *
271
	 * @param string|null $currencyid Three letter ISO currency format, e.g. EUR
272
	 * @param boolean $null True if null is allowed, false if not
273
	 * @return string|null Three letter ISO currency ID or null for no currency
274
	 * @throws \Aimeos\MShop\Exception If the currency ID is invalid
275
	 */
276
	protected function checkCurrencyId( $currencyid, $null = true )
277
	{
278
		if( $null === false && $currencyid == null ) {
279
			throw new \Aimeos\MShop\Exception( sprintf( 'Invalid ISO currency code "%1$s"', '<null>' ) );
280
		}
281
282
		if( $currencyid != null )
283
		{
284
			if( preg_match( '/^[A-Z]{3}$/', $currencyid ) !== 1 ) {
285
				throw new \Aimeos\MShop\Exception( sprintf( 'Invalid ISO currency code "%1$s"', $currencyid ) );
286
			}
287
288
			return $currencyid;
289
		}
290
	}
291
292
293
	/**
294
	 * Tests if the language ID parameter represents an ISO language format.
295
	 *
296
	 * @param string|null $langid ISO language format, e.g. de or de_DE
297
	 * @param boolean $null True if null is allowed, false if not
298
	 * @return string|null ISO language ID or null for no language
299
	 * @throws \Aimeos\MShop\Exception If the language ID is invalid
300
	 */
301
	protected function checkLanguageId( $langid, $null = true )
302
	{
303
		if( $langid === '' ) {
304
			$langid = null;
305
		}
306
307
		if( $null === false && $langid == null ) {
308
			throw new \Aimeos\MShop\Exception( sprintf( 'Invalid ISO language code "%1$s"', '<null>' ) );
309
		}
310
311
		if( $langid != null )
312
		{
313
			if( preg_match( '/^[a-zA-Z]{2}(_[a-zA-Z]{2})?$/', $langid ) !== 1 ) {
314
				throw new \Aimeos\MShop\Exception( sprintf( 'Invalid ISO language code "%1$s"', $langid ) );
315
			}
316
317
			$parts = explode( '_', $langid );
318
			$parts[0] = strtolower( $parts[0] );
319
320
			if( isset( $parts[1] ) ) {
321
				$parts[1] = strtoupper( $parts[1] );
322
			}
323
324
			return implode( '_', $parts );
325
		}
326
	}
327
328
329
	/**
330
	 * Returns the raw value list.
331
	 *
332
	 * @return array Associative list of key/value pairs
333
	 */
334
	protected function getRawValues()
335
	{
336
		return $this->values;
337
	}
338
}
339