Completed
Push — master ( 24a86a...771b97 )
by Aimeos
10:31
created

Standard::fromArray()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 6
nop 1
dl 0
loc 19
rs 9.0111
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-2017
7
 * @package MShop
8
 * @subpackage Locale
9
 */
10
11
12
namespace Aimeos\MShop\Locale\Item\Language;
13
14
15
/**
16
 * Default implementation of a Language item.
17
 *
18
 * @package MShop
19
 * @subpackage Locale
20
 */
21
class Standard
22
	extends \Aimeos\MShop\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\Locale\Item\Language\Iface
24
{
25
26
	private $modified = false;
27
	private $values;
28
29
30
	/**
31
	 * Initialize the language object.
32
	 *
33
	 * @param array $values Possible params to be set on init.
34
	 */
35
	public function __construct( array $values = [] )
36
	{
37
		parent::__construct( 'locale.language.', $values );
38
39
		$this->values = $values;
40
41
		if( isset( $values['locale.language.id'] ) ) {
42
			$this->setId( $values['locale.language.id'] );
43
		}
44
	}
45
46
47
	/**
48
	 * Returns the id of the language.
49
	 *
50
	 * @return string|null Id of the language
51
	 */
52
	public function getId()
53
	{
54
		if( isset( $this->values['locale.language.id'] ) ) {
55
			return (string) $this->values['locale.language.id'];
56
		}
57
58
		return null;
59
	}
60
61
62
	/**
63
	 * Sets the id of the language.
64
	 *
65
	 * @param string $key Id to set
66
	 * @return \Aimeos\MShop\Locale\Item\Language\Iface Locale language item for chaining method calls
67
	 */
68
	public function setId( $key )
69
	{
70
		if( $key !== null && $key !== '' )
71
		{
72
			$this->setCode( $key );
73
			$this->values['locale.language.id'] = $this->values['locale.language.code'];
74
			$this->modified = false;
75
		}
76
		else
77
		{
78
			$this->values['locale.language.id'] = null;
79
			$this->modified = true;
80
		}
81
82
		return $this;
83
	}
84
85
86
	/**
87
	 * Returns the two letter ISO language code.
88
	 *
89
	 * @return string two letter ISO language code
90
	 */
91
	public function getCode()
92
	{
93
		if( isset( $this->values['locale.language.code'] ) ) {
94
			return (string) $this->values['locale.language.code'];
95
		}
96
97
		return '';
98
	}
99
100
101
	/**
102
	 * Sets the two letter ISO language code.
103
	 *
104
	 * @param string $key two letter ISO language code
105
	 * @return \Aimeos\MShop\Locale\Item\Language\Iface Locale language item for chaining method calls
106
	 */
107
	public function setCode( $key )
108
	{
109
		$matches = [];
110
		$len = strlen( $key );
111
112
		if( $len < 2 || $len > 5 || preg_match( '/^([a-zA-Z]{2,3})((-|_)([a-zA-Z]{2}))?$/', $key, $matches ) !== 1 ) {
113
			throw new \Aimeos\MShop\Locale\Exception( sprintf( 'Invalid characters in ISO language code "%1$s"', $key ) );
114
		}
115
116
		if( (string) $key !== $this->getCode() )
117
		{
118
			$code = strtolower( $matches[1] ) . ( isset( $matches[4] ) ? $matches[3] . $matches[4] : '' );
119
			$this->values['locale.language.code'] = $code;
120
			$this->modified = true;
121
		}
122
123
		return $this;
124
	}
125
126
127
	/**
128
	 * Returns the label property.
129
	 *
130
	 * @return string Returns the label of the language
131
	 */
132
	public function getLabel()
133
	{
134
		if( isset( $this->values['locale.language.label'] ) ) {
135
			return (string) $this->values['locale.language.label'];
136
		}
137
138
		return '';
139
	}
140
141
142
	/**
143
	 * Sets the label property.
144
	 *
145
	 * @param string $label Label of the language
146
	 * @return \Aimeos\MShop\Locale\Item\Language\Iface Locale language item for chaining method calls
147
	 */
148
	public function setLabel( $label )
149
	{
150
		if( (string) $label !== $this->getLabel() )
151
		{
152
			$this->values['locale.language.label'] = (string) $label;
153
			$this->setModified();
154
		}
155
156
		return $this;
157
	}
158
159
160
	/**
161
	 * Returns the status of the item.
162
	 *
163
	 * @return integer Status of the item
164
	 */
165
	public function getStatus()
166
	{
167
		if( isset( $this->values['locale.language.status'] ) ) {
168
			return (int) $this->values['locale.language.status'];
169
		}
170
171
		return 0;
172
	}
173
174
175
	/**
176
	 * Sets the status of the item.
177
	 *
178
	 * @param integer $status Status of the item
179
	 * @return \Aimeos\MShop\Locale\Item\Language\Iface Locale language item for chaining method calls
180
	 */
181
	public function setStatus( $status )
182
	{
183
		if( (int) $status !== $this->getStatus() )
184
		{
185
			$this->values['locale.language.status'] = (int) $status;
186
			$this->setModified();
187
		}
188
189
		return $this;
190
	}
191
192
193
	/**
194
	 * Returns the item type
195
	 *
196
	 * @return string Item type, subtypes are separated by slashes
197
	 */
198
	public function getResourceType()
199
	{
200
		return 'locale/language';
201
	}
202
203
204
	/**
205
	 * Tests if the item is available based on status, time, language and currency
206
	 *
207
	 * @return boolean True if available, false if not
208
	 */
209
	public function isAvailable()
210
	{
211
		return parent::isAvailable() && (bool) $this->getStatus();
212
	}
213
214
215
	/**
216
	 * Sets the item values from the given array.
217
	 *
218
	 * @param array $list Associative list of item keys and their values
219
	 * @return array Associative list of keys and their values that are unknown
220
	 */
221
	public function fromArray( array $list )
222
	{
223
		$unknown = [];
224
		$list = parent::fromArray( $list );
225
226
		foreach( $list as $key => $value )
227
		{
228
			switch( $key )
229
			{
230
				case 'locale.language.id': $this->setId( $value ); break;
231
				case 'locale.language.code': $this->setCode( $value ); break;
232
				case 'locale.language.label': $this->setLabel( $value ); break;
233
				case 'locale.language.status': $this->setStatus( $value ); break;
234
				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...
235
			}
236
		}
237
238
		return $unknown;
239
	}
240
241
242
	/**
243
	 * Returns the item values as array.
244
	 *
245
	 * @param boolean True to return private properties, false for public only
246
	 * @return array Associative list of item properties and their values
247
	 */
248
	public function toArray( $private = false )
249
	{
250
		$list = parent::toArray( $private );
251
252
		$list['locale.language.id'] = $this->getId();
253
		$list['locale.language.code'] = $this->getCode();
254
		$list['locale.language.label'] = $this->getLabel();
255
		$list['locale.language.status'] = $this->getStatus();
256
257
		return $list;
258
	}
259
260
261
	/**
262
	 * Tests if the needed object properties are modified.
263
	 *
264
	 * @return boolean True if modiefied flag was set otherwise false
265
	 */
266
	public function isModified()
267
	{
268
		return $this->modified || parent::isModified();
269
	}
270
271
}
272