Completed
Push — master ( d29f07...779818 )
by Aimeos
21:15
created

Standard::getPosition()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
rs 9.4285
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 Common
9
 */
10
11
12
namespace Aimeos\MShop\Common\Item\Type;
13
14
15
/**
16
 * Default implementation of the list item.
17
 *
18
 * @package MShop
19
 * @subpackage Common
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\Common\Item\Type\Iface
24
{
25
	private $prefix;
26
	private $values;
27
28
29
	/**
30
	 * Initializes the type item object.
31
	 *
32
	 * @param string $prefix Property prefix when converting to array
33
	 * @param array $values Initial values of the list type item
34
	 */
35
	public function __construct( $prefix, array $values = [] )
36
	{
37
		parent::__construct( $prefix, $values );
38
39
		$this->prefix = $prefix;
40
		$this->values = $values;
41
	}
42
43
44
	/**
45
	 * Returns the code of the common list type item
46
	 *
47
	 * @return string Code of the common list type item
48
	 */
49
	public function getCode()
50
	{
51
		if( isset( $this->values[$this->prefix . 'code'] ) ) {
52
			return (string) $this->values[$this->prefix . 'code'];
53
		}
54
55
		return '';
56
	}
57
58
59
	/**
60
	 * Sets the code of the common list type item
61
	 *
62
	 * @param string $code New code of the common list type item
63
	 * @return \Aimeos\MShop\Common\Item\Type\Iface Common type item for chaining method calls
64
	 */
65
	public function setCode( $code )
66
	{
67
		if( (string) $code !== $this->getCode() )
68
		{
69
			$this->values[$this->prefix . 'code'] = $this->checkCode( $code );
70
			$this->setModified();
71
		}
72
73
		return $this;
74
	}
75
76
77
	/**
78
	 * Returns the domain of the common list type item
79
	 *
80
	 * @return string Domain of the common list type item
81
	 */
82
	public function getDomain()
83
	{
84
		if( isset( $this->values[$this->prefix . 'domain'] ) ) {
85
			return (string) $this->values[$this->prefix . 'domain'];
86
		}
87
88
		return '';
89
	}
90
91
92
	/**
93
	 * Sets the domain of the common list type item
94
	 *
95
	 * @param string $domain New domain of the common list type item
96
	 * @return \Aimeos\MShop\Common\Item\Type\Iface Common type item for chaining method calls
97
	 */
98
	public function setDomain( $domain )
99
	{
100
		if( (string) $domain !== $this->getDomain() )
101
		{
102
			$this->values[$this->prefix . 'domain'] = (string) $domain;
103
			$this->setModified();
104
		}
105
106
		return $this;
107
	}
108
109
110
	/**
111
	 * Returns the translated name for the type item
112
	 *
113
	 * @return string Translated name of the type item
114
	 */
115
	public function getName()
116
	{
117
		if( isset( $this->values[$this->prefix . 'name'] ) ) {
118
			return (string) $this->values[$this->prefix . 'name'];
119
		}
120
121
		return $this->getLabel();
122
	}
123
124
125
	/**
126
	 * Returns the label of the common list type item
127
	 *
128
	 * @return string Label of the common list type item
129
	 */
130
	public function getLabel()
131
	{
132
		if( isset( $this->values[$this->prefix . 'label'] ) ) {
133
			return (string) $this->values[$this->prefix . 'label'];
134
		}
135
136
		return '';
137
	}
138
139
140
	/**
141
	 * Sets the label of the common list type item
142
	 *
143
	 * @param string $label New label of the common list type item
144
	 * @return \Aimeos\MShop\Common\Item\Type\Iface Common type item for chaining method calls
145
	 */
146
	public function setLabel( $label )
147
	{
148
		if( (string) $label !== $this->getLabel() )
149
		{
150
			$this->values[$this->prefix . 'label'] = (string) $label;
151
			$this->setModified();
152
		}
153
154
		return $this;
155
	}
156
157
	/**
158
	 * Returns the position of the item in the list.
159
	 *
160
	 * @return integer Position of the item in the list
161
	 */
162
	public function getPosition()
163
	{
164
		if( isset( $this->values[$this->prefix . 'position'] ) ) {
165
			return (int) $this->values[$this->prefix . 'position'];
166
		}
167
168
		return 0;
169
	}
170
171
172
	/**
173
	 * Sets the new position of the item in the list.
174
	 *
175
	 * @param integer $pos position of the item in the list
176
	 * @return \Aimeos\MShop\Common\Item\Iface Item for chaining method calls
177
	 */
178
	public function setPosition( $pos )
179
	{
180
		if( (int) $pos !== $this->getPosition() )
181
		{
182
			$this->values[$this->prefix . 'position'] = (int) $pos;
183
			$this->setModified();
184
		}
185
186
		return $this;
187
	}
188
189
190
	/**
191
	 * Returns the status of the common list type item
192
	 *
193
	 * @return integer Status of the common list type item
194
	 */
195
	public function getStatus()
196
	{
197
		if( isset( $this->values[$this->prefix . 'status'] ) ) {
198
			return (int) $this->values[$this->prefix . 'status'];
199
		}
200
201
		return 0;
202
	}
203
204
205
	/**
206
	 * Sets the status of the common list type item
207
	 *
208
	 * @param integer $status New status of the common list type item
209
	 * @return \Aimeos\MShop\Common\Item\Type\Iface Common type item for chaining method calls
210
	 */
211
	public function setStatus( $status )
212
	{
213
		if( (int) $status !== $this->getStatus() )
214
		{
215
			$this->values[$this->prefix . 'status'] = (int) $status;
216
			$this->setModified();
217
		}
218
219
		return $this;
220
	}
221
222
223
	/**
224
	 * Returns the item type
225
	 *
226
	 * @return string Item type, subtypes are separated by slashes
227
	 */
228
	public function getResourceType()
229
	{
230
		return str_replace( '.', '/', rtrim( $this->prefix, '.' ) );
231
	}
232
233
234
	/**
235
	 * Tests if the item is available based on status, time, language and currency
236
	 *
237
	 * @return boolean True if available, false if not
238
	 */
239
	public function isAvailable()
240
	{
241
		return parent::isAvailable() && (bool) $this->getStatus();
242
	}
243
244
245
	/**
246
	 * Sets the item values from the given array.
247
	 *
248
	 * @param array $list Associative list of item keys and their values
249
	 * @return array Associative list of keys and their values that are unknown
250
	 */
251
	public function fromArray( array $list )
252
	{
253
		$unknown = [];
254
		$list = parent::fromArray( $list );
255
		unset( $list[$this->prefix . 'name'] );
256
257
		foreach( $list as $key => $value )
258
		{
259
			switch( $key )
260
			{
261
				case $this->prefix . 'code': $this->setCode( $value ); break;
262
				case $this->prefix . 'domain': $this->setDomain( $value ); break;
263
				case $this->prefix . 'label': $this->setLabel( $value ); break;
264
				case $this->prefix . 'position': $this->setPosition( $value ); break;
265
				case $this->prefix . 'status': $this->setStatus( $value ); break;
266
				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...
267
			}
268
		}
269
270
		return $unknown;
271
	}
272
273
274
	/**
275
	 * Returns an associative list of item properties.
276
	 *
277
	 * @param boolean True to return private properties, false for public only
278
	 * @return array List of item properties.
279
	 */
280
	public function toArray( $private = false )
281
	{
282
		$list = parent::toArray( $private );
283
284
		$list[$this->prefix . 'code'] = $this->getCode();
285
		$list[$this->prefix . 'domain'] = $this->getDomain();
286
		$list[$this->prefix . 'name'] = $this->getName();
287
		$list[$this->prefix . 'label'] = $this->getLabel();
288
		$list[$this->prefix . 'position'] = $this->getPosition();
289
		$list[$this->prefix . 'status'] = $this->getStatus();
290
291
		return $list;
292
	}
293
}
294